QT Set qtreewidget a column can be edited

Source: Internet
Author: User

As we all know, in Qt, Qtreewidget are organized in the form of qtreewidgetitem, which means that if you set properties individually, you can make a row editable or not editable .

Qtreewidgetitem*->setflags (qt::itemisenabled | qt::itemisselectable | qt::itemiseditable);//Set the contents of a row to be editable

Here are some ways to make a column editable only.

Qtreewidget::openpersistenteditor (Qtreewidgetitem * item, int column = 0) can be used to open the editing state of a column of a row

Qtreewidget::closepersistenteditor (Qtreewidgetitem * item, int column = 0) can be used to close the Always edit state of a column in a row

When the mouse double-clicks a section of the tree control, the tree control emits a message itemdoubleclicked (Qtreewidgetitem * Item, int column), and if we capture the message and judge the value of column, you can makes one column editable, while the other columns are not editable . However, since Openpersistenteditor will make this cell always editable , the cell is not selected by the mouse in time, so we also need to do this when the user finishes editing the current cell. Calling Closepersistenteditor to close a cell is always editable and can be processed in the signal processing function of the itemselectionchanged signal (this signal is also emitted if the value is not modified).


Key Code sections:

In the constructor:

Connect (ui->feederset_treewidget,signal (itemdoubleclicked (qtreewidgetitem*,int)),
            This,slot (Openeditor (Qtreewidgetitem*,int)));
Connect (ui->feederset_treewidget,signal (itemselectionchanged)),
            This,slot (Closeeditor ()));
Ui->feederset_treewidget->setstylesheet ("qtreewidget::item{height:20px}");//set leading size to prevent changes in line spacing after compilation modification is complete

/* in two slot processing functions */
Set columns other than the first and fifth columns, double-click to modify
void Xdlconfig::openeditor (Qtreewidgetitem *item, int column)
{
    if (column! = 0 && Column! = 4)
    {
        Ui->feederset_treewidget->openpersistenteditor (item,column);//Set an item to edit
        Temitem = Item;
        temcolumn = column;
    }
}
Turn off the editable state when you are finished modifying or selecting a different column
void Xdlconfig::closeeditor ()
{
    Qmessagebox::question (This, "Modify", QString ("%1"). Arg (Temcolumn), qmessagebox::yes,qmessagebox::no);
    if (temitem! = NULL)
    {
        Ui->feederset_treewidget->closepersistenteditor (temitem,temcolumn);//Setting an item is not editable
    }
}

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.