Results 1 to 17 of 17

Thread: QTreeView, Delegates and Persistent Widgets

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView, Delegates and Persistent Widgets

    Quote Originally Posted by barnabyr View Post
    Hi ...

    So, if we could go back to combo boxes for a second, how many clicks on a view item does your program need to open a combo box ? One or more .. ?

    Thanks.
    You can do it with one click. I implemented my own method which uses a custom delegate and re-implements the editorEvent(). The following code is for a check box, but it wouldn't be much diferent for any other type of widget.

    Qt Code:
    1. // Trap the mouse button in editorEvent and check/uncheck the checkbox by calling setData()
    2. // which is a subclassed member of the model this delegate is attached to.
    3. bool CheckBoxDelegate::editorEvent ( QEvent * event,
    4. const QStyleOptionViewItem & /* option */,
    5. const QModelIndex & index )
    6. {
    7. if (event->type() == QEvent::MouseButtonRelease)
    8. {
    9. bool checkState = index.model()->data(index, Qt::CheckStateRole).toBool();
    10. model->setData(index, !checkState);
    11. }
    12. return true; // Event has been handled here.
    13. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2006
    Location
    San Francisco, CA
    Posts
    23
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeView, Delegates and Persistent Widgets

    Quote Originally Posted by vieraci View Post
    You can do it with one click. I implemented my own method which uses a custom delegate and re-implements the editorEvent(). The following code is for a check box, but it wouldn't be much diferent for any other type of widget.
    Hmm. Well for a check box it's easy. You are just toggling the state based on the mouse release event. But for a combobox you have to populate the combo and then actually care about which one was selected. Which all needs to happen though more events. I guess, because of the complexity, that's why the standard way of implementing the delegate is through the createEditor(), setEditorData(), setModelData() paradigm.

    I'd be interested to see an example if you have one ....

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView, Delegates and Persistent Widgets

    for a combobox you can make a real QComboBox for each item, but set WA_DontShowOnScreen, and instead of this render it in delegate's paint method using QWidget::render() (you have to set the eventFilter on every widget so you can react on PaintEvents). Then enable mouseTracking in the view (or it's viewport - dont remember) and handle mouse movement and clicks in editorEvent().
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  4. #4
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView, Delegates and Persistent Widgets

    You don't have to make a real combo box, it is populated in createEditor() method and don't forget, it's data is already set. All that has to be done is in the editorEvent() method, just call showPopup(). (I haven't tested but it should be this easy).
    Last edited by vieraci; 6th July 2009 at 23:57. Reason: updated contents

  5. #5
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView, Delegates and Persistent Widgets

    Hi...
    I think that my question isn't out of argument from the others, so I post here.

    I have to obtain a result similar to that in the attached photo.
    I started from different Qt examples in order to test different ways to obtain some results and then put them together.
    At the moment I'm a bit stuck on a problem (the first): how to present data from the model in a frame like the one on the right of the sample photo. I tried to modify the spinboxdelegate example to visualize/edit/modify the data from the table in a lateral frame, but with no succes, because I don't know how to place the widget created by
    QWidget *SpinBoxDelegate::createEditor function. I tried to reimplement the paint method, but the problem is always the same...
    The 2nd problem is: I'm reading a lot of documentation trying to comprehend if delegates are the best solution to achieve my goal. At the moment I think so... But if someone has got much experience in this kind of programming and can indicate me an eventually better way, I'll surely evaluate it.

    Note: At the moment I'm stuck on the first problem... The second is a little bigger, but It is part of the evaluation process.

    I really hope someone can help me!
    Best regards and Thanks in advance for your help.
    Alex
    Attached Images Attached Images

  6. #6
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView, Delegates and Persistent Widgets

    Any help?
    Now I tried another way to obtain my goal.
    I started from a little and simple example of the treemodel and I created a model containing items from a file. As item I used QStandardItem, as model I used QStandardItemModel, and I visualized all in a QTreeView. I stored in the various Items different kind of data: an icon with DecorationRole, a string with DisplayRole and some custom data with UserRole. Now my problem is:
    the data that I want to edit when I doubleclick an item are stored in UserRole because if I store them with EditRole they become the same as DisplayRole ('cause of the standard implementation of QStandardItem).
    My questions are:
    1. How can I edit data in UserRole?
    2. If that is not possible, I suppose I have to subclass QStandardItem and reimplement data and setData methods... Is it possible to reimplement only this two methods to obtain a different behaviour for DisplayRole and EditRole? Could someone please post an example?
    3. Any hints on how to edit data? I think a custom delegate would be the better way, do you?

    Thanks,
    Alex

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
  •  
Qt is a trademark of The Qt Company.