轉載:QT QTableView用法小結

來源:互聯網
上載者:User

標籤:

出自:

  http://blog.chinaunix.net/uid-20382483-id-3518513.html

 

QTableView常用於實現資料的表格顯示。下面我們如何按步驟實現學生資訊表格:

一 添加表頭

    //準備資料模型
    QStandardItemModel *student_model = new QStandardItemModel();
    student_model->setHorizontalHeaderItem(0, new QStandardItem(QObject::tr("Name")));
    student_model->setHorizontalHeaderItem(1, new QStandardItem(QObject::tr("NO.")));
    student_model->setHorizontalHeaderItem(2, new QStandardItem(QObject::tr("Sex")));
    student_model->setHorizontalHeaderItem(3, new QStandardItem(QObject::tr("Age")));
    student_model->setHorizontalHeaderItem(4, new QStandardItem(QObject::tr("College")));
    //利用setModel()方法將資料模型與QTableView綁定
    ui->student_tableview->setModel(student_model);


二 設定表格屬性

    //設定列寬不可變動,即不能通過滑鼠拖動增加列寬        
    ui->student_tableview->horizontalHeader()->setResizeMode(0, QHeaderView::Fixed);   
    ui->student_tableview->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);   
    ui->student_tableview->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);   
    ui->student_tableview->horizontalHeader()->setResizeMode(3, QHeaderView::Fixed);   
    ui->student_tableview->horizontalHeader()->setResizeMode(4, QHeaderView::Fixed);   

    //設定表格的各列的寬度值        
    ui->student_tableview->setColumnWidth(0,100);    
    ui->student_tableview->setColumnWidth(1,100);    
    ui->student_tableview->setColumnWidth(2,100);    
    ui->student_tableview->setColumnWidth(3,100);    
    ui->student_tableview->setColumnWidth(4,100);       

    //預設顯示行頭,如果你覺得不美觀的話,我們可以將隱藏        
    ui->student_tableview->verticalHeader()->hide();      

    //設定選中時為整行選中        
    ui->student_tableview->setSelectionBehavior(QAbstractItemView::SelectRows);         
      
    //設定表格的單元為唯讀屬性,即不能編輯        
    ui->student_tableview->setEditTriggers(QAbstractItemView::NoEditTriggers);          

    //如果你用在QTableView中使用右鍵菜單,需啟用該屬性        
    ui->tstudent_tableview->setContextMenuPolicy(Qt::CustomContextMenu);

 

三 動態添加行

    在表格中添加行時,我們只需要在model中插入資料即可,一旦model中的資料發生變化,QTabelView顯示就會做相應的變動

    //在第一行新增學生張三的個人資訊(setItem函數的第一個參數表示行號,第二個表示列號,第三個為要顯示的資料)
    student_model->setItem(0, 0, new QStandardItem(“張三"));
    student_model->setItem(0, 1, new QStandardItem("20120202"));
    student_model->setItem(0, 2, new QStandardItem("男"));
    student_model->setItem(0, 3, new QStandardItem("18"));
    student_model->setItem(0, 4, new QStandardItem("土木學院"));


四 設定資料顯示的樣式

    //設定儲存格文本置中,張三的資料設定為置中顯示
    student_model->item(0, 0)->setTextAlignment(Qt::AlignCenter);
    student_model->item(0, 1)->setTextAlignment(Qt::AlignCenter);
    student_model->item(0, 2)->setTextAlignment(Qt::AlignCenter);
    student_model->item(0, 3)->setTextAlignment(Qt::AlignCenter);
    student_model->item(0, 4)->setTextAlignment(Qt::AlignCenter);

    //設定儲存格文本顏色,張三的資料設定為紅色
    student_model->item(0, 0)->setForeground(QBrush(QColor(255, 0, 0))); 
    student_model->item(0, 1)->setForeground(QBrush(QColor(255, 0, 0))); 
    student_model->item(0, 2)->setForeground(QBrush(QColor(255, 0, 0))); 
    student_model->item(0, 3)->setForeground(QBrush(QColor(255, 0, 0))); 
    student_model->item(0, 4)->setForeground(QBrush(QColor(255, 0, 0)));

    //將字型加粗
    student_model->item(0, 0)->setFont( QFont( "Times", 10, QFont::Black ) );
    student_model->item(0, 1)->setFont( QFont( "Times", 10, QFont::Black ) );
    student_model->item(0, 2)->setFont( QFont( "Times", 10, QFont::Black ) );
    student_model->item(0, 3)->setFont( QFont( "Times", 10, QFont::Black ) );
    student_model->item(0, 4)->setFont( QFont( "Times", 10, QFont::Black ) );

    //設定排序方式,按年齡降序顯示
    student_model->sort(3, Qt::DescendingOrder);

 

 

①、

設定選中單個元素、整行、整列。

ui->student_tableview->setSelectionBehavior(QAbstractItemView::SelectRows);       

enum QAbstractItemView::SelectionBehavior
Constant Value Description
QAbstractItemView::SelectItems 0 Selecting single items.
QAbstractItemView::SelectRows 1 Selecting only rows.
QAbstractItemView::SelectColumns 2 Selecting only columns.
  

②、設定是否允許多選:

ui->student_tableview->setSelectionMode(QAbstractItemView::SingleSelection); 

enum QAbstractItemView::SelectionMode

This enum indicates how the view responds to user selections:

Constant Value Description
QAbstractItemView::SingleSelection 1 When the user selects an item, any already-selected item becomes unselected, and the user cannot unselect the selected item by clicking on it.
QAbstractItemView::ContiguousSelection 4 When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item.
QAbstractItemView::ExtendedSelection 3 When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Ctrl key when clicking on an item, the clicked item gets toggled and all other items are left untouched. If the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item. Multiple items can be selected by dragging the mouse over them.
QAbstractItemView::MultiSelection 2 When the user selects an item in the usual way, the selection status of that item is toggled and the other items are left alone. Multiple items can be toggled by dragging the mouse over them.
QAbstractItemView::NoSelection 0 Items cannot be selected.

The most commonly used modes are SingleSelection and ExtendedSelection.

 

③、設定表的儲存格的編輯模式

ui->student_tableview->setEditTriggers(QAbstractItemView.NoEditTriggers)

enum QAbstractItemView::EditTrigger
flags QAbstractItemView::EditTriggers

This enum describes actions which will initiate item editing.

Constant Value Description
QAbstractItemView::NoEditTriggers 0 No editing possible.
QAbstractItemView::CurrentChanged 1 Editing start whenever current item changes.
QAbstractItemView::DoubleClicked 2 Editing starts when an item is double clicked.
QAbstractItemView::SelectedClicked 4 Editing starts when clicking on an already selected item.
QAbstractItemView::EditKeyPressed 8 Editing starts when the platform edit key has been pressed over an item.
QAbstractItemView::AnyKeyPressed 16 Editing starts when any key is pressed over an item.
QAbstractItemView::AllEditTriggers 31 Editing starts for all above actions.

The EditTriggers type is a typedef for QFlags<EditTrigger>. It stores an OR combination of EditTrigger values

 

 

 

 

 

 

轉載:QT QTableView用法小結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.