In the development process of QT, often use the form (qtablewidget) This control, the data on the net many, but is the most basic, some more frequently encountered problem also said not very clear. So, here's a summary today!
The following is a single form of personal simulation of Windows Explorer
First, set the form style
Table_widget->setcolumncount (4); Set Number of columns
Table_widget->horizontalheader ()->setdefaultsectionsize (150);
Table_widget->horizontalheader ()->setclickable (false); Set header is not clickable (sort by default click)
Set Header content
Qstringlist header;
HEADER<<TR ("name") <<tr ("Last Modify Time") <<tr ("type") <<tr ("size");
Table_widget->sethorizontalheaderlabels (header);
Set Header font Bold
Qfont font = This->horizontalheader ()->font ();
Font.setbold (TRUE);
Table_widget->horizontalheader ()->setfont (font);
Table_widget->horizontalheader ()->setstretchlastsection (true); Set Full table width
Table_widget->verticalheader ()->setresizemode (qheaderview::resizetocontents);
Table_widget->verticalheader ()->setdefaultsectionsize (10); Set row height
Table_widget->setframeshape (Qframe::noframe); Set no border
Table_widget->setshowgrid (FALSE); Set not to show grid lines
Table_widget->verticalheader ()->setvisible (false); Set vertical head not visible
Table_widget->setselectionmode (qabstractitemview::extendedselection); Multiple options (Ctrl, Shift, CTRL + A are all available)
Table_widget->setselectionbehavior (qabstractitemview::selectrows); Set selection behavior to select one row at a time
Table_widget->setedittriggers (qabstractitemview::noedittriggers); Set non-editable
Table_widget->horizontalheader ()->resizesection (0,150); Sets the width of the first column of the header to 150
Table_widget->horizontalheader ()->setfixedheight (25); Set the height of the header
Table_widget->setstylesheet ("selection-background-color:lightblue;"); Set Selected background color
Table_widget->horizontalheader ()->setstylesheet ("Qheaderview::section{background:skyblue;}"); Set Header background color
Set horizontal, vertical scroll bar Styles
Table_widget->horizontalscrollbar ()->setstylesheet ("qscrollbar{background:transparent; height:10px;} "
"Qscrollbar::handle{background:lightgray; border:2px solid Transparent; border-radius:5px;} "
"Qscrollbar::handle:hover{background:gray;}"
"Qscrollbar::sub-line{background:transparent;}"
"Qscrollbar::add-line{background:transparent;}");
Table_widget->verticalscrollbar ()->setstylesheet ("qscrollbar{background:transparent; width:10px;} "
"Qscrollbar::handle{background:lightgray; border:2px solid Transparent; border-radius:5px;} "
"Qscrollbar::handle:hover{background:gray;}"
"Qscrollbar::sub-line{background:transparent;}"
"Qscrollbar::add-line{background:transparent;}");
OK, the styling is complete and the effect is as follows:
Qtablewidget details (style, right-click menu, Header collapse, multiple selection, etc.)
Problem one: mouse click on the option will appear virtual box, in the QT official website to find a blog specifically introduced, directly on the code!
(1) Implement one of the following classes
#include "no_focus_delegate.h"
void Nofocusdelegate::p aint (qpainter* painter, const qstyleoptionviewitem & option, const Qmodelindex &index) Const
{
Qstyleoptionviewitem itemoption (option);
if (Itemoption.state & Qstyle::state_hasfocus)
{
Itemoption.state = itemoption.state ^ qstyle::state_hasfocus;
}
Qstyleditemdelegate::p aint (painter, itemoption, index);
}
(2) Add the following code to the table construction
Table_widget->setitemdelegate (New Nofocusdelegate ());
Qtablewidget details (style, right-click menu, Header collapse, multiple selection, etc.)
OK, dashed border removal
Problem two: When the table has only one row, the header will collapse.
Qtablewidget details (style, right-click menu, Header collapse, multiple selection, etc.)
Groping for a long time to be resolved:
Do not light the header row when clicking on the table (get focus)
Table_widget->horizontalheader ()->sethighlightsections (false);
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;
}
Third, the operation of the form (add, delete rows, 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
Iv. Click the 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
}
V. Open a line for editing
Now that the simulation window mimics the image, Windows can modify the name, then QT will be able to implement
Qtablewidget details (style, right-click menu, Header collapse, multiple selection, etc.)
Get the current node and get the edit name
Qtablewidgetitem *item = Table_widget->item (edit_row, 0); Edit_row for the line number you want to edit
Table_widget->setcurrentcell (edit_row, 0);
Table_widget->openpersistenteditor (item); Open an Edit item
Table_widget->edititem (item);
Close Edit Item
Table_widget->closepersistenteditor (item);
Qtablewidget details (style, right-click menu, Header collapse, multiple selection, etc.)
OK, rename done,!
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 ();
}
}
OK, you are done!
Qtablewidget details (style, right-click menu, Header collapse, multiple selection, etc.)
Seven, signal
void cellactivated (int row, int column)
void cellchanged (int row, int column)
void cellclicked (int r ow, int column)
void celldoubleclicked (int row, int column)
void cellentered (int row, int column)
void Cellpre ssed (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.
Qt qfileiconprovider (Gets the file icon, type).
Qt qfileiconprovider (Get file icon, type).
For more information about Qtableview, please refer to:
QT model/view (update data in real time).
The Qtableview of Qt.
These are in contact with Qt since the summary of some of the small experience, hope to be useful to everyone! Does not accumulate Kuibu not even thousands of miles, does not accumulate the small flow not to become the river ...
Note:
Technology lies in communication, communication, reproduced please specify the source and maintain the integrity of the work.
╰☆ struggle ing? Child ' original: http://blog.sina.com.cn/s/blog_a6fb6cc90101dd5u.html.
Qtablewidget details (style, right-click menu, Header collapse, multiple selection, etc.)