[Solved] QListWidget for displaying items in QTableView’s cells
Hi,
I’m trying to use QListWidgets to display QStringLists in the cells of a QTableView. To do so, I subclassed QStyledItemDelegate and render the QListWidget in the paint() method. This works fine so far, the only problem I have is, that option.rect.topLeft() in paint(…, const QStyleOptionViewItem& option, …) does not correspond to the top left corner of the corresponding cell. It seems to be off by the dimensions of the top left header cell, as this screenshot suggests:

So my question is: how do I get the right coordinates for correctly rendering the QListWidgets in QTableView’s cells? Working example code is below.
Thanks,
Frank
MultipleSelectDelegate.h:
- #ifndef MULTIPLESELECTDELEGATE_H
- #define MULTIPLESELECTDELEGATE_H
- #include <QStyledItemDelegate>
- Q_OBJECT
- public:
- };
- #endif // MULTIPLESELECTDELEGATE_H
MultipleSelectDelegate.cpp:
- #include "MultipleSelectDelegate.h"
- #include <QStyledItemDelegate>
- #include <QListWidget>
- #include <QStringList>
- QListWidget list_widget;
- list_widget.addItems(list);
- list_widget.render(painter, option.rect.topLeft());
- }
- QListWidget list_widget;
- list_widget.addItems(list);
- return list_widget.sizeHint();
- }
TableModel.h:
- #ifndef TABLEMODEL_H
- #define TABLEMODEL_H
- #include <QAbstractTableModel>
- #include <QModelIndex>
- #include <QList>
- #include <QObject>
- #include <QVariant>
- Q_OBJECT
- public:
- private:
- };
- #endif // TABLEMODEL_H
TableModel.cpp:
- #include "TableModel.h"
- #include <QStringList>
- list1 << "one" << "two" << "three" << "four";
- list2 << "1111" << "2222" << "3333" << "4444";
- data_ << list1 << list2;
- }
- return data_.size();
- }
- return 1;
- }
- if (!index.isValid())
- if (index.row() >= data_.size())
- return data_.at(index.row());
- }
- }
main.cpp:
- #include <QApplication>
- #include <QTableView>
- #include "MultipleSelectDelegate.h"
- #include "TableModel.h"
- int main(int argc, char *argv[]) {
- TableModel* model = new TableModel(table);
- MultipleSelectDelegate* delegate = new MultipleSelectDelegate(table);
- table->setModel(model);
- table->setItemDelegate(delegate);
- table->show();
- return app.exec();
- }
5 replies
Do you need the list widget for some functionality or do you just use it to “ease” the display of the string list? If you do the latter, there surely would be more elegant and efficient ways to do this.
As a first thought, I would try to change the data() method of your model and have it return a string for the Qt::DisplayRole:
- {
- return theStringList.join("\n");
- } else {
- // your other role code here....
- }
- }
The standard delegates handle multiline text.
Just to explain why I’ve choosen a QListWidget for displaying a list of strings: My final goal is to implement an item delegate for multiple selection of objects in a list. If the list is displayed some items of it should be selected and when the user edits the cell it should be possible to select other items or deselect already selected items. I thought the easiest way to accomplish a multiple select delegate is to use a QListWidget for displaying and editing the selections.
I found a solution for my problem by looking again at the Star Delegate Example [doc.qt.nokia.com]. Instead of calling render(…) with option.rect.topLeft() as offset, translating painter with option.rect.topLeft() before calling render(…) fixes my problem:
- list_widget.render(painter, option.rect.topLeft());
- painter->translate(option.rect.topLeft());
- list_widget.render(painter);
So apparently I didn’t understand what the effect of calling QWidget::render(…) with a targetOffset [doc.qt.nokia.com] means.
Thanks again for all your input.
You must log in to post a reply. Not a member yet? Register here!



