Qtablewidget Control Summary

Source: Internet
Author: User

Qtablewidget Control Summary

Introduction to "1" qtablewidget

Qtablewidget is a common control for displaying data tables in QT dialog design.

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

1/ Set table for entire row selected /
2 Ui.qtablewidget->setselectionbehavior (qabstractitemview::selectrows);
(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/ set allows 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) to set the font format for a specific cell

1  Ui.qtablewidget->item  (1 ,  0 ) ->  setbackgroundcolor (Qcolor (0 , 60 , 10 ); 2  ui.qtablewidget->item  (1 , 0 )   SetTextColor (Qcolor (200 , 111 , 100 )); 3  ui.qtablewidget->item  (1 , 0 )   SetFont (Qfont ()); 

(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//Effect of merging cells
2 Ui.qtablewidget->setspan (2, 2, 3, 2);
3//First parameter: Number of cell rows to change
4//second parameter: Number of cell columns to change
5//Third parameter: Number of rows to merge
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     //Initialize interface 2Qstringlist hstrlist;3Hstrlist.Push_back (QString ("Name"));4Hstrlist.Push_back (QString ("id"));5Hstrlist.Push_back (QString ("Age"));6Hstrlist.Push_back (QString ("Sex"));7Hstrlist.Push_back (QString ("department"));8      9 Ten     //sets the number of rows (column labels can be set only if the column exists) Oneint hlablecnt=Hstrlist.Count (); AUi.Qtablewidget -Setrowcount (Ten); -Ui.Qtablewidget -Setcolumncount (HLABLECNT); -  the     //Set column labels -Ui.Qtablewidget -Sethorizontalheaderlabels (hstrlist); -Ui.Qtablewidget -SetItem (0,0,NewQtablewidgetitem ("haha"));//Set string -Ui.Qtablewidget -SetItem (0,0,NewQtablewidgetitem (Qicon ("Images/ied.png"),"Jan ' s Month"));//Cell settings image and string

Copy Code
(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) Get the contents of a cell

1 QString strText = ui.qtablewidget->item(0, 0)->text();

Qtablewidget Control Summary

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.