Results 1 to 8 of 8

Thread: QListView and custom Widgets

  1. #1
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QListView and custom Widgets

    I want to create a QListView that displays custom widgets inside. I use it to reorder these custom widgets (for instance by using a custom model like here) and they need to be widgets, because I want to have different QPushButtons inside them.

    wysota mentioned in this post that
    You can have a real widget, but it's not worth it - it's too heavy right now (maybe when 4.4 is released this situation will change).
    What is the situation in 4.7, is it 'worth it' now?

    Also I am not sure how to actually implement it the way I want it. From this tutorial I know how I would create custom Widgets in order to edit Model data, but in my case, the custom widget should always be displayed inside the QListView (and not just when an item is edited).

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QListView and custom Widgets

    Yes, you can do it.

    It will be cost you more time to do it with QListView, but this is recommended if you are planning to have different types of custom Widgets in the ListView.

    If you plan to have just couple of custom widgets, you are try using QListWidget, which should be quick and simple.

    Yes indeed it is possible to display your custom widget inside the ListView in list item display mode and even in list item editing mode, it possible with both QListView and QListWidget.

  3. #3
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListView and custom Widgets

    Quote Originally Posted by Santosh Reddy View Post
    Yes, you can do it.

    It will be cost you more time to do it with QListView, but this is recommended if you are planning to have different types of custom Widgets in the ListView.

    If you plan to have just couple of custom widgets, you are try using QListWidget, which should be quick and simple.

    Yes indeed it is possible to display your custom widget inside the ListView in list item display mode and even in list item editing mode, it possible with both QListView and QListWidget.
    Actually, for the time being, it would be just one custom Widget, not different types of custom Widgets. But why would it be easier then wiht a QListWidget instead of a QListView?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QListView and custom Widgets

    For using QListView, one need to implement required Model interface functions (i guess atleast 5 Calls need to be implemented), and then a Delegate
    Refer for delegate: http://doc.qt.nokia.com/4.7-snapshot...imple-delegate

    For using QListWidget, this interface is already implemted and you can create custom widgets by sub-classing QListWidgetItem, and just design your widget, no need to worry about ModelIndex, Delegate etc as in case of QListView
    Refer: http://doc.qt.nokia.com/4.7-snapshot...t.html#details

    class MyListWidgetItem : public QListWidgetItem()
    {

    };

    QListWidgetItem *newItem = new MyListWidgetItem;
    newItem->setText(itemText);
    listWidget->insertItem(row, newItem);

    You also need to use QListWidget::setItemWidget()
    Last edited by Santosh Reddy; 10th May 2011 at 18:06.

  5. #5
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListView and custom Widgets

    Quote Originally Posted by Santosh Reddy View Post
    For using QListView, one need to implement required Model interface functions (i guess atleast 5 Calls need to be implemented), and then a Delegate
    Refer for delegate: http://doc.qt.nokia.com/4.7-snapshot...imple-delegate

    For using QListWidget, this interface is already implemted and you can create custom widgets by sub-classing QListWidgetItem, and just design your widget, no need to worry about ModelIndex, Delegate etc as in case of QListView
    Refer: http://doc.qt.nokia.com/4.7-snapshot...t.html#details

    class MyListWidgetItem : public QListWidgetItem()
    {

    };

    QListWidgetItem *newItem = new MyListWidgetItem;
    newItem->setText(itemText);
    listWidget->insertItem(row, newItem);

    You also need to use QListWidget::setItemWidget()
    Yes, but are you sure I can subclass QListWidgetItem to make list widget items that consist of regular QWidgets, like a QLabel and a QPushButton for example? QListWidgetItem does not seem to have any virtual function that I could implement to achieve this. And the only thing the documentation mentions about subclassing QListWidgetItems is the UserType.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QListView and custom Widgets

    Ok, This is how you do it,

    Sub-class QListWidgetItem to somthing like MyListWidgetItem, then in MyListWidgetItem ctor, you need to create custom QWidget, and whatever you want in it, like QLayout, QPushbutton, anything which you can put onto a QWidget.

    Then use this new QWidget, and using QListWidget::setItemWidget(QWdiget*) , set it to the cell on which the newly created MyListWidgetItem is being created.

    Example: the ctor looks somting like

    class MyListWidgetItem : public QTableWidgetItem
    {
    public:
    MyListWidgetItem(const QString &text, int type) ;
    void setCustomWidget(void);
    }

    MyListWidgetItem::MyListWidgetItem(const QString &text, int type) : QTableWidgetItem(text, type)
    {
    ;
    }

    MyListWidgetItem::setCustomWidget(void)
    {
    QWidget* customWdiget = new QWdiget();
    customWdiget->setName(text);

    // add more widgets onto customWdiget
    // then add this widget to the table cell, where this MyListWidgetItem is present
    this->tableWidget()->setCellWidget(this->row(), this->column(), customWdiget);
    }

    Good Luck...

  7. #7
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListView and custom Widgets

    Hm, I made a simple test with a QListWidget like this:
    Qt Code:
    1. class MyListWidgetItem : public QListWidgetItem
    2. {
    3. public:
    4. MyListWidgetItem( const QString& text, QListWidget* parent = 0, int type = Type ) : QListWidgetItem( text, parent, type ) {}
    5.  
    6. void setCustomWidget()
    7. {
    8. QPushButton* button = new QPushButton( "Button" );
    9. this->listWidget()->setItemWidget( this, button );
    10. }
    11. };
    12.  
    13.  
    14.  
    15.  
    16. lw.addItem( new MyListWidgetItem( "" ) );
    17. lw.show();
    To copy to clipboard, switch view to plain text mode 
    but it won't show the QPushButton. Also the documentation says
    This function should only be used to display static content in the place of a list widget item. If you want to display custom dynamic content or implement a custom editor widget, use QListView and subclass QItemDelegate instead.
    So if I want to have clickable QPushButtons, I would still have to do it via QListView and QItemDelegate? (And handle the interaction manually there.)

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QListView and custom Widgets

    Yes, you can use it only for static content, I guess still you can connect signals to the widget, may not be able to edit it graphically

    Here is an example of cutom widget with QPushButtons, with the screenshot attached, its bit tricky, the baseclass QListWidgetItem has to be already present/added in the QListWidget for setItemWidget() to work.

    And again, you if you need to edit the widget, like enter some data, in text filed, you better use QDelegateItem, use QListView to be more generic. It would be your design decision.


    CustomListViewItem.JPG

    Qt Code:
    1. void MainWindow::AddCustomListWidgets(QListWidget* listWidget) const
    2. {
    3. listWidget->setAlternatingRowColors(true);
    4.  
    5. QListWidgetItem* new_item;
    6.  
    7. new_item = new QListWidgetItem(QString("Standard QListWidgetItem 1"), listWidget);
    8. new_item = new QLWIPushButton(QString("QPushButtonItem 1"), listWidget);
    9. listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 2")));
    10. new_item = new QLWIPushButton(QString("QPushButtonItem 2"), listWidget);
    11. listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 3")));
    12. new_item = new QLWIPushButton(QString("QPushButtonItem 3"), listWidget);
    13. listWidget->addItem(new QListWidgetItem(QString("Standard QListWidgetItem 4")));
    14.  
    15. }
    16.  
    17. QLWIPushButton::QLWIPushButton(const QString text, QListWidget* view)
    18. {
    19. QWidget* widget = new QWidget;
    20. QGridLayout* layout = new QGridLayout();
    21.  
    22. layout->addWidget(new QPushButton(text), 0, 0);
    23. layout->addWidget(new QPushButton(text), 0, 1);
    24. layout->addWidget(new QPushButton(text), 1, 0);
    25. layout->addWidget(new QPushButton(text), 1, 1);
    26.  
    27. widget->setLayout(layout);
    28.  
    29. view->setItemWidget(this, widget);
    30. setSizeHint(widget->sizeHint());
    31. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 18th July 2012, 09:59
  2. QListView: Custom or not
    By Archa4 in forum Newbie
    Replies: 5
    Last Post: 8th February 2011, 14:04
  3. QListView::setIndexWidget custom sorting
    By drus in forum Qt Programming
    Replies: 2
    Last Post: 10th December 2010, 08:52
  4. QListView and custom widgets in each cell
    By T4ng10r in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2009, 10:14
  5. Replies: 0
    Last Post: 15th May 2009, 15:38

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.