Results 1 to 8 of 8

Thread: QTreeWidgetItem questions

  1. #1
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default QTreeWidgetItem questions

    I need a way for certain items in a QTreeWidget to have only one editable column. A double click on any of the other columns on that item should not show the edit-widget. How can I accomplish this? I suppose I could intercept the mouse-event, but how do I know which column was clicked? Or is there an even easier way that I overlooked?

    And is it possible for certain columns to show a QDateTimeEdit widget as their edit widget instead of a QLineEdit?

    Thanks!
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidgetItem questions

    Quote Originally Posted by Michiel View Post
    I need a way for certain items in a QTreeWidget to have only one editable column. A double click on any of the other columns on that item should not show the edit-widget. How can I accomplish this?
    The problem is that with model-view you can specify flags per index but for tree widget items you can specify flags only as per whole row. One way could be using a custom delegate to prevent editing in certain columns.

    Quote Originally Posted by Michiel View Post
    And is it possible for certain columns to show a QDateTimeEdit widget as their edit widget instead of a QLineEdit?
    Set the data as QDateTime, not as QString and you'll get corresponding editor out of the box:
    Qt Code:
    1. QTreeWidgetItem* item = ...
    2. // item->setText(0, "2007-05-14"); // <-- not like this
    3. item->setData(0, Qt::DisplayRole, dateTime) // <-- but like this (dateTime is a QDateTime)
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTreeWidgetItem questions

    Hm. It's actually a subclass of QTreeWidgetItem. I don't really understand these roles yet, and I've been using Qt::EditRole and Qt::UserRole for editing (through the editor and through the rest of the program respectively). But I reimplemented setData() to pass the request on to QTreeWidgetItem::setData() in case of other roles.

    I did as you said and used Qt::DisplayRole instead of Qt::UserRole when setting the value from the code. But it still uses a line-edit. (Plus it causes a few other bugs with displaying the data, but that's just because I'm not storing it anymore.)

    Could you explain more clearly please?
    Last edited by Michiel; 14th May 2007 at 14:50.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidgetItem questions

    Maybe the QTreeWidgetItem subclass mixes up the role or something? Works fine for me:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char* argv[])
    4. {
    5. QApplication a(argc, argv);
    6. QTreeWidget treeWidget;
    7. treeWidget.setColumnCount(2);
    8. QTreeWidgetItem* item = new QTreeWidgetItem(&treeWidget);
    9. item->setFlags(item->flags() | Qt::ItemIsEditable);
    10. // notice the difference while editing these two columns
    11. item->setText(0, "Text");
    12. item->setData(1, Qt::DisplayRole, QDateTime::currentDateTime());
    13. treeWidget.show();
    14. return a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Apr 2007
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidgetItem questions

    I have had good success simply not specifying Qt::ItemIsEditable in the item->setFlags() call. By providing Qt::ItemIsEnabled | Qt::ItemIsSelectable only, you effectively disable editing on a cell by cell basis.

  6. #6
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTreeWidgetItem questions

    Quote Originally Posted by jcooperddtd View Post
    I have had good success simply not specifying Qt::ItemIsEditable in the item->setFlags() call. By providing Qt::ItemIsEnabled | Qt::ItemIsSelectable only, you effectively disable editing on a cell by cell basis.
    But 'item' is the whole row. If you do not specify Qt::itemIsEditable, none of the cells in the row is editable. Maybe you're thinking about tables?

    By the way: I'm now trying a whole new approach. Using QTreeView and a real model instead of QTreeWidget. I think it will work.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidgetItem questions

    Quote Originally Posted by Michiel View Post
    By the way: I'm now trying a whole new approach. Using QTreeView and a real model instead of QTreeWidget. I think it will work.
    I don't see any reason why wouldn't it work. The model-view approach is way more flexible.

    Anyway, just for completeness, here's the solution for a tree widget:
    Qt Code:
    1. class MyItemDelegate : public QItemDelegate
    2. {
    3. public:
    4. MyItemDelegate(QObject* parent = 0) : QItemDelegate(parent) {}
    5.  
    6. QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    7. {
    8. // allow only specific column to be edited, second column in this example
    9. if (index.column() == 1)
    10. return QItemDelegate::createEditor(parent, option, index);
    11. return 0;
    12. }
    13. };
    14.  
    15. // usage:
    16. // treeWidget->setItemDelegate(new MyItemDelegate(treeWidget));
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 1st February 2009 at 20:47. Reason: spelling error
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    Michiel (15th May 2007)

  9. #8
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTreeWidgetItem questions

    Cool! Looks like that would work. I'm still going to use a real model now, but here's a "thanks" for your trouble.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

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.