From:
Http://blog.chinaunix.net/uid-20382483-id-3518513.html
Qtableview is commonly used to display tabular data in the implementation. Here's how to follow the steps to implement the Student information form:
Add a table header
Preparing the Data Model
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"));
Using the Setmodel () method to bind the data model to Qtableview
Ui->student_tableview->setmodel (Student_model);
Two Set table properties
Set column width is not variable, that is, you cannot increase the column width by dragging the mouse
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);
//sets the width value of each column of the table
ui->student_tableview->setcolumnwidth (0,100);     
ui->student_tableview-> Setcolumnwidth (1,100);     
ui-> Student_tableview->setcolumnwidth (2,100);     
ui->student_tableview->setcolumnwidth (4,100);
//default display wardrobe, if you feel not beautiful, we can hide
Ui->student_tableview->verticalheader ()->hide ();
//Set selected for entire row
Ui->student_tableview->setselectionbehavior ( qabstractitemview::selectrows);
      
//Set table cells as read-only properties, That cannot be edited
Ui->student_tableview->setedittriggers (qabstractitemview::noedittriggers);
If you use the right-click menu in Qtableview, you need to enable this property
Ui->tstudent_tableview->setcontextmenupolicy (Qt::customcontextmenu);
Three dynamic Add lines
when adding rows to a table, we only need to insert the data into the model, and once the data in the model changes, the Qtabelview display changes accordingly
//Add the student Zhang San personal information in the first line (the first parameter of the SetItem function represents the line number, the second represents the column number, and the third is the data to be displayed)
student_model->setitem (0, 0, New Qstandarditem ("Zhang San")) ;
student_model->setitem (0, 1, New Qstandarditem (" 20120202 "));
student_model->setitem (0, 2, new Qstandarditem ("male"));
student_model->setitem (0, 3, New Qstandarditem ("18")) ;
student_model->setitem (0, 4, New Qstandarditem ("School of Civil Engineering" ));
Four sets the style of the data display
//Set cell text centered, Zhang San data set to center display
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);
//Set cell text color, Zhang San data set to red
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));
//font bold
student_model->item (0, 0) ->setfont (Qfont ("Times", ten, Qfont::black));
student_model->item (0, 1)->setfont (QFont ("Times" , Qfont::black));
student_model->item (0, 2)->setfont (QFont ("Times" , Qfont::black));
student_model->item (0, 3)->setfont (QFont ("Times" , Qfont::black));
student_model->item (0, 4)->setfont (QFont ("Times" , Qfont::black));
Set sort order, show by age descending
Student_model->sort (3, Qt::D escendingorder);
①
Sets a single element, an entire row, and an entire column to be selected.
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. |
②, sets whether to allow multiple selections:
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 a item, any already-selected item becomes unselected, and the user cannot unselect the selected ITE m by clicking on it. |
qabstractitemview::contiguousselection |
4 |
when the user selects an item in the usual-a, the selection is cleared and the new IT Em selected. However, if the user presses the Shift key while clicking on a item, all items between the current item and the clicked I TEM is selected or unselected, depending on the state of the clicked item. |
qabstractitemview::extendedselection |
3 |
when the user selects an item in the usual-a, the selection is cleared and the new IT Em selected. However, if the user presses the Ctrl key when clicking on a item, the clicked item gets toggled and all other items is Left untouched. If the user presses the Shift key while clicking on a item, all items between the current item and the clicked item is s Elected 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 a item in the usual-in-the-same, the selection status of that item is toggled and the other items were 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 is singleselection and extendedselection.
③, setting the editing mode of the table cell
Ui->student_tableview->setedittriggers (Qabstractitemview.noedittriggers)
Enum Qabstractitemview::Edittrigger
Flags Qabstractitemview::edittriggers
This enum describes actions which would 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 a item is a double clicked. |
QAbstractItemView::SelectedClicked |
4 |
Editing starts when clicking on an already selected item. |
QAbstractItemView::EditKeyPressed |
8 |
Editing starts when the platform edit key had been pressed over an item. |
QAbstractItemView::AnyKeyPressed |
16 |
Editing starts when the any key was 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
Reprint: QT Qtableview Usage Summary