Qtablewidget Control Summary < two >

Source: Internet
Author: User

Qtablewidget is a common space for displaying data tables in QT programs, similar to the DataGrid in VC and C #. Speaking of Qtablewidget, we must tell the difference between it and Qtabelview. Qtablewidget is a subclass of Qtableview, the main difference is that Qtableview can use a custom data model to display content (that is, to bind the data source through Setmodel first), whereas Qtablewidget can only use the standard data model. and its cell data is Qtablewidgetitem object (that is, you do not need a data source, you can fill in cell information). This is mainly reflected in the Qtableview class with the Setmodel member function, and in the Qtablewidget class, the member function becomes private. The use of Qtablewidget is inseparable from Qtablewidgetitem. Qtablewidgetitem is used to represent a cell in a table, and a table needs to be built with cells.

[CPP]View PlainCopy
  1. #include <QtGui/QApplication>
  2. #include <QTableWidget>
  3. #include <QTableWidgetItem>
  4. int main (int argc, char *argv[])
  5. {
  6. Qapplication A (argc, argv);
  7. Qtablewidget *tablewidget = new Qtablewidget (10,5); //Constructs a Qtablewidget object and sets it to 10 rows, 5 columns
  8. ///The Qtablewidget object can also be constructed using the following method
  9. //Qtablewidget *tablewidget = new Qtablewidget;
  10. //Tablewidget->setrowcount (10); Set the number of rows to ten
  11. //Tablewidget->setcolumncount (5); Set number of columns to 5
  12. Tablewidget->setwindowtitle ("Qtablewidget & Item");
  13. Tablewidget->resize (350, 200); //Setup table
  14. Qstringlist header;
  15. header<< "Month" <<"Description";
  16. Tablewidget->sethorizontalheaderlabels (header);
  17. Tablewidget->setitem (0,0,new Qtablewidgetitem ("Jan"));
  18. Tablewidget->setitem (1,0,new Qtablewidgetitem ("Feb"));
  19. Tablewidget->setitem (2,0,new Qtablewidgetitem ("Mar"));
  20. Tablewidget->setitem (0,1,new Qtablewidgetitem (Qicon ("Images/ied.png"), "Jan ' s Month"));
  21. Tablewidget->setitem (Qtablewidgetitem,New-Qicon ("Images/ied.png"), "Feb ' s Month");
  22. Tablewidget->setitem (2,1,new Qtablewidgetitem (Qicon ("Images/ied.png"), "Mar ' s Month");
  23. Tablewidget->show ();
  24. return a.exec ();
  25. }


I. Realization of the effect of Qtablewidget itself

1. Change the table to prohibit editing

By default, the characters in the table can be changed, such as 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:

Tablewidget->setedittriggers (qabstractitemview::noedittriggers);

Qabstractitemview.noedittriggers is one of the Qabstractitemview.edittrigger enumerations, which are the conditions that trigger the modification of the contents of a cell:

Qabstractitemview.noedittriggers

0

No editing possible. Cannot modify the contents of a table

Qabstractitemview.currentchanged

1

Editing start whenever current item changes. Can modify cells at any time

Qabstractitemview.doubleclicked

2

Editing starts when a item is double clicked. Double-click the cell

Qabstractitemview.selectedclicked

4

Editing starts when clicking on an already selected item. Click Selected Content

Qabstractitemview.editkeypressed

8

Editing starts when the platform edit key had been pressed over an item.

Qabstractitemview.anykeypressed

16

Editing starts when the any key was pressed over an item. Press any key to modify

Qabstractitemview.alledittriggers

31

Editing starts for all above actions. The above conditions include

2. Set table for whole row selection

Tablewidget->setselectionbehavior (qabstractitemview::selectrows); The way the whole row is selected

The Qabstractitemview.selectionbehavior enumeration also has the following types

Constant

Value

Description

Qabstractitemview.selectitems

0

Selecting single items. Select individual cells

Qabstractitemview.selectrows

1

Selecting only rows. Select a row

Qabstractitemview.selectcolumns

2

Selecting only columns. Select a column

3. Single-checked and multiple-checked settings:

Tablewidget->setselectionmode (qabstractitemview::extendedselection); Set to multiple targets can be selected

The parameters of this function can also be:

Qabstractitemview.noselection cannot choose

Qabstractitemview.singleselection Select a single target

Qabstractitemview.multiselection Multiple targets selected

Qabstractitemview.extendedselection qabstractitemview.contiguousselection difference is not obvious, the main function is the normal case is a radio, but press CTRL or SHIFT key, can select multiple

4. Display and hide of table header

For the table header of a horizontal or vertical method, you can hide/show the settings in the following ways:

[CPP]View PlainCopy
    1. Tablewidget->verticalheader ()->setvisible (false); //Hide list header
    2. Tablewidget->horizontalheader ()->setvisible (false); //Hide row header

Note: #include <QHeaderView> required

5. Set the font and color of the header text

[CPP]View PlainCopy
    1. Qtablewidgetitem *columnheaderitem0 = tablewidget->horizontalheaderitem (0); //Get the item object of the horizontal direction table header
    2. Columnheaderitem0->setfont (Qfont ("Helvetica")); //Set font
    3. Columnheaderitem0->setbackgroundcolor (Qcolor (0,60,10)); //Set cell background color
    4. Columnheaderitem0->settextcolor (Qcolor (200,111,30)); //Set text color

Note: #include <QHeaderView> required

6. Add a control to the cell:

Qtablewidget not only allows text to be added to the cell, it also allows the control to be placed in the cell. For example, to add a drop-down box to a cell, you can do this:

[CPP]View PlainCopy
    1. Qcombobox *combox = new Qcombobox ();
    2. Combox->additem ("Y");
    3. Combox->additem ("N");
    4. Tablewidget->setcellwidget (0,2,combox);

Read Qcombobox information:

[HTML]View PlainCopy
    1. Qwidget * widget=ui->tablewidget->cellwidget (i,0);//Get widget
    2. Qcombobox *combox= (qcombobox*) widget;//forced conversion to Qcombobox
    3. QString string=combox->currenttext ();
    4. Qdebug ()<<string;



Two. Setting the cell

1. Set the font color and background color and font characters of the cell

[CPP]View PlainCopy
    1. Qtablewidgetitem *item = new Qtablewidgetitem ("Apple");
    2. Item->setbackgroundcolor (Qcolor (0,60,10));
    3. Item->settextcolor (Qcolor (200,111,100));
    4. Item->setfont (Qfont ("Helvetica"));
    5. Tablewidget->setitem (0,3,item);

Another: If you need to use this font for all cells, you can use Tablewidget->setfont (Qfont ("Helvetica"));

2. Set the alignment of text within a cell

This is relatively simple, using the newitem.settextalignment () function, the function's arguments are the alignment within the cell, and the character input order is from left to right or from right to left.

The horizontal alignment is as follows:

Constant Value Description
Qt.alignleft 0x0001 Aligns with the left edge.
Qt.alignright 0x0002 Aligns with the right edge.
Qt.alignhcenter 0x0004 Centers horizontally in the available space.
Qt.alignjustify 0x0008 Justifies the text in the available space.

Vertical alignment:

Constant Value Description
Qt.aligntop 0x0020 Aligns with the top.
Qt.alignbottom 0x0040 Aligns with the bottom.
Qt.alignvcenter 0x0080 Centers vertically in the available space.

If both are set, just use qt.alignhcenter | Qt.alignvcenter the way you can

3. Implementation of merged cell effects:

Tablewidget->setspan (0, 0, 3, 1) # Its parameters are: 1 rows to change cell 2 number of rows 3 columns to merge

4. Set the size of the cell

First, you can specify the size of a row or column

[CPP]View PlainCopy
    1. Tablewidget->setcolumnwidth (3,200);
    2. Tablewidget->setrowheight (3,60);

You can also set the row and column size to match the content

[CPP]View PlainCopy
    1. Tablewidget->resizecolumnstocontents ();
    2. Tablewidget->resizerowstocontents ();


5. Get the contents of the clicked cell

By implementing the slot function of the itemclicked (Qtablewidgetitem *) signal, you can get the cell pointer that the mouse clicked on to get the text information

Connect (tablewidget,signal (itemdoubleclicked (Qtreewidgetitem*,int)), This,slot (GetItem (Qtreewidgetitem*,int)));

Binds the itemclicked signal to the function getitem


6.QTableWidget to adjust the table row width mainly involves one of the following functions

Resizecolumnstocontents (); Adjust column widths based on content
resizecolumntocontents (int col); Automatically adjusts a given column width based on content
Horizontalheader ()->setresizemode sets the given column to the given pattern
Main modes are stretch and fixed

7.

Int row = RowCount ();
 removerow (row);//Clear existing rows
 setshowgrid (TRUE);//Show Table Lines
 verticalheader ()->setvisible (false);//Hide Left vertical
 qheaderview *headerview = Horizontalheader ();
 headerview->setmovable (false);//Remove the movement of the table header
 headerview->resizesection (0,284);//Set the first column width
 headerview->resizesection (1,127);//Set the second column width
 headerview->setresizemode (qheaderview::fixed); /list cannot be moved
 headerview->setclickable (false);//do not respond to mouse clicks
 setedittriggers (qtablewidget:: noedittriggers);//cannot edit
 setselectionbehavior (qtablewidget::selectrows);//Select one row at a time
  Setselectionmode (qabstractitemview::singleselection);//Only Radio
 /*qscrollbar *scrollbar = Horizontalscrollbar ();
 scrollbar->hide (); */
 sethorizontalscrollbarpolicy (Qt::scrollbaralwaysoff);//Remove horizontal scroll bar
 setverticalscrollmode (Qabstractitemview::scrollperitem);//The vertical scrollbar moves by item
 setautoscroll (FALSE);// Remove Auto-scroll

Qtablewidget Control Summary < two;

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.