To learn qtablewidget, you should first look at the Qtableview control (the control also has "pedigree"!) As a researcher), because Qtablewidget inherits from class Qtableview.
The main difference is that Qtableview can use a custom data model to display content (which means that the data source is bound by Setmodel first), whereas Qtablewidget can only use the standard data model.
Qtablewidget cell data is implemented by Qtablewidgetitem objects (that is, you do not need a data source, and the information in the cell needs to be populated individually).
This is mainly due to the Setmodel member function in the Qtableview class, and to the Qtablewidget class, the member function becomes private.
The use of Qtablewidget is inseparable from Qtablewidgetitem. Qtablewidgetitem is used to represent one of the cells in a table, and the entire table needs to be built with cell-by-object Qtablewidgetitem.
"2" Qtablewidget control properties
(1) Prohibit editing of forms
By default, the characters in the table can be changed.
For example, 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:
1 ui.qtablewidget->setedittriggers (qabstractitemview::noedittriggers);
(2) Set table to select Entire row
(3) Set single check and multiple selected
A single check means that only one cell can be selected at a time, and more than one can select a "piece" pattern.
1/* Settings allow multiple check * /2 Ui.qtablewidget->setselectionmode (qabstractitemview::extendedselection);
(4) Display and hiding of table header
For a horizontal or vertical header, if you do not want to display the settings you can do (hide/show) in the following ways:
1 Ui.qtablewidget->verticalheader ()->setvisible (true); 2 Ui.qtablewidget->horizontalheader ()->setvisible (false);
(5) Setting the alignment of the font in a specific cell
1 ui.qtablewidget->item (0, 0)->settextalignment (qt::alignhcenter);
(6) Setting the font format for specific cells
1 Ui.qtablewidget->item (1, 0)->setbackgroundcolor (Qcolor (0,60,10));
(7) Set the value of the specific cell
1 Ui.qtablewidget->setitem (1, 0, new Qtablewidgetitem (str));
(8) Convert Qtablewidgetitem object content to Qstring
1 QString str =ui.qtablewidget->item (0, 0)->data (Qt::D isplayrole). toString ();
(9) Adding controls to specific cells
1 Qcombobox *combox = new Qcombobox (); 2 Combox->additem ("F"); 3 Combox->additem ("M"); 4 Ui.qtablewidget->setcellwidget (0,3,combox);
(11) Merging cells
1//Merge cell effects 2 Ui.qtablewidget->setspan (2, 2, 3, 2); 3//First parameter: Number of rows to change 4//second parameter: Number of cells to change 5//Third parameter: Number of rows to be merged 6// Fourth parameter: Number of columns to merge
(12) Insert a picture in a specific cell
Ui.qtablewidget->setitem (3, 2, New Qtablewidgetitem (Qicon ("Images/music.png"), "Music"));
(13) Setting the display grid
Ui.qtablewidget->setshowgrid (TRUE);//Show Table lines
(14) Set scroll bar
Ui.qtablewidget->sethorizontalscrollbarpolicy (Qt::scrollbaralwaysoff);//Remove horizontal scroll bar
(15) Setting column labels
1 //initialization Interface 2 qstringlist hstrlist; 3 hstrlist.push_back (QString ("name")); 4 Hstrlist.push_back (QString ("id")); 5 Hstrlist.push_back (QString ("Age")), 6 hstrlist.push_back (QString ("Sex")), 7 Hstrlist.push_back ( QString ("department")); 8 9 //Set the column number (only if the column exists, you can set the label) int hlablecnt = Hstrlist.count (); ui.qtablewidget-> Setrowcount ( ui.qtablewidget->setcolumncount) (hlablecnt), + //Set column label Ui.qtablewidget->sethorizontalheaderlabels (hstrlist);
(16) Set the row and column size to match the content
1 ui.qtablewidget->resizecolumnstocontents (); 2 ui.qtablewidget->resizerowstocontents ();
(17) Setting the font
Ui.qtablewidget->setfont (font); Set font
(18) Select the entire row (remove the dashed line when selected)
Set proxy delegate based on Qstyleditemdelegate
Make it a bit
void Cmystyleddelegate::p aint (qpainter *painter, const qstyleoptionviewitem &option, const qmodelindex &index) Const { qstyleoptionviewitem view_option (option); if (View_option.state & qstyle::state_hasfocus) { view_option.state = view_option.state ^ Qstyle::state_ Hasfocus; } Qstyleditemdelegate::p aint (painter, view_option, index);}
Set delegate
Cmystyleddelegate ());
Single-Choice multiple selection
Pstablewigt->setselectionmode (qabstractitemview::singleselection); Select only one row extendedselection multiple lines at a time
Second, multiple select and get the selected row
This->setselectionmode (qabstractitemview::extendedselection); Set multiple selection (you can ctral+a Select all Ctral+shift) to get the selected line number:
BOOL Tablewidget::getselectedrow (Qset&set_row)
{
Qlist items = This->selecteditems ();
int item_count = Items.Count ();
if (item_count <= 0)
{
return false;
}
for (int i=0; i
{
Gets the selected row
int item_row = This->row (items.at (i));
Set_row.insert (Item_row);
}
return true;
}
three , Action form (add, delete row, etc.)
(1) Dynamically inserting rows
int row_count = Table_widget->rowcount (); Get the number of table lines
Table_widget->insertrow (Row_count); Insert New row
Qtablewidgetitem *item = new Qtablewidgetitem ();
Qtablewidgetitem *item1 = new Qtablewidgetitem ();
Qtablewidgetitem *item2 = new Qtablewidgetitem ();
Qtablewidgetitem *item3 = new Qtablewidgetitem ();
Set the corresponding icon, file name, last update time, corresponding type, file size
Item->seticon (icon); icon for calling system icons, prefix to differentiate
Item->settext (name);
Item1->settext (Last_modify_time);
Item2->settext (type); The type is called to the system, and the prefix to differentiate
Item3->settext (size);
Table_widget->setitem (row_count, 0, item);
Table_widget->setitem (Row_count, 1, item1);
Table_widget->setitem (Row_count, 2, item2);
Table_widget->setitem (Row_count, 3, ITEM3);
Set style to Gray
Qcolor Color ("gray");
Item1->settextcolor (color);
Item2->settextcolor (color);
Item3->settextcolor (color);
(2) Inserting rows at a specified location
In fact, (1) similar, (1) The premise is to get to the table row number
Table_widget->insertrow (row); Insert a new row row for the inserted position
Four , click event triggered by the table header
(1) Signal and slot for connecting the table head
Connect (Horizontalheader (), SIGNAL (sectionclicked (int)), this, SLOT (onheaderclicked (int)));
(2) Implement slot function
void tablewidget::onheaderclicked (int column)
{
Column is one of the columns of the table header that you clicked
}
Six, right-click menu
(1) Creating menus, menu items
void Tablewidget::createactions ()
{
Create a menu item
Pop_menu = new Qmenu ();
Action_name = new Qaction (this);
Action_size = new Qaction (this);
Action_type = new Qaction (this);
Action_date = new Qaction (this);
Action_open = new Qaction (this);
Action_download = new Qaction (this);
Action_flush = new Qaction (this);
Action_delete = new Qaction (this);
Action_rename = new Qaction (this);
Action_create_folder = new Qaction (this);
Action_open->settext (QString ("open"));
Action_download->settext (QString ("Download"));
Action_flush->settext (QString ("refresh"));
Action_delete->settext (QString ("delete"));
Action_rename->settext (QString ("renaming"));
Action_create_folder->settext (QString ("New Folder"));
Action_name->settext (QString ("name"));
Action_size->settext (QString ("size"));
Action_type->settext (QString ("Project Type"));
Action_date->settext (QString ("date Modified"));
Set shortcut keys
Action_flush->setshortcut (Qkeysequence::refresh);
Set folder icon
Action_create_folder->seticon (icon);
Qobject::connect (Action_create_folder, SIGNAL (triggered ()), this, SLOT (CreateFolder ()));
}
(2) Re-implementation of Contextmenuevent
void Tablewidget::contextmenuevent (Qcontextmenuevent *event)
{
Pop_menu->clear (); Clear the original menu
Qpoint point = Event->pos (); Get window coordinates
Qtablewidgetitem *item = This->itemat (point);
if (item! = NULL)
{
Pop_menu->addaction (Action_download);
Pop_menu->addaction (Action_flush);
Pop_menu->addseparator ();
Pop_menu->addaction (Action_delete);
Pop_menu->addaction (Action_rename);
Pop_menu->addseparator ();
Pop_menu->addaction (Action_create_folder);
Sort_style = Pop_menu->addmenu ("sort");
Sort_style->addaction (Action_name);
Sort_style->addaction (action_size);
Sort_style->addaction (Action_type);
Sort_style->addaction (action_date);
The position of the menu appears as the current mouse position
Pop_menu->exec (qcursor::p OS ());
Event->accept ();
}
}
Seven, Signal
void cellactivated (int row, int column)
void cellchanged (int row, int column)
void cellclicked (int row, int column)
void celldoubleclicked (int row, int column)
void cellentered (int row, int column)
void cellpressed (int row, int column)
void itemactivated (Qtablewidgetitem *item)
void ItemChanged (Qtablewidgetitem *item)
void itemclicked (Qtablewidgetitem *item)
void itemdoubleclicked (Qtablewidgetitem *item)
void itementered (Qtablewidgetitem *item)
void itempressed (Qtablewidgetitem *item)
void Itemselectionchanged ()
void Currentitemchanged (Qtablewidgetitem *current, Qtablewidgetitem *previous)
void currentcellchanged (int currentrow, int currentcolumn, int previousrow, int previouscolumn)
How do I get the file (clip) icon and type of the interface? For a file, there are at least 100 files of different extensions, and if the icon and type are fixed, it will not work, so here are the two ways to get it.
(19) Get the contents of a cell
1 QString strText = ui.qtablewidget->item (0, 0)->text ();
The Qtablewidget of QT