Hi,

I'm working on a model to list some user defined data. For this I'm using the QTreeWidget class with 4 columns. The first column is a checkbox indicating whether the item is activated or not. In the second and third column I've inserted QComboBox Widgets using the setItemWidget(...) function to display possible options (The reason why i have chosen QTreeWidget and not QTableWidget). The last column should show a string which will be initialized empty.

However as the first three columns are very eye-catching due to their widgets, the last column with the empty string could be ignored by the user. Therefore I've implemented an UpdateWidgetState() function to check whether the last column is empty and if that's the case I set the font to italic, the foregroundcolor to grey and set the text to "enter function here". The UpdateWidgetState() function is called in the initiliazation routine and everytime when the QTreeWidget emits the itemChanged signal. This routine clears the tree widget and refills the view with the actual data.

The code looks something like this:

Qt Code:
  1. int num_items = GetNumberOfItems....
  2. treeWidget->clear();
  3. QList<QTreeWidgetItem *> items;
  4. int i;
  5. for (i = 0; i < num_items ; i++)
  6. {
  7. QString FunctionValue;
  8.  
  9. //get data for FunctionValue....
  10.  
  11. list << "" << "" << "" << FunctionValue;
  12.  
  13. disconnect(treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
  14. this, SLOT(treeWidgetItemChanged(QTreeWidgetItem*,int)));
  15.  
  16. QTreeWidgetItem * treeWidgetItem = new QTreeWidgetItem(treeWidget, list);
  17. treeWidgetItem->setFlags(treeWidget->flags()|Qt::ItemIsEditable);
  18. treeWidgetItem->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator);
  19.  
  20. QFont item_font = treeWidget->font(3);
  21. QBrush item_brush = treeWidget->foreground(3);
  22. if (FunctionValue.isEmpty())
  23. {
  24. item_font.setItalic(TRUE);
  25. item_brush.setColor(Qt::gray);
  26. treeWidget->setText(3, "enter function here ...");
  27. }
  28. else
  29. {
  30. item_font.setItalic(FALSE);
  31. item_brush.setColor(Qt::black);
  32. }
  33.  
  34. treeWidget->setFont(3, item_font);
  35. treeWidget->setForeground(3, item_brush);
  36.  
  37. connect(treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
  38. this, SLOT(treeWidgetItemChanged(QTreeWidgetItem*,int)));
  39.  
  40. //data for first, second and third column.....
  41.  
  42. items.append(treeWidgetItem);
  43. }
  44. treeWidget->insertTopLevelItems(0, items);
To copy to clipboard, switch view to plain text mode 

The problem now is, that I want to show an empty text when the last column is edited (if the edit begins I don't want to have the "enter function here ..." string selected). For this I'm catching the itemActivated(...) signal and setting the text to an empty string manually:

Qt Code:
  1. void myDialog::treeWidgetItemActivated(QTreeWidgetItem * item, int column)
  2. {
  3. if (column == 3)
  4. {
  5. QString FunctionValue = get function value ....
  6. if (FunctionValue.isEmpty())
  7. {
  8. disconnect(treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
  9. this, SLOT(treeWidgetItemChanged(QTreeWidgetItem*,int)));
  10.  
  11. item->setText(3,"");
  12. connect(treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
  13. this, SLOT(treeWidgetItemChanged(QTreeWidgetItem*,int)));
  14. }
  15.  
  16. }
  17. }
To copy to clipboard, switch view to plain text mode 

However this signal is not triggered when I set the QTreeWidgetItem flag to Qt::ItemIsEditable and double click on it. However the signal is entered if i press the enter key (in which case the dialog is also closed). When I don't set the flag, then everything works well and the signal is catched. However I can't edit the text in this case. I've tried to do a workaround using openPersistentEditor and closePersistentEditor but that doesn't work very well.
Is this behaviour normal? If yes, is there a possibility to edit the text in the tree widget starting with a blank field (without using an extra Widget or subclassing the QTreeView model).

Thank you for you help!