Detailed description of qtablewidget (style, right-click menu, collapsed header, multiple choice, etc.) 10:54:04

Source: Internet
Author: User

1. Set form styles

Click (here) to fold or open

  1. Table_widget-> setcolumncount (4); // you can specify the number of columns.
  2. Table_widget-> horizontalheader ()-> setdefasesectionsize (150 );
  3. Table_widget-> horizontalheader ()-> setclickable (false); // set the header to be unclickable (sorted by default)
  4.  
  5. // Set the header content
  6. Qstringlist header;
  7. Header <tr ("name") <tr ("Last modify time") <tr ("type") <tr ("size ");
  8. Table_widget-> sethorizontalheaderlabels (header );
  9.  
  10. // Set the header font to bold
  11. Qfont font = This-> horizontalheader ()-> font ();
  12. Font. setbold (true );
  13. Table_widget-> horizontalheader ()-> setfont (font );
  14.  
  15. Table_widget-> horizontalheader ()-> setstretchlastsection (true); // you can specify the full table width.
  16. Table_widget-> verticalheader ()-> setresizemode (qheaderview: resizetocontents );
  17. Table_widget-> verticalheader ()-> setdefasesectionsize (10); // set the line spacing
  18. Table_widget-> setframeshape (qframe: noframe); // set no border
  19. Table_widget-> setshowgrid (false); // set not to display grid lines
  20. Table_widget-> verticalheader ()-> setvisible (false); // sets the vertical header to be invisible.
  21. Table_widget-> setselectionmode (q1_actitemview: extendedselection); // You can select Multiple widgets (CTRL, shift, CTRL +)
  22. Table_widget-> setselectionbehavior (q1_actitemview: selectrows); // You can select a row at a time.
  23. Table_widget-> setedittriggers (q1_actitemview: noedittriggers); // The settings cannot be edited.
  24. Table_widget-> horizontalheader ()-> resizesection (0,150); // set the width of the first column of the header to 150
  25. Table_widget-> horizontalheader ()-> setfixedheight (25); // you can specify the height of the header.
  26. Table_widget-> setstylesheet ("selection-Background-color: lightblue;"); // you can specify the selected background color.
  27. Table_widget-> horizontalheader ()-> setstylesheet ("qheaderview: Section {Background: skyblue;}"); // set the header background color
  28.  
  29. // Set the horizontal and vertical scroll bar styles
  30. Table_widget-> horizontalscrollbar ()-> setstylesheet ("qscrollbar {Background: transparent; Height: 10px ;}"
  31. "Qscrollbar: handle {Background: lightgray; Border: 2px solid transparent; border-radius: 5px ;}"
  32. "Qscrollbar: handle: hover {Background: Gray ;}"
  33. "Qscrollbar: sub-line {Background: transparent ;}"
  34. "Qscrollbar: Add-line {Background: transparent ;}");
  35. Table_widget-> verticalscrollbar ()-> setstylesheet ("qscrollbar {Background: transparent; width: 10px ;}"
  36. "Qscrollbar: handle {Background: lightgray; Border: 2px solid transparent; border-radius: 5px ;}"
  37. "Qscrollbar: handle: hover {Background: Gray ;}"
  38. "Qscrollbar: sub-line {Background: transparent ;}"
  39. "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

  1. # Include "no_focus_delegate.h"
  2. Void nofocusdelegate: paint (qpainter * painter, const qstyleoptionviewitem & option, const qmodelindex & Index) const
  3. {
  4. Qstyleoptionviewitem itemoption (option );
  5. If (itemoption. State & qstyle: state_hasfocus)
  6. {
  7. Itemoption. State = itemoption. State ^ qstyle: state_hasfocus;
  8. }
  9. Qstyleditemdelegate: paint (painter, itemoption, index );
  10. }
(2) Add the following code to the table structure:

Click (here) to fold or open

  1. 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

  1. // When you click a table, the header is not highlighted (obtain the focus)
  2. Table_widget-> horizontalheader ()-> sethighlightsections (false );
 
  Ii. Operation forms (adding and deleting rows)
(1) dynamically Insert rows

Click (here) to fold or open

  1. Int row_count = table_widget-> rowcount (); // gets the number of rows in the form.
  2. Table_widget-> insertrow (row_count); // Insert a new row
  3. Qtablewidgetitem * Item = new qtablewidgetitem ();
  4. Qtablewidgetitem * Item1 = new qtablewidgetitem ();
  5. Qtablewidgetitem * item2 = new qtablewidgetitem ();
  6. Qtablewidgetitem * item3 = new qtablewidgetitem ();
  7. // Set the corresponding icon, file name, last update time, corresponding type, and file size
  8. Item-> seticon (icon); // icon indicates the icon of the calling system, which can be differentiated by suffix.
  9. Item-> settext (name );
  10. Item1-> settext (last_modify_time );
  11. Item2-> settext (type); // type indicates the type of the calling system, which can be differentiated by future fix.
  12. Item3-> settext (size );
  13. Table_widget-> setitem (row_count, 0, item );
  14. Table_widget-> setitem (row_count, 1, Item1 );
  15. Table_widget-> setitem (row_count, 2, item2 );
  16. Table_widget-> setitem (row_count, 3, item3 );
  17. // Set the style to gray
  18. Qcolor color ("gray ");
  19. Item1-> settextcolor (color );
  20. Item2-> settextcolor (color );
  21. 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

  1. 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

  1. Connect (horizontalheader (), signal (sectionclicked (INT), this, slot (onheaderclicked (INT )));
(2) Implementation of Slot Functions

Click (here) to fold or open

  1. Void tablewidget: onheaderclicked (INT column)
  2. {
  3. // Column is the column of the clicked header.
  4. }
 
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

  1. // Get the current node and edit the name
  2. Qtablewidgetitem * Item = table_widget-> item (edit_row, 0); // edit_row indicates the row number to be edited.
  3. Table_widget-> setcurrentcell (edit_row, 0 );
  4. Table_widget-> openpersistenteditor (item); // open the edit item
  5. Table_widget-> edititem (item );
  6.  
  7. // Close the edit item
  8. 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

  1. Void tablewidget: createactions ()
  2. {
  3. // Create a menu item
  4. Pop_menu = new qmenu ();
  5. Action_name = new qaction (this );
  6. Action_size = new qaction (this );
  7. Action_type = new qaction (this );
  8. Action_date = new qaction (this );
  9. Action_open = new qaction (this );
  10. Action_download = new qaction (this );
  11. Action_flush = new qaction (this );
  12. Action_delete = new qaction (this );
  13. Action_rename = new qaction (this );
  14. Action_create_folder = new qaction (this );
  15.  
  16. Action_open-> settext (qstring ("open "));
  17. Action_download-> settext (qstring ("Download "));
  18. Action_flush-> settext (qstring ("refresh "));
  19. Action_delete-> settext (qstring ("delete "));
  20. Action_rename-> settext (qstring ("RENAME "));
  21. Action_create_folder-> settext (qstring ("new folder "));
  22. Action_name-> settext (qstring ("name "));
  23. Action_size-> settext (qstring ("size "));
  24. Action_type-> settext (qstring ("project type "));
  25. Action_date-> settext (qstring ("Modify date "));
  26. // Set the shortcut key
  27. Action_flush-> setshortcut (qkeysequence: refresh );
  28.  
  29. // Set the folder icon
  30. Action_create_folder-> seticon (icon );
  31. Qobject: connect (action_create_folder, signal (triggered (), this, slot (createfolder ()));
  32. }

(2) Implement contextmenuevent again

Click (here) to fold or open

  1. Void tablewidget: contextmenuevent (qcontextmenuevent * event)
  2. {
  3. Pop_menu-> clear (); // clear the original menu
  4. Qpoint point = event-> pos (); // obtain the window Coordinate
  5. Qtablewidgetitem * Item = This-> itemat (point );
  6. If (item! = NULL)
  7. {
  8. Pop_menu-> addaction (action_download );
  9. Pop_menu-> addaction (action_flush );
  10. Pop_menu-> addseparator ();
  11. Pop_menu-> addaction (action_delete );
  12. Pop_menu-> addaction (action_rename );
  13. Pop_menu-> addseparator ();
  14. Pop_menu-> addaction (action_create_folder );
  15. Sort_style = pop_menu-> addmenu ("sort ");
  16. Sort_style-> addaction (action_name );
  17. Sort_style-> addaction (action_size );
  18. Sort_style-> addaction (action_type );
  19. Sort_style-> addaction (action_date );
  20. // The menu appears at the current mouse position
  21. Pop_menu-> exec (qcursor: pos ());
  22. Event-> Accept ();
  23. }
  24. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.