Results 1 to 20 of 22

Thread: QT4: Sorting in QTreeWidget (subclass)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 Re: QT4: Sorting in QTreeWidget (subclass)

    Ah. Call the base class implementation. That makes sense. I'll try that and post the result later today. 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
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QT4: Sorting in QTreeWidget (subclass)

    Yep, that was it, of course. Thanks for your help!
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  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: QT4: Sorting in QTreeWidget (subclass)

    How do I tell a row in the treewidget to redraw itself after it has been changed by a signal/slot connection?

    I can't find anything. But I must be looking in the wrong place.

    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

  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: QT4: Sorting in QTreeWidget (subclass)

    In what way do you change the content of that row? It should update itself.

    Of course you can force an update of a certain part of the widget/view, but I think there might be something wrong with your approach..

    The forced update would look for example something like:
    Qt Code:
    1. update(visualRect(item));
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. setDirtyRegion(visualRect(item));
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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: QT4: Sorting in QTreeWidget (subclass)

    It updates itself when I call the base class setData function inside the derived setData function. If I don't, it won't. In some cases (some roles), I want to do something else with incoming data, and not call the base class function.

    I assume that the base class function calls one of those update functions (which is how it happens 'automatically'). And maybe I have to do that too.

    The update() function doesn't work.

    Just to clarify, my setData function only changes one of those four private variables. So I'm not surprised the update doesn't happen automatically.

    EDIT: I've found a solution. Not sure it's the best one. In the setData function, I call the QTreeWidgetItem::setText(int column, QString text). If there's a better way (I suspect there is a way which calls getData), please let me know.
    Last edited by Michiel; 28th March 2006 at 19:33.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  6. #6
    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: QT4: Sorting in QTreeWidget (subclass)

    Yes, the base class implementation of setData() leads the internal tree model to emit dataChanged() signal which furthermore causes the view to update itself.

  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: QT4: Sorting in QTreeWidget (subclass)

    Quote Originally Posted by Michiel
    In the setData function, I call the QTreeWidgetItem::setText(int column, QString text).
    QTreeWidgetItem::setText(column, text) is exactly the same than
    QTreeWidgetItem::setData(column, Qt::DisplayRole, text)

    Edit: I would suggest you to simply call the base class implementation, or what's the loss with that? You can pick your member variables and still call the base class implementation..

    Qt Code:
    1. void TreeWidgetItem::setData(int column, int role, const QVariant & value) {
    2. QTreeWidgetItem::setData(column, role, value);
    3. // pick your variables
    4. if (role == Qt::EditRole)
    5. ..
    6. else if (role == Qt::UserRole)
    7. ..
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 28th March 2006 at 20:21.

  8. #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: QT4: Sorting in QTreeWidget (subclass)

    It might do a lot of redundant stuff. Like, set the text itself, then overwrite it again with my code.

    Also, wouldn't it update before my data is changed?
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  9. #9
    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: QT4: Sorting in QTreeWidget (subclass)

    Umm..
    Quote Originally Posted by Michiel
    Just to clarify, my setData function only changes one of those four private variables.
    Well, change the order. Do your stuff first and call the base class implementation afterwards..
    You could also do a trick like when your setData() receives a QDateTime variant with user role, you pick that date as a member variable and pass QDateTime.toString() with display role to the base class implementation.

    But anyway.. don't take this as offense, but is there really any need for overriding data() and setData() at all? Unless you are gonna do some more customize stuff, you could avoid all this hassle and handle it by just simply overriding the operator used for comparing..

  10. #10
    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: QT4: Sorting in QTreeWidget (subclass)

    How could I intercept a user-edit of a field otherwise? If a user changes a date with the keyboard, I'll need QDateTime::fromString() to convert it to a date. As far as I can see, overwriting setData is the only way. And since I have to use it anyway, why not put everything else relevant in there too? I need a function to change _start and _end from the outside anyway. setData seems to do the trick.

    That, and I'm just trying to program the way the qt developers probably intend me to. Since those functions are virtual, I think they should be overwritten by subclasses. I didn't know what other parts of QT relied on them.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  11. #11
    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: QT4: Sorting in QTreeWidget (subclass)

    Quote Originally Posted by Michiel
    How could I intercept a user-edit of a field otherwise?
    You could even place a QDateTimeEdit widget as an item widget in each item you intend to.

    Quote Originally Posted by Michiel
    I didn't know what other parts of QT relied on them.
    The problem with overriding data() and/or setData() is that Qt's "convenience" itemviews rely on their strictly private model. So you really have to know what you're doing..
    You can set custom data with custom roles without overriding data() or setData(). Qt will just not handle them. Delegates are there for display and editing facilities for items.

  12. #12
    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: QT4: Sorting in QTreeWidget (subclass)

    I think I should have used QTreeView anyway. I'll probably change to that later. For now, I want a working program, and at the moment, it works.

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

Similar Threads

  1. Drag and Drop in a QTreeWidget subclass
    By kishore in forum Qt Programming
    Replies: 2
    Last Post: 14th May 2008, 07:12
  2. Sorting column in QTreeWidget
    By lni in forum Qt Programming
    Replies: 8
    Last Post: 14th November 2007, 18:35
  3. Sorting in QTreeWidget
    By adhit in forum Qt Programming
    Replies: 15
    Last Post: 8th May 2007, 12:49
  4. resizing a QTreeWidget
    By drhex in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2006, 22:32

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.