Qtablewidget control summary

Source: Internet
Author: User

[1] qtablewidget Overview

Qtablewidget is a common control used to display data tables in the QT dialog box design.

To learn qtablewidget, first look at the qtableview control (the control also has a "family! Like researchers), because qtablewidget inherits from class qtableview.

The main difference between the two is that qtableview can use a custom data model to display the content (that is, you must bind the data source through setmodel first), while qtablewidget can only use the standard data model.

The qtablewidget cell data is implemented by the qtablewidgetitem object (that is, the data source is not required, and the information in the cells must be filled one by one ).

This is mainly because the qtableview class has the setmodel member function. In the qtablewidget class, this member function becomes private.

Qtablewidget is inseparable from qtablewidgetitem. Qtablewidgetitem is used to represent one of the cells in the table. The entire table must be constructed with the cell-by-Cell Object qtablewidgetitem.

[2] qtablewidget control attributes

(1) prohibit table editing

By default, the characters in the table can be changed.

For example, you can double-click a cell to modify the original content. If you want to disable this operation, make the table read-only to the user, you can:

1 ui.qtablewidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

(2) set the table to select the entire row.

1/* select the table as the whole row */2 UI. qtablewidget-> setselectionbehavior (q1_actitemview: selectrows );

(3) set a single selection and multiple selections

A single selection means that only one cell can be selected at a time. Multiple cells are equivalent to the "One Cell" mode.

1/* allow multiple selections */2 UI. qtablewidget-> setselectionmode (q1_actitemview: extendedselection );

(4) display and hide table Headers

For horizontal or vertical headers, you can set (hide/display) as follows if you do not want to display them:

1 ui.qtablewidget->verticalHeader()->setVisible(true);  2 ui.qtablewidget->horizontalHeader()->setVisible(false);

(5) set the font alignment of a specific cell.

1 ui.qtablewidget->item(0, 0)->setTextAlignment(Qt::AlignHCenter);

(6) set the font format in 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("Helvetica")); 

(7) set the value of a specific cell

1 ui.qtablewidget->setItem(1, 0, new QTableWidgetItem(str));

(8) convert the content of the qtablewidgetitem object to qstring

1 QString str =ui.qtablewidget->item(0, 0)->data(Qt::DisplayRole).toString();

(9) add controls to cells

1 QComboBox *comBox = new QComboBox(); 2 comBox->addItem("F"); 3 comBox->addItem("M"); 4 ui.qtablewidget->setCellWidget(0,3,comBox);

(11) Merge Cells

1 // Effect of merging Cells 2 UI. qtablewidget-> setspan (2, 2, 3, 2); 3 // The first parameter: number of cells to be changed; 4 // The second parameter: number of cell columns to be changed 5 // third parameter: number of rows to be merged 6 // fourth parameter: Number of columns to be merged

(12) insert an image into a specific cell

ui.qtablewidget->setItem(3, 2, new QTableWidgetItem(QIcon("images/music.png"), "Music"));

(13) set the display grid

Ui. qtablewidget-> setshowgrid (true); // display table lines

(14) set the scroll bar

Ui. qtablewidget-> sethorizontalscrollbarpolicy (QT: scrollbaralwaysoff); // remove the horizontal scroll bar

(15) set column labels

1 // initialization interface 2 qstringlist hstrlist; 3 hstrlist. push_back (qstring ("name"); 4 hstrlist. push_back (qstring ("ID"); 5 hstrlist. push_back (qstring ("Age"); 6 hstrlist. push_back (qstring ("sex"); 7 hstrlist. push_back (qstring ("Department"); 8 9 10 // set the number of rows and columns (the column label can be set only when the Column exists) 11 int hlablecnt = hstrlist. count (); 12 UI. qtablewidget-> setrowcount (10); 13 UI. qtablewidget-> setcolumncount (hlablecnt); 14 15 // set the column label 16 UI. qtablewidget-> sethorizontalheaderlabels (hstrlist );

(16) set the size of rows and columns to match the content

1 ui.qtablewidget->resizeColumnsToContents();  2 ui.qtablewidget->resizeRowsToContents();  

(17) set the font

Ui. qtablewidget-> setfont (font); // set the font

(18) obtain the content of a cell

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

 

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.