Results 1 to 9 of 9

Thread: Creating advanced widget in a delegate.

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Post Creating advanced widget in a delegate.

    Hey folks,

    I'm trying to display my own widget in a QListView using a QItemDelegate.

    When specifying a simple widget, the delegate seems to work pretty good.
    Unfortunately when I specify a more complete one, it doesn't seem to display anything.
    The view is left blank, plus no resizing of the list row.

    Qt Code:
    1. QWidget * ZeContactDelegate::createEditor(QWidget *parent,
    2. const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. QWidget * test = new QWidget(parent);
    6.  
    7. QVBoxLayout * layout = new QVBoxLayout(test);
    8.  
    9. QPushButton * editor = new QPushButton;
    10. QPushButton * editor2 = new QPushButton;
    11. QPushButton * editor3 = new QPushButton;
    12.  
    13. layout->addWidget(editor);
    14. layout->addWidget(editor2);
    15. layout->addWidget(editor3);
    16.  
    17. test->resize(333, 333);
    18. test->show();
    19.  
    20. return test;
    21. }
    To copy to clipboard, switch view to plain text mode 

    Any thoughts ?

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

    Default Re: Creating advanced widget in a delegate.

    You shouldn't resize the widget, let the view handle the geometry. If you're trying to force item size this way, it won't work. From what I see the widget you return is hardly an editor... maybe you should use setIndexWidget() instead?

  3. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Creating advanced widget in a delegate.

    According to the Qt documentation

    void QAbstractItemView::setIndexWidget ( const QModelIndex & index, QWidget * widget )
    Sets the given widget on the item at the given index, passing the ownership of the widget to the viewport.
    [...]
    This function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QItemDelegate instead.
    This function was introduced in Qt 4.1.


    I guess setIndexWidget might not be the best way to do that.

  4. #4
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Creating advanced widget in a delegate.

    I'm currently using QListView with a custom "string model" and my own delegate.

    According to Qt doc :

    "The QListView class provides a list or icon view onto a model."

    Instead of using "QListView" I guess I could :

    1/ inherit "QAbstractItemView" and create my own 'paint' routine in a new View with no delegate.
    2/ "hack" the "QListView" and create my own paint routine in my delegate.

    Is there a best solution?

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

    Default Re: Creating advanced widget in a delegate.

    Quote Originally Posted by bunjee View Post
    I guess setIndexWidget might not be the best way to do that.
    Depends what you want to do
    What exactly do you want to achieve anyway? I doubt a widget with three push buttons may be appropriate to be called an "editor".

  6. #6
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Creating advanced widget in a delegate.

    Something like that :

    http://www.qtcentre.org/forum/f-qt-p...list-5409.html

    A sort of expanded view of an item with several buttons and stuff when clicking on it.

    Thanks.

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

    Default Re: Creating advanced widget in a delegate.

    So why do you say setIndexWidget is not fit for it? I would myself use a custom painting delegate instead of sticking a widget there anyway, but that's me The thing you want to put there is certainly not an editor...

  8. #8
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Creating advanced widget in a delegate.

    Quote Originally Posted by wysota View Post
    So why do you say setIndexWidget is not fit for it? I would myself use a custom painting delegate instead of sticking a widget there anyway, but that's me The thing you want to put there is certainly not an editor...
    If I wanted to do like you,
    how would I implement a QPushButton in the painting delegate ?

    Thanks.

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

    Default Re: Creating advanced widget in a delegate.

    Qt Code:
    1. style()->drawControl(...)
    To copy to clipboard, switch view to plain text mode 
    and use the editorEvent to handle a click.

    Using setIndexWidget() is fine too, it'll be harder to control its looks though. I surely wouldn't use an editor widget there... There is just no benefit of doing that - you don't actually edit anything. Of course it's possible to do that and you can use it if you wish. It's just I'd chose another way

  10. The following user says thank you to wysota for this useful post:

    bunjee (5th February 2007)

Similar Threads

  1. Creating a square sized widget in a layout
    By cboles in forum Qt Programming
    Replies: 5
    Last Post: 22nd September 2008, 23:38
  2. Pin/Unpin Dock Widget
    By charlesD in forum Newbie
    Replies: 1
    Last Post: 21st June 2006, 06:57
  3. Replies: 4
    Last Post: 24th March 2006, 22:50
  4. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16
  5. advanced split widget
    By vitaly in forum Qt Programming
    Replies: 10
    Last Post: 24th January 2006, 20:00

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.