Results 1 to 20 of 21

Thread: inserting custom Widget to listview

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default inserting custom Widget to listview

    Hi All,

    I want to add one custom widget having a label and a button to my listview as one list item ...

    If there is any code snippet or example code in this scenario .. please do suggest ..


    Thanks,
    Amit

  2. #2
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: inserting custom Widget to listview

    Use void QAbstractItemView::setIndexWidget ( const QModelIndex & index, QWidget * widget )
    See also the warning in the documentation that you have to create a delegate if you want anything sensible with the widget. Generally, adding widgets to an itemview is a bad idea because it does not scale. If you 1000 rows, you need 1000 widgets in a naive implementation. The better implementation is to have only as much widgets as currently visibie and reuse widgets that get out of view for the appearing rows during scrolling. Basically, that is what the delegate does.
    It's nice to be important but it's more important to be nice.

  3. The following user says thank you to axeljaeger for this useful post:

    MarkoSan (10th January 2010)

  4. #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: inserting custom Widget to listview

    If you want to show only standard controls like labels, buttons, check boxes then the most efficient way is to make your own item delegate where in paint() method you paint those controls with QStyle (see drawControl() and others). But those controls are only drawn so you have to implement by hand event processing in QAbstractItemDelegate::editorEvent() and you have to make eventFilter and install it on view's viewport to which you have to enable mouse tracking (and maybe set WA_Hover attribute to get Leave and Enter events - I don't remember exactly). Sounds complicate but it is the most efficient way in case with simple widgets, because you don't really have any real widget objects.
    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.

  5. The following user says thank you to faldzip for this useful post:

    MarkoSan (10th January 2010)

  6. #4
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: inserting custom Widget to listview

    Quote Originally Posted by axeljaeger View Post
    Use void QAbstractItemView::setIndexWidget ( const QModelIndex & index, QWidget * widget )
    See also the warning in the documentation that you have to create a delegate if you want anything sensible with the widget. Generally, adding widgets to an itemview is a bad idea because it does not scale. If you 1000 rows, you need 1000 widgets in a naive implementation. The better implementation is to have only as much widgets as currently visibie and reuse widgets that get out of view for the appearing rows during scrolling. Basically, that is what the delegate does.
    So, then the custom widget is "placed" into delegate and delegate then handles number of showing widgets?
    Qt 5.3 Opensource & Creator 3.1.2

  7. #5
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: inserting custom Widget to listview

    I guess the widget replaces the delegate for that certain cell. If you have 100 rows, you will have to set 100 widgets. I think you will also have to to data binding of that widget yourself.
    It's nice to be important but it's more important to be nice.

  8. #6
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: inserting custom Widget to listview

    Quote Originally Posted by axeljaeger View Post
    I guess the widget replaces the delegate for that certain cell. If you have 100 rows, you will have to set 100 widgets. I think you will also have to to data binding of that widget yourself.
    Hmm, I am a little confused right now. Here is how I want for cell to look like in attachment. Is delegate only interaction part (3 buttons on the right side) or whole cell with labels and pic?
    Attached Images Attached Images
    Qt 5.3 Opensource & Creator 3.1.2

  9. #7
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: inserting custom Widget to listview

    Delegate is responsible for drawing the whole cell's content including labels. It is also responsible for the behaviour.
    It's nice to be important but it's more important to be nice.

  10. #8
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: inserting custom Widget to listview

    So, I subclass QWidget with all labels, pic, buttons, ..., layout them as in attachment and then I set this widget to subclassed QAbstractItemDelegate?
    Qt 5.3 Opensource & Creator 3.1.2

  11. #9
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: inserting custom Widget to listview

    Yes but there is one showstopper: The delegate will use the widget only in edit state. For all other cells, it will use QAbstractItemDelegate:aint.
    It's nice to be important but it's more important to be nice.

  12. #10
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: inserting custom Widget to listview

    Quote Originally Posted by axeljaeger View Post
    Yes but there is one showstopper: The delegate will use the widget only in edit state. For all other cells, it will use QAbstractItemDelegate:aint.
    With "edit state" you mean Qt::EditRole?
    Qt 5.3 Opensource & Creator 3.1.2

  13. #11
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: inserting custom Widget to listview

    No: For example, if you have a table, you see a lot of cells, but only one cell can show a blinking textcursor at the same time. This cell is in "editing" state. All others are just "labels". You usually not use full widgets for the cells that are not in editing state because of performance reasons.
    It's nice to be important but it's more important to be nice.

  14. The following user says thank you to axeljaeger for this useful post:

    MarkoSan (10th January 2010)

  15. #12
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: inserting custom Widget to listview

    Well, thanks, but that means my cell design will be performance demanding??
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. How to use custom widget editor with QItemDelegate
    By wysman in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2009, 18:20
  2. Custom Widget - First Steps
    By sekatsim in forum Qt Programming
    Replies: 8
    Last Post: 26th June 2008, 17:19
  3. Custom widget
    By zorro68 in forum Qt Programming
    Replies: 7
    Last Post: 28th January 2008, 14:06
  4. custom plug-in widget in another custom plug-in widget.
    By MrGarbage in forum Qt Programming
    Replies: 6
    Last Post: 27th August 2007, 15:38
  5. Custom tab widget question
    By PrimeCP in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2007, 11:17

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.