Results 1 to 3 of 3

Thread: How to fill QListWidget with custom widgets by using QStringList as data model

  1. #1
    Join Date
    Nov 2016
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default How to fill QListWidget with custom widgets by using QStringList as data model

    Hi everyone,

    I have a huge list of strings (up to 5.000) in a QStringList that I want to display in a custom way (not only as a simple list of strings).
    Therefore I created a QWidget in designer in the way how each of my list entries should be displayed.

    The custom QWidget:
    Screen Shot 2016-11-07 at 15.25.49.png

    In my mainview I want to have a QListWidget or QListView that uses my QStringList as data model (may I have to switch to QStringListModel) but display each line as one of the above QWidgets.
    And if the data of the model (QStringList/Model) changes the QListWidget/View should update automatically.

    My desired result (with display button mouseover):
    Screen Shot 2016-11-07 at 16.04.57.png
    Don't worry there will be a search bar too.

    I know how to make a (simple) QListView that uses and reacts on changes of a model.
    I also know how to get the custom QWidget into a QListWidget.
    But I have no idea on how to connect these two things and have a view that is filled by and reacts on a models data and displays the data in custom widgets.

    This is how I currently update my view.
    It deletes all current entries fetches the list again and fills in everything again, but I think there has to be a way to make that easier/better.
    Qt Code:
    1. void updateView() {
    2. ui->listWidget->clear();
    3. m_dataList->clear(); // QStringList
    4.  
    5. // fill the given QStringList with data
    6. getData(m_dataList);
    7.  
    8. foreach (const QString &itemInList, m_dataList) {
    9. ListEntry *entry = new ListEntry(itemInList, ui->listWidget);
    10.  
    11. QSize size(60, 60);
    12. QListWidgetItem *item = new QListWidgetItem(listWidget);
    13. item->setSizeHint(size);
    14. listWidget->setItemWidget(item, entry);
    15.  
    16. connect(entry, &ListEntry::delete,
    17. this, &MainWindow::onDeleteClicked);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    Maybe there is just a little piece to solve it that I'm currently totally not aware of.

    Kind regards for everyone to help out.

  2. #2
    Join Date
    Mar 2014
    Posts
    23
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: How to fill QListWidget with custom widgets by using QStringList as data model

    You might want to have a look at QItemDelegate.
    Using a delegate enables you to completely customize the look of your items in a QListView (and other item containers as well).

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

    EneRevo (8th November 2016)

  4. #3
    Join Date
    Nov 2016
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to fill QListWidget with custom widgets by using QStringList as data model

    This looks like the missing piece I wasn't aware of.

    Thank you.

    Edit:
    Maybe I was a bit to fast.

    The Delegate actually looks like what I wanted,
    but I can't figure out how to use my designed QWidget with it or draw a QPushButton in the paint function (due to other forums and threads it isn't possible to draw a PushButton there, true?).
    Last edited by EneRevo; 8th November 2016 at 14:35.

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 20:39
  2. How to lock data petition for a custom model ?
    By tonnot in forum Qt Programming
    Replies: 5
    Last Post: 2nd November 2011, 09:32
  3. Combine custom delegate and model data() roles
    By qlands in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2011, 13:43
  4. mapping composite data from model to widgets
    By tzeentch.gm in forum Newbie
    Replies: 4
    Last Post: 4th October 2011, 07:32
  5. Custom model for QTreeView with SQLite data source
    By emanuel in forum Qt Programming
    Replies: 0
    Last Post: 19th January 2011, 01:53

Tags for this Thread

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.