Results 1 to 17 of 17

Thread: Model View - Delegate - setIndexWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2008
    Posts
    29
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Model View - Delegate - setIndexWidget

    Hi,
    I tried to use the setIndexWidget() function in my MV GUI Qt Code.
    I just want to have a simple composed widget in my subclass instance of QTableView, an widget inside an table cell.
    Do you have any ideas or better code examples, how to use this function in the right way ? Trolltechs API docu does not explain, how to use this function in Qt's Model-View architecture. As is read before, the best way would be to use this in the delagate (with some dirty tricks), here you find some threads too, which describe my problem best:

    http://lists.trolltech.com/qt-intere...ad00097-0.html

    http://lists.trolltech.com/qt-intere...ad00550-0.html

    Kind regards,
    Alex

  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: Model View - Delegate - setIndexWidget

    Index widgets are not actually part of the MVC concept but they are additional widgets laid on top of items in the view. They are expensive to maintain and one should avoid them. What kind of widgets would you like to put there?
    J-P Nurmi

  3. #3
    Join Date
    Mar 2008
    Posts
    29
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Model View - Delegate - setIndexWidget

    I want to provide widgets in table cells, which consist of some text labels, some icons and an text edit field(date field), perhaps similar to the graphical interface of outlook calender...
    Can you help me how to proceed ?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model View - Delegate - setIndexWidget

    As for icons and text the default delegate should handle it fine. As for text edit fields - you'll be only editing one field at a time, so a slightly modified delegate returning a QTextEdit instance as the editor should be enough.

  5. #5
    Join Date
    Mar 2008
    Posts
    29
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Model View - Delegate - setIndexWidget

    Do you mean, I have to use the different roles in my custom model to let the default delegate render the cells content ?
    I tried to do so, but what made me first think of to try an different way, was, that with the decoration role I only could render one icon.
    I am also not sure, would it be possible to have drag and drop support, if I implement the cells content in this way ?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model View - Delegate - setIndexWidget

    Quote Originally Posted by starcontrol View Post
    Do you mean, I have to use the different roles in my custom model to let the default delegate render the cells content ?
    Yes, that's correct.

    I tried to do so, but what made me first think of to try an different way, was, that with the decoration role I only could render one icon.
    If you want something non-standard, use a custom delegate. You'll be able to render whatever you want depending on the data contained in an item.

    I am also not sure, would it be possible to have drag and drop support, if I implement the cells content in this way ?
    Yes, itemviews support d&d.

  7. #7
    Join Date
    Mar 2008
    Posts
    29
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Model View - Delegate - setIndexWidget

    So I have to implement the delegates paint() function to render my widget ?
    Do I have to paint my icons, text labels, editor fields ? There must be an way, instead "manually painting" the widgets, or ?

    A second wish:
    I have to create spans inside my table view, these spans will have to contain what I described before. The QTableView has an method setSpan(), but how can I check in my custom view from my custom model, where it has to paint the spans?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model View - Delegate - setIndexWidget

    Quote Originally Posted by starcontrol View Post
    So I have to implement the delegates paint() function to render my widget ?
    Not widget, but item - yes.

    Do I have to paint my icons, text labels, editor fields ?
    You only paint the inactive state. In the editing state the delegate returns a real widget.

    There must be an way, instead "manually painting" the widgets, or ?
    I think you have to define what you mean by "widget". Icon is not a widget. Text is not a widget. So, what is a widget according to your definition?

    I have to create spans inside my table view, these spans will have to contain what I described before. The QTableView has an method setSpan(), but how can I check in my custom view from my custom model, where it has to paint the spans?
    Why should the model care about the span? It's just a set of data.

  9. #9
    Join Date
    Mar 2008
    Posts
    29
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Model View - Delegate - setIndexWidget

    I think "manually painting" the items may not be the succeeding way. If I'll use the delegates paint method, I have a simple bitmap, which is rendered inside the table view cell (in my case, inside the span). If the span will be very long, because of an longer event (calender event) the bitmap will be strechted and the text will be rendered not clean, you understand what I mean ?
    The second problem is, the item will also have to contain an "active text edit field (date edit field for editing the event)" in the inactive state, how will this be possible with an rendered bitmap ?

    Kind regards,
    Alex

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model View - Delegate - setIndexWidget

    Quote Originally Posted by starcontrol View Post
    If the span will be very long, because of an longer event (calender event) the bitmap will be strechted and the text will be rendered not clean, you understand what I mean ?
    The model doesn't care. It doesn't have a concept of width or height. The delegate handles all that and it is given the rectangle that needs to be painted.

    The second problem is, the item will also have to contain an "active text edit field (date edit field for editing the event)" in the inactive state, how will this be possible with an rendered bitmap ?
    Render the date as text and react on clicking the cell - activate the editor and let the user edit the date. You don't need the spinbox to be active all the time. You can even render the looks of the box if you really need to.

  11. #11
    Join Date
    Mar 2008
    Posts
    29
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Model View - Delegate - setIndexWidget

    Ok, I tried your suggestion..., but nothing is displayed in my table cells, do you know, what I am doing wrong ? How do I have to implement the sizeHint() function ?
    Qt Code:
    1. void SchulungsplanDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex &index)
    2. {
    3. painter->save();
    4.  
    5. if (option.state & QStyle::State_Active)
    6. painter->fillRect(option.rect, option.palette.highlight());
    7.  
    8. // gets the cells size and draws the text inside
    9. QRect myRect = QRect(option.rect.topLeft(), option.rect.bottomRight()) ;
    10. painter->drawText(myRect, Qt::AlignCenter, "test");
    11.  
    12. painter->restore();
    13. }
    14.  
    15. /*
    16. QSize sizeHint(const QStyleOptionViewItem &option,
    17.   const QModelIndex &index ) const
    18. {
    19. // ????????????
    20. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. Help with Model View
    By weepdoo in forum Qt Programming
    Replies: 13
    Last Post: 12th October 2007, 10:32
  3. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  4. Model - View Programming doubt.
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 13:01
  5. Replies: 6
    Last Post: 20th April 2006, 10:23

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.