In the days of using QT not much, already two times used to qtablewidget this control, also slowly habit and like on it. When you use Qtablewidget again, it's not as confusing as when you first started using it. Uh, uh. Now come to summarize my experience with qtablewidget ... (*^__^*) hehe ...
Use also looked at a lot of information, in this thank the predecessors of the intentions to summarize and share!
1.QTableWidget cannot change with the size of the main window in MainWindow?
Workaround: Add a layout outside the table.
Code: Tablewidget = new Qtablewidget;
Tablewidget->setobjectname (Qstring::fromutf8 ("Tablewidget"));
Qvboxlayout *verticallayout;
Verticallayout->addwidget (Tablewidget);
2. Change the table to prohibit editing:
Tablewidget->setedittriggers (qabstractitemview::noedittriggers);
(parameter meaning: qabstractitemview.noedittriggers--cannot modify the contents of the table
qabstractitemview.currentchanged--can modify cells at any time
qabstractitemview.doubleclicked--Double-click a cell
qabstractitemview.selectedclicked--Click the selected content
qabstractitemview.editkeypressed--
qabstractitemview.anykeypressed--can be modified by pressing any key
qabstractitemview.alledittriggers--the above conditions are all included)
3. Set table for whole row selection
Tablewidget->setselectionbehavior (qabstractitemview::selectrows); The way the whole row is selected
(Parameter meaning: abstractitemview.selectitems--Select a single cell
qabstractitemview.selectrows--Select a row
qabstractitemview.selectcolumns--Select a column)
4. Single-checked and multiple-checked settings:
Tablewidget->setselectionmode (qabstractitemview::extendedselection); Set to multiple targets can be selected
(parameter meaning: qabstractitemview.noselection--cannot select
qabstractitemview.singleselection--Select a single target
qabstractitemview.multiselection--Multiple targets selected
Qabstractitemview.extendedselection/qabstractitemview.contiguousselection The difference is not obvious, the main function is the normal case is the radio, but press CTRL or SHIFT key, Multiple options available)
5. 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:
Tablewidget->verticalheader ()->setvisible (false); Hide List Header
Tablewidget->horizontalheader ()->setvisible (false); Hide a row table header
Note: #include <QHeaderView> required
6. Set the font and color of the header text
Qtablewidgetitem *columnheaderitem0 = tablewidget->horizontalheaderitem (0); Gets the item object for the horizontal heading of the 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
7. Add a control to the cell:
Qcombobox *combox = new Qcombobox ();
Combox->additem ("Y");
Combox->additem ("N");
Tablewidget->setcellwidget (0,2,combox);
8. Add a picture to the cell:
Tablewidget->setitem (row, 0, New Qtablewidgetitem (Qicon (":/new/images/kingdemo.ico"), tr ("")));
9 Set the cell font color, background color, and font character:
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"));
10. Set the alignment of text within a cell
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
11. Merge Cells:
Tablewidget->setspan (0, 0, 3, 1) # Its parameters are: To change the cell 1 rows, 2 columns, the number of 3 rows to be merged, 4 columns
12. Set the size of the cell
First, you can specify the size of a row or column
Tablewidget->setcolumnwidth (3,200);
Tablewidget->setrowheight (3,60);
You can also set the row and column size to match the content
Tablewidget->resizecolumnstocontents ();
Tablewidget->resizerowstocontents ();
13. 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
14.QTableWidget to adjust the table row width mainly involves the following functions
Tablewidget->horizontalheader ()->setresizemode (qheaderview::stretch);//Make the column fully populated and split equally
Tablewidget->verticalheader ()->setresizemode (qheaderview::stretch);//Line Adaptive width
Tablewidget->resizecolumnstocontents (); Adjust column widths based on content
tablewidget->resizecolumntocontents (int col);//automatically adjusts the given column width based on content
Tablewidget->horizontalheader ()->setresizemode//sets the given column to the given pattern
Main modes are stretch and fixed
15. Add Table Header content:
Method One:
Qstringlist header;
header<< "" <<tr ("1") <<tr ("2") <<tr ("3") <<tr ("4") <<tr ("5");
Method Two:
Tablewidget->sethorizontalheaderlabels (Qstringlist () << tr ("1") <<tr ("2") <<tr ("3") <<tr ("4) <<tr (" 5 "));
16. Clear:
Tablewidget->clear ();//Clears all visible data (including the header), and the row is still
Tablewidget->clearcontents ();//Clears only the data in the table and does not clear the header contents
Tablewidget->setrowcount (0);//Lianxing also clear off
15. Some bits and pieces of Knowledge Point code:
int row = Tablewidget->rowcount ();//Gets the current Total row count in the table
Tablewidget->setrowcount (row+1);//Add a row
Tablewidget->removerow (row);//clear the existing ranks
Int row1 = Tablewidget->currentitem ()->row ();//Current selected row
BOOL focus = tablewidget->isitemselected (Tablewidget->currentitem ());//Determines whether a row is selected
QString proname = Tablewidget->item (row, col)->text ();//Get a lattice of content
Setshowgrid (TRUE);//Show Table lines
Verticalheader ()->setvisible (false);//Hide Left Vertical
Qheaderview *headerview = Horizontalheader ();
Headerview->setmovable (false);//Remove the movement of the table head
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 single selection
/*qscrollbar *scrollbar = Horizontalscrollbar ();
Scrollbar->hide (); */
Sethorizontalscrollbarpolicy (Qt::scrollbaralwaysoff);//Remove horizontal scroll bar
Setverticalscrollmode (Qabstractitemview::scrollperitem);//The vertical scroll bar moves by item
Setautoscroll (false);//Remove Auto-scrolling
17. Sort:
Tablewidget->sortbycolumn (0, Qt::ascendingorder);//As the name implies, the function means to arrange a column in ascending/descending order
Yes, yes! Think of and use only so many temporarily, and then fill ... (Refer to some of the seniors, don't mind Oh, (*^__^*))
http://blog.csdn.net/mingxia_sui/article/details/7681863
Summary of usage of Qtablewidget