2 Attachment(s)
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:
Attachment 12207
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):
Attachment 12208
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.
Code:
void updateView() {
ui->listWidget->clear();
m_dataList->clear(); // QStringList
// fill the given QStringList with data
getData(m_dataList);
foreach
(const QString &itemInList, m_dataList
) { ListEntry *entry = new ListEntry(itemInList, ui->listWidget);
item->setSizeHint(size);
listWidget->setItemWidget(item, entry);
connect(entry, &ListEntry::delete,
this, &MainWindow::onDeleteClicked);
}
}
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.
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).
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?).