1. Set form styles
Click (here) to fold or open
- Table_widget-> setcolumncount (4); // you can specify the number of columns.
- Table_widget-> horizontalheader ()-> setdefasesectionsize (150 );
- Table_widget-> horizontalheader ()-> setclickable (false); // set the header to be unclickable (sorted by default)
-
- // Set the header content
- Qstringlist header;
- Header <tr ("name") <tr ("Last modify time") <tr ("type") <tr ("size ");
- Table_widget-> sethorizontalheaderlabels (header );
-
- // Set the header font to bold
- Qfont font = This-> horizontalheader ()-> font ();
- Font. setbold (true );
- Table_widget-> horizontalheader ()-> setfont (font );
-
- Table_widget-> horizontalheader ()-> setstretchlastsection (true); // you can specify the full table width.
- Table_widget-> verticalheader ()-> setresizemode (qheaderview: resizetocontents );
- Table_widget-> verticalheader ()-> setdefasesectionsize (10); // set the line spacing
- Table_widget-> setframeshape (qframe: noframe); // set no border
- Table_widget-> setshowgrid (false); // set not to display grid lines
- Table_widget-> verticalheader ()-> setvisible (false); // sets the vertical header to be invisible.
- Table_widget-> setselectionmode (q1_actitemview: extendedselection); // You can select Multiple widgets (CTRL, shift, CTRL +)
- Table_widget-> setselectionbehavior (q1_actitemview: selectrows); // You can select a row at a time.
- Table_widget-> setedittriggers (q1_actitemview: noedittriggers); // The settings cannot be edited.
- Table_widget-> horizontalheader ()-> resizesection (0,150); // set the width of the first column of the header to 150
- Table_widget-> horizontalheader ()-> setfixedheight (25); // you can specify the height of the header.
- Table_widget-> setstylesheet ("selection-Background-color: lightblue;"); // you can specify the selected background color.
- Table_widget-> horizontalheader ()-> setstylesheet ("qheaderview: Section {Background: skyblue;}"); // set the header background color
-
- // Set the horizontal and 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 ;}");
Now, the style setting is complete, and the effect is as follows:
Question 1: A dotted box appears when you click the option. on the qt official website, find a blog dedicated to introducing the option and directly add the code!
(1) implement the following class:
Click (here) to fold or open
- # Include "no_focus_delegate.h"
- Void nofocusdelegate: paint (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: paint (painter, itemoption, index );
- }
(2) Add the following code to the table structure:
Click (here) to fold or open
- Table_widget-> setitemdelegate (New nofocusdelegate ());
OK, remove the dotted border
Problem 2: When the table has only one row, the table header will collapse.
It took a long time to solve the problem:
Click (here) to fold or open
- // When you click a table, the header is not highlighted (obtain the focus)
- Table_widget-> horizontalheader ()-> sethighlightsections (false );
Ii. Operation forms (adding and deleting rows)
(1) dynamically Insert rows
Click (here) to fold or open
- Int row_count = table_widget-> rowcount (); // gets the number of rows in the form.
- Table_widget-> insertrow (row_count); // Insert a 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, and file size
- Item-> seticon (icon); // icon indicates the icon of the calling system, which can be differentiated by suffix.
- Item-> settext (name );
- Item1-> settext (last_modify_time );
- Item2-> settext (type); // type indicates the type of the calling system, which can be differentiated by future fix.
- 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 the style to gray
- Qcolor color ("gray ");
- Item1-> settextcolor (color );
- Item2-> settextcolor (color );
- Item3-> settextcolor (color );
(2) Insert rows at the specified position
In fact, it is similar to (1). (1) The premise is that the number of rows in the table is obtained.
Click (here) to fold or open
- Table_widget-> insertrow (ROW); // Insert a new row as the inserted position
3. Click the event triggered by the header
(1) signal and slot connecting the header
Click (here) to fold or open
- Connect (horizontalheader (), signal (sectionclicked (INT), this, slot (onheaderclicked (INT )));
(2) Implementation of Slot Functions
Click (here) to fold or open
- Void tablewidget: onheaderclicked (INT column)
- {
- // Column is the column of the clicked header.
- }
4. Open a line and edit it
Since the simulation of Windows is like a little imitation, Windows can modify the name, so QT can certainly implement
Click (here) to fold or open
- // Get the current node and edit the name
- Qtablewidgetitem * Item = table_widget-> item (edit_row, 0); // edit_row indicates the row number to be edited.
- Table_widget-> setcurrentcell (edit_row, 0 );
- Table_widget-> openpersistenteditor (item); // open the edit item
- Table_widget-> edititem (item );
-
- // Close the edit item
- Table_widget-> closepersistenteditor (item );
OK. The rename is complete ,!
5. Right-click the menu
(1) Create menus and menu items
Click (here) to fold or open
- 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 ("RENAME "));
- 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 ("Modify date "));
- // Set the shortcut key
- Action_flush-> setshortcut (qkeysequence: refresh );
-
- // Set the folder icon
- Action_create_folder-> seticon (icon );
- Qobject: connect (action_create_folder, signal (triggered (), this, slot (createfolder ()));
- }
(2) Implement contextmenuevent again
Click (here) to fold or open
- Void tablewidget: contextmenuevent (qcontextmenuevent * event)
- {
- Pop_menu-> clear (); // clear the original menu
- Qpoint point = event-> pos (); // obtain the window Coordinate
- 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 menu appears at the current mouse position
- Pop_menu-> exec (qcursor: pos ());
- Event-> Accept ();
- }
- }
OK, all done!
Vi. 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 previusrow, int previuscolumn)
At this point, there is another problem. How do I display the file (folder) icons and types on the interface? How can we tell you what kind of icons are displayed? If the icon and type are written to death, it is not feasible, because there are at least 100 types of files with different suffixes! You can call the system API to obtain the type and icon of the specified file (by suffix) of the current system...
These are some of the small experiences that have been summarized since I came into contact with QT. I hope they will be useful to you!
Detailed description of qtablewidget (style, right-click menu, collapsed header, multiple choice, etc.) 10:54:04