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 contents
qstringlist Header;
header<<tr ("name") <<tr ("Last Modifytime") <<tr ("type") <<tr ("size");
Table_widget->sethorizontalheaderlabelS (header);
//Set table 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 display grid lines
Table_widget->verticalheader ()->setvisible (false);//Set vertical head not visible
Table_widget->setselectionmode (qabstractitemview::extendedselection);You can select multiple (Ctrl, Shift,CTRL + a All can)
Table_widget->setselectionbehavior (qabstractitemview::selectrows);Set selection behavior to select one row at a time
Table_widget->setedittriggers (qabstractitemview::noedittriggers);//Settings not 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 table 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 style
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:
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 () );
OK, dashed border removal
Problem two: When the table has only one row, the header will collapse.
groping for a long time to be resolved:
//when clicking on a table does not light the header row (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
{
/ /Get 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 ();//Gets the number of table lines
Table_widget->insertrow (row_count);//Insert New Line
Qtablewidgetitem *item = Newqtablewidgetitem ();
Qtablewidgetitem *item1 = Newqtablewidgetitem ();
Qtablewidgetitem *item2 = Newqtablewidgetitem ();
Qtablewidgetitem *item3 = Newqtablewidgetitem ();
//Set the corresponding icon, file name, last update time, corresponding type, file size
Item->seticon (icon); //icon is the icon that calls the system, and the prefix to differentiate
Item->settext (name);
Item1->settext (last_modify_time);
item2->settext (type);//type is the type of the calling 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 (Intcolumn)
{
//column a column for the header of the clicked Table
}
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
//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 );
OK, rename done ,!
Six, Right-click menu
(1) Creating menus, menu items
voidtablewidget::createactions ()
{
/ /Create 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= newqaction (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
voidtablewidget::contextmenuevent (qcontextmenuevent *event)
{
pop_menu->clear ( ); //Clear 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!
Seven, signal
void cellactivated (int row, intcolumn)
void cellchanged (int row, int column)
void cellclicked (int row, int column)
void celldoubleclicked (int row, intcolumn)
void cellentered (int row, int column)
void cellpressed (int row, int column)
voiditemactivated (qtablewidgetitem *item)
void itemchanged (Qtablewidgetitem *item)
void itemclicked (Qtablewidgetitem *item)
void itemdoubleclicked (Qtablewidgetitem*item)
void itementered (Qtablewidgetitem *item)
void itempressed (Qtablewidgetitem *item)
void itemselectionchanged ()
voidcurrentitemchanged (Qtablewidgetitem *current, qtablewidgetitem*previous)
void currentcellchanged (intcurrentrow, int currentcolumn, int previousrow, intpreviouscolumn)
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 (Get the file icon, type).
- Qt Qfileiconprovider (Get the 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.)