The use of QT under Qtablewidget

Source: Internet
Author: User
Tags rowcount

The main content of this article:
    1. Basic settings for Qtablewidget
    2. Set some fields of Qtablewidget not editable and background color
    3. Qtablewidget inserting cells into a cell
    4. Qtablewidget Sort By field value
    5. Insert a checkbox in cell Qtablewidget
    6. Demo Code
1, the basic settings of Qtablewidget
Ui -Tablewidget -Setcolumncount ( One); UI -Tablewidget -Setrowcount (Numofreg);/ * Set Tablewidget * /Ui -Tablewidget -Sethorizontalheaderlabels (Qstringlist ()<< "Id" << "Name" << "Unit" << "Value" << "Min" << "Max"  \                                               << "Type" << "Mode" << "Enable" << "last read" << "Last Write"); UI -Tablewidget -Verticalheader () -SetVisible (false);//Hide Horizontal headerUi -Tablewidget -Setselectionbehavior (Qabstractitemview:: Selectitems);//Single selectedUi -Tablewidget -Setselectionmode (Qabstractitemview:: Extendedselection);//You can select multiple


2. Set some fields of Qtablewidget not editable and background color

Set Tablewidget Some fields can be edited, some fields can not be edited by the method:

void Qtablewidget::setitem (int row, int column, Qtablewidgetitem * Item)

Sets the item for the given row and column to item.


Take a look at what types of Qt::itemflags are:

When you see the above functions, you know that you can set the item and then set the properties of the cell.

Each cell has its own item and cannot be shared.

Set the background color of the Qtablewidget:
You can set the color of the Qtablewidgetitem:

Of course, you can also use the following function, but this is not recommended for use in Qt5.4 's documentation (deprecated):

/ * Setting Id the value of the Name field cannot be modified * // * Set non-modifiable ID name two column background color is gray * /for (int i= 0; I<Numofreg; I++) {Qtablewidgetitem*Item= NewQtablewidgetitem (); Item -SetBackground (Qbrush (Qcolor (Qt:: Lightgray))); Item -SetFlags (item -Flags ()&(~QT:: Itemiseditable)); Ui -Tablewidget -SetItem (I,0, item);} for (int i= 0; I<Numofreg; I++) {Qtablewidgetitem*Item= NewQtablewidgetitem (); Item -SetBackground (Qbrush (Qcolor (Qt:: Lightgray))); Item -SetFlags (item -Flags ()&(~QT:: Itemiseditable)); Ui -Tablewidget -SetItem (I,1, item);}


3, Qtablewidget cell insertion

In the following code, Reginfo is an array of pointers, creates objects of 5 Registerinfo classes, and populates the default fields. These field values are then inserted into the corresponding cells.

Reginfo[0] = newRegisterinfo(0x00,QString("Target Motor"),255,0,QString("U8"),QString("RW"), false);Reginfo[1] = newRegisterinfo(0x01,QString("Flags"),U32_max,0,QString("U32"),QString("R"), false);Reginfo[2] = newRegisterinfo(0x02,QString("Status"),U8_max,0,QString("U8"),QString("R"), false);Reginfo[3] = newRegisterinfo(0x03,QString("Control mode"),U8_max,0,QString("U8"),QString("RW"), false);Reginfo[4] = newRegisterinfo(0x04,QString("Speed Reference"),4000, -4000,QString("S32"),QString("R"), false); for(int i =0; I < Ui->tablewidget->rowcount (); i++) {Ui->tablewidget->item (I,0)->settext (QString:: Number (Reginfo[i]->getid (),Ten)); Ui->tablewidget->item (I,1)->settext (Reginfo[i]->getname ());} for(int i =0; I < Ui->tablewidget->rowcount (); i++) {Qtablewidgetitem*item_max = newQtablewidgetitem(); Item_max->settext (QString:: Number (Reginfo[i]->getmax ())); Item_max->setbackground (Qbrush(Qcolor(Qt:: Lightgray)); Item_max->setflags (Item_max->flags () & (~Qt::itemiseditable)); Ui->tablewidget->setitem (I,5, Item_max);Qtablewidgetitem*item_min = newQtablewidgetitem(); Item_min->settext (QString:: Number (Reginfo[i]->getmin ())); Item_min->setbackground (Qbrush(Qcolor(Qt:: Lightgray)); Item_min->setflags (Item_min->flags () & (~Qt::itemiseditable)); Ui->tablewidget->setitem (I,4, item_min);Qtablewidgetitem*item_type = newQtablewidgetitem();    Item_type->settext (Reginfo[i]->gettype ()); Item_type->setbackground (Qbrush(Qcolor(Qt:: Lightgray)); Item_type->setflags (Item_type->flags () & (~Qt::itemiseditable)); Ui->tablewidget->setitem (I,6, Item_type);Qtablewidgetitem*item_mode = newQtablewidgetitem();    Item_mode->settext (Reginfo[i]->getmode ()); Item_mode->setbackground (Qbrush(Qcolor(Qt:: Lightgray)); Item_mode->setflags (Item_mode->flags () & (~Qt::itemiseditable)); Ui->tablewidget->setitem (I,7, Item_mode);}


4. Qtablewidget sorted by field value

The following implementation clicks the header of each column, implementing the column alphabetically ascending or ordered.
The Horizontalheader () function is Qtablewidget inherited from Qtableview, which returns a pointer to a qheaderview type.

And Qheaderview has a sectionclicked such a signal:

After clearing up such a relationship, you can associate the signal with the sort slot function that you wrote:

connect(ui->tableWidget->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(mySortByColumn(int)));

Notice that Qtablewidget has such a sort function sortbycolumn:

Where Qt::sortorder is an enumeration type, it has two values in ascending and descending order:

Write your own slot function Mysortbycolumn:

void MainWindow::mySortByColumn(int column){    =true;    ui->tableWidget->? Qt::AscendingOrder : Qt::DescendingOrder);    =!f;}

This allows you to click on the header of a column so that the column is sorted in ascending (descending) order, and then clicked once, so that the column is sorted in descending order (ascending).

The name field is sorted in ascending order:

The Max field is sorted by order:

5. Insert a checkbox in the Qtablewidget cell
for0; i < ui->tableWidget->rowCount(); i++) {    QTableWidgetItemQTableWidgetItem();    Qt::CheckedQt::Unchecked);    8, item_flag);}


6. Code

Slightly

Reference: Qt5.4 Help documentation
Finish

The use of QT under Qtablewidget

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.