Results 1 to 3 of 3

Thread: Setting QTreeWidget flag to Qt::ItemIsEditable seems to disable itemActivated SIGNAL

  1. #1
    Join Date
    Sep 2007
    Location
    Vienna, Austria
    Posts
    19
    Thanks
    10
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Setting QTreeWidget flag to Qt::ItemIsEditable seems to disable itemActivated SIGNAL

    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!

  2. #2
    Join Date
    Sep 2007
    Location
    Vienna, Austria
    Posts
    19
    Thanks
    10
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: Setting QTreeWidget flag to Qt::ItemIsEditable seems to disable itemActivated SIG

    I've summarized the problem in a small demonstration: If I set the ItemIsEditable flag then the itemActivated signal is not triggered on double clicking. Else it works fine.

    Qt Code:
    1. Window::Window()
    2. {
    3. treeWidget = new QTreeWidget();
    4.  
    5. list << "";
    6.  
    7. QTreeWidgetItem * treeWidgetItem = new QTreeWidgetItem(treeWidget, list);
    8.  
    9. //setting this flag disables itemActivated signal for double clicking
    10. treeWidgetItem->setFlags(treeWidgetItem->flags()|Qt::ItemIsEditable);
    11.  
    12. treeWidget->insertTopLevelItem(0, treeWidgetItem);
    13.  
    14. connect(treeWidget, SIGNAL(itemActivated(QTreeWidgetItem*, int)),
    15. this, SLOT(treeWidgetItemActivated(QTreeWidgetItem*, int)));
    16.  
    17. QGridLayout * layout = new QGridLayout;
    18. layout->addWidget(treeWidget);
    19.  
    20. setLayout(layout);
    21. }
    22.  
    23. void Window::treeWidgetItemActivated(QTreeWidgetItem * item, int column)
    24. {
    25. QMessageBox::information(this, "", "itemActivated triggered");
    26. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  3. #3
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Setting QTreeWidget flag to Qt::ItemIsEditable seems to disable itemActivated SIG

    As I read the source code, that is the expected behavior: link

    Two possible solutions:
    1. Use QTreeWidget::itemDoubleClicked (QTreeWidgetItem * item, int column) which has the same signature.
    2. Subclass QTreeWidget and emit the signal yourself:
      Qt Code:
      1. void MyTreeWidget::mouseDoubleClickEvent(QMouseEvent *event){
      2. emit activated(indexAt(event->pos()));
      3. QTreeWidget::mouseDoubleClickEvent(event);
      4. }
      To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to norobro for this useful post:

    y.shan (23rd November 2011)

Similar Threads

  1. Replies: 2
    Last Post: 20th October 2010, 16:16
  2. QTreeWidget disable children
    By hlvietlong in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2009, 01:34
  3. Replies: 0
    Last Post: 20th May 2009, 10:01
  4. QTreeWidget Single Column Sort Disable
    By craigjameshamilton in forum Newbie
    Replies: 1
    Last Post: 21st April 2008, 08:08
  5. setting QTreeWidget vertical labels
    By hyling in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2007, 18:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.