Qtablewidget is a common space for displaying data tables in QT programs, similar to the DataGrid in VC and C #. Speaking of Qtablewidget, we must tell the difference between it and Qtabelview. Qtablewidget is a subclass of Qtableview, the main difference is that Qtableview can use a custom data model to display content (that is, to bind the data source through Setmodel first), whereas Qtablewidget can only use the standard data model. and its cell data is Qtablewidgetitem object (that is, you do not need a data source, you can fill in cell information). This is mainly reflected in the Qtableview class with the Setmodel member function, and in the Qtablewidget class, the member function becomes private. The use of Qtablewidget is inseparable from Qtablewidgetitem. Qtablewidgetitem is used to represent a cell in a table, and a table needs to be built with cells.
[CPP]View PlainCopy
- #include <QtGui/QApplication>
- #include <QTableWidget>
- #include <QTableWidgetItem>
- int main (int argc, char *argv[])
- {
- Qapplication A (argc, argv);
- Qtablewidget *tablewidget = new Qtablewidget (10,5); //Constructs a Qtablewidget object and sets it to 10 rows, 5 columns
- ///The Qtablewidget object can also be constructed using the following method
- //Qtablewidget *tablewidget = new Qtablewidget;
- //Tablewidget->setrowcount (10); Set the number of rows to ten
- //Tablewidget->setcolumncount (5); Set number of columns to 5
- Tablewidget->setwindowtitle ("Qtablewidget & Item");
- Tablewidget->resize (350, 200); //Setup table
- Qstringlist header;
- header<< "Month" <<"Description";
- Tablewidget->sethorizontalheaderlabels (header);
- Tablewidget->setitem (0,0,new Qtablewidgetitem ("Jan"));
- Tablewidget->setitem (1,0,new Qtablewidgetitem ("Feb"));
- Tablewidget->setitem (2,0,new Qtablewidgetitem ("Mar"));
- Tablewidget->setitem (0,1,new Qtablewidgetitem (Qicon ("Images/ied.png"), "Jan ' s Month"));
- Tablewidget->setitem (Qtablewidgetitem,New-Qicon ("Images/ied.png"), "Feb ' s Month");
- Tablewidget->setitem (2,1,new Qtablewidgetitem (Qicon ("Images/ied.png"), "Mar ' s Month");
- Tablewidget->show ();
- return a.exec ();
- }
I. Realization of the effect of Qtablewidget itself
1. Change the table to prohibit editing
By default, the characters in the table can be changed, such as double-clicking a cell, you can modify the original content, if you want to prohibit the user's operation, so that the table is read-only to the user, you can:
Tablewidget->setedittriggers (qabstractitemview::noedittriggers);
Qabstractitemview.noedittriggers is one of the Qabstractitemview.edittrigger enumerations, which are the conditions that trigger the modification of the contents of a cell:
Qabstractitemview.noedittriggers |
0 |
No editing possible. Cannot modify the contents of a table |
Qabstractitemview.currentchanged |
1 |
Editing start whenever current item changes. Can modify cells at any time |
Qabstractitemview.doubleclicked |
2 |
Editing starts when a item is double clicked. Double-click the cell |
Qabstractitemview.selectedclicked |
4 |
Editing starts when clicking on an already selected item. Click Selected Content |
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. Press any key to modify |
Qabstractitemview.alledittriggers |
31 |
Editing starts for all above actions. The above conditions include |
2. Set table for whole row selection
Tablewidget->setselectionbehavior (qabstractitemview::selectrows); The way the whole row is selected
The Qabstractitemview.selectionbehavior enumeration also has the following types
Constant |
Value |
Description |
Qabstractitemview.selectitems |
0 |
Selecting single items. Select individual cells |
Qabstractitemview.selectrows |
1 |
Selecting only rows. Select a row |
Qabstractitemview.selectcolumns |
2 |
Selecting only columns. Select a column |
3. Single-checked and multiple-checked settings:
Tablewidget->setselectionmode (qabstractitemview::extendedselection); Set to multiple targets can be selected
The parameters of this function can also be:
Qabstractitemview.noselection cannot choose
Qabstractitemview.singleselection Select a single target
Qabstractitemview.multiselection Multiple targets selected
Qabstractitemview.extendedselection qabstractitemview.contiguousselection difference is not obvious, the main function is the normal case is a radio, but press CTRL or SHIFT key, can select multiple
4. Display and hide of table header
For the table header of a horizontal or vertical method, you can hide/show the settings in the following ways:
[CPP]View PlainCopy
- Tablewidget->verticalheader ()->setvisible (false); //Hide list header
- Tablewidget->horizontalheader ()->setvisible (false); //Hide row header
Note: #include <QHeaderView> required
5. Set the font and color of the header text
[CPP]View PlainCopy
- Qtablewidgetitem *columnheaderitem0 = tablewidget->horizontalheaderitem (0); //Get the item object of the horizontal direction table header
- Columnheaderitem0->setfont (Qfont ("Helvetica")); //Set font
- Columnheaderitem0->setbackgroundcolor (Qcolor (0,60,10)); //Set cell background color
- Columnheaderitem0->settextcolor (Qcolor (200,111,30)); //Set text color
Note: #include <QHeaderView> required
6. Add a control to the cell:
Qtablewidget not only allows text to be added to the cell, it also allows the control to be placed in the cell. For example, to add a drop-down box to a cell, you can do this:
[CPP]View PlainCopy
- Qcombobox *combox = new Qcombobox ();
- Combox->additem ("Y");
- Combox->additem ("N");
- Tablewidget->setcellwidget (0,2,combox);
Read Qcombobox information:
[HTML]View PlainCopy
- Qwidget * widget=ui->tablewidget->cellwidget (i,0);//Get widget
- Qcombobox *combox= (qcombobox*) widget;//forced conversion to Qcombobox
- QString string=combox->currenttext ();
- Qdebug ()<<string;
Two. Setting the cell
1. Set the font color and background color and font characters of the cell
[CPP]View PlainCopy
- Qtablewidgetitem *item = new Qtablewidgetitem ("Apple");
- Item->setbackgroundcolor (Qcolor (0,60,10));
- Item->settextcolor (Qcolor (200,111,100));
- Item->setfont (Qfont ("Helvetica"));
- Tablewidget->setitem (0,3,item);
Another: If you need to use this font for all cells, you can use Tablewidget->setfont (Qfont ("Helvetica"));
2. Set the alignment of text within a cell
This is relatively simple, using the newitem.settextalignment () function, the function's arguments are the alignment within the cell, and the character input order is from left to right or from right to left.
The horizontal alignment is as follows:
| Constant |
Value |
Description |
| Qt.alignleft |
0x0001 |
Aligns with the left edge. |
| Qt.alignright |
0x0002 |
Aligns with the right edge. |
| Qt.alignhcenter |
0x0004 |
Centers horizontally in the available space. |
| Qt.alignjustify |
0x0008 |
Justifies the text in the available space. |
Vertical alignment:
| Constant |
Value |
Description |
| Qt.aligntop |
0x0020 |
Aligns with the top. |
| Qt.alignbottom |
0x0040 |
Aligns with the bottom. |
| Qt.alignvcenter |
0x0080 |
Centers vertically in the available space. |
If both are set, just use qt.alignhcenter | Qt.alignvcenter the way you can
3. Implementation of merged cell effects:
Tablewidget->setspan (0, 0, 3, 1) # Its parameters are: 1 rows to change cell 2 number of rows 3 columns to merge
4. Set the size of the cell
First, you can specify the size of a row or column
[CPP]View PlainCopy
- Tablewidget->setcolumnwidth (3,200);
- Tablewidget->setrowheight (3,60);
You can also set the row and column size to match the content
[CPP]View PlainCopy
- Tablewidget->resizecolumnstocontents ();
- Tablewidget->resizerowstocontents ();
5. Get the contents of the clicked cell
By implementing the slot function of the itemclicked (Qtablewidgetitem *) signal, you can get the cell pointer that the mouse clicked on to get the text information
Connect (tablewidget,signal (itemdoubleclicked (Qtreewidgetitem*,int)), This,slot (GetItem (Qtreewidgetitem*,int)));
Binds the itemclicked signal to the function getitem
6.QTableWidget to adjust the table row width mainly involves one of the following functions
Resizecolumnstocontents (); Adjust column widths based on content
resizecolumntocontents (int col); Automatically adjusts a given column width based on content
Horizontalheader ()->setresizemode sets the given column to the given pattern
Main modes are stretch and fixed
7.
Int row = RowCount ();
removerow (row);//Clear existing rows
setshowgrid (TRUE);//Show Table Lines
verticalheader ()->setvisible (false);//Hide Left vertical
qheaderview *headerview = Horizontalheader ();
headerview->setmovable (false);//Remove the movement of the table header
headerview->resizesection (0,284);//Set the first column width
headerview->resizesection (1,127);//Set the second column width
headerview->setresizemode (qheaderview::fixed); /list cannot be moved
headerview->setclickable (false);//do not respond to mouse clicks
setedittriggers (qtablewidget:: noedittriggers);//cannot edit
setselectionbehavior (qtablewidget::selectrows);//Select one row at a time
Setselectionmode (qabstractitemview::singleselection);//Only Radio
/*qscrollbar *scrollbar = Horizontalscrollbar ();
scrollbar->hide (); */
sethorizontalscrollbarpolicy (Qt::scrollbaralwaysoff);//Remove horizontal scroll bar
setverticalscrollmode (Qabstractitemview::scrollperitem);//The vertical scrollbar moves by item
setautoscroll (FALSE);// Remove Auto-scroll
Qtablewidget Control Summary < two;