Reputation: 2778
I'm developing a Qt5 app that includes a widget whose role is to show the user a list of objects (vertical list, single column), each entry containing 3 or 4 attributes. I also need to reuse a custom widget that was designed to represent each object. My first thought was to use a QListView
, but it seems that they only handle a single attribute. Does anyone know if Qt offers a way to present a list of widgets representing multiple attributes in a single list view?
Upvotes: 2
Views: 1788
Reputation: 839
i believe, what you need is QListWidget
which i have been looking for past few days :(
this is how i use it:
#include <QListWidget>
#include <QPointer>
class MyObjectListWidget : public QListWidget
{
public:
MyObjectListWidget(QWidget *parent=nullptr);
bool addMyObject(QPointer<MyObject> obj);
private:
QList<QPointer<MyObject>> my_objects;
};
and in the cpp file:
bool MyObjectListWidget::addMyObject(QPointer<MyObject> obj){
if(!obj.isValid())
return false;
auto item = new QListWidgetItem(this);
addItem(item);
auto row = new MyObjectItemWidget(obj,this);
item->setSizeHint(row->minimumSizeHint());
setItemWidget(item,row);
my_objects.push_back(obj);
return true;
}
MyObjectItemWidget
is my custom widget with a .ui
file which helps to generate the view and takes a custom object as arg named MyObject
Upvotes: 0
Reputation: 2033
I see three options:
QTreeView
, which offers multiple columns. They don't look like a spreadsheet, like QTableVeiw
does, but more like the list view. You don't have to use a tree structure to use it, it will display lists or tables just fine.QStyledItemDelegate
and render all attributes into one cell yourself. You could even use the custom widget as a base for rendering. However you will have only one instance of it, and it will never actually displayed. So make sure the custom widget will render correctly anyway and that updating it is fast. You have to change its data for every cell you render.QBoxLayout
. If you go that way and have huge amounts of items, you will encounter performance problems though. If you actually encounter that and really want to go that way, check out a library I wrote for that exact purpose: longscroll-qt.Upvotes: 0