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
}
}