Need help on QListView or QListWidget
-
Hello,
I want to populate a QListView or(?) QListWidget with listitems containing a human readable QSring and a struct pointer.
After selecting the QString I want to get back thecorresponding struct pointer. (similar to a combobox)Here is what I did to populate a QList:
QList<QPair<QString, struct device_info*> > *ql = new QList<QPair<QString, struct device_info*> >; ql->append(qMakePair(string, current_device));
Little code or link highly welcome, thx.
-
@wally123 said:
Hi if you use a QListWidget,
you could use the items data property to store a pointer to the device_info with the Text
Then when u select one, u have instance access to the structs.
please see here
http://stackoverflow.com/questions/7136818/can-i-store-some-user-data-in-every-item-of-a-qlistwidget -
Anyway, I can not build it.
QListWidgetItem *newItem = new QListWidgetItem; QVariant qv1(current_device); newItem->setData(Qt::UserRole, qv1); newItem->setText(string); ui->lw_device_list->insertItem(row, newItem);
error message
/opt/Qt/5.5/gcc_64/include/QtCore/qvariant.h:465: error: 'QVariant::QVariant(void*)' is private inline QVariant(void *) Q_DECL_EQ_DELETE; ^
struct device_info* current_device
-
@wally123 said:
is current_device a void * ?
device_info is just normal struct yes?You will need to typecast to get QVariant to eat it.
Maybe you can have Qlist with structs and just store the index to qlist.
That may be more safe than say cast to void * and back to pointer to device_info
else see here
http://blog.bigpixel.ro/2010/04/storing-pointer-in-qvariant/QVariant qv1= qVariantFromValue((void *) current_device);
U can get friends with QVariant here
http://doc.qt.io/qt-5/qvariant.html#QVariant-1