What can be placed in a QListWidget?
I'm wondering what the requirements are for an object to be placed in an QListWidget?
Is it sufficient that it inherits QListWidgetItem, and in that case, what function does it need to implement? What about it it besides the QListWidgetItem also inherits the class QGraphicsItem? Can the graphics element (with the QListWidgetItem class inherited) still be placed in the list? Thanks!
Eh... No. That is not how it works.
A QListWidget is a convenience class around QListView and an item model. Items in such a model are just small pieces of data. They are not items that can be painted directly, let alone QGraphicsItems. Items in an item view like QListView are painted by delegates. These decide how to render the piece of data they represent, and they will provide an editor widget if needed.
QGraphicsItem is a part from a completely different area of the Qt API. It belongs in a QGraphicsView. These can not be mixed, at least, not in a simple way.
You might be interested in QAbstractItemView::setIndexWidget. It allows you to instantiate a widget to display in the cell for a specific index. Not a very scalable solution, and also not very integrated into the model-view system (the widget does not get data set for the cell it is on) but it might be enough for what you want.
As another alternative, you might be interested in using QML for your list. It allows you much more freedom in how you render your items.
Thank you for your reply! Actually the only thing I'm interested in is having a list that represents the drawn graphicsitems. That is, every time the user draws a new graphics item (this part is implemented), a reference to this is added to a list. By selecting a item in the list, the corresponding graphicsitem is highlighted in the graphicsview. By deleting an item from the list (selecting it and pressing a dedicated button) the graphicsitem is removed as well.
Briefly, the list is an index of the active graphics items. All the graphics items are drawn by the user at runtime, and a typical amount is 1-10 items. I'm wondering what is the best way to create such a list-item relationship? Note that the name visible in the list can be a duplicate, still each listitem representing a graphicsitem of its own. Thanks
Ah, ok. Please to try be a bit more clear on what your actual problem is next time; that saves people trying to help you a lot of time and energy.
Well, it can be done in several ways. One simple way is to simply store a pointer to the actual graphics item in the QListWidgetItem itself, using setData and a role of your own. You can get that value out again using the data() method.
I apologize for not being accurate enough. I've now tried to add a pointer (referring to a graphics item) to a qlistwidgetitem
but this failed. I get an error message saying "QVariant::QVariant(void*) is private within this context"
I also tried to create a QVariant item and assign the pointer to this:
@QVariant tmp = QVariant::fromValue(node);@
first plainly like this, getting a lot of errors, then adding
to the Node class, also including QtCore, but still getting a bunch of errors, like
"no matching function for call to Node::Node()"
note that the Node class constructor takes parameters, also it inherits:
public QObject, public QGraphicsItem
Unfortunately I'm not accustom to the QVariant... I really appreciate your help! :)
added: I created an additional empty Node constructor
with the implementation:
This removed some of the error, leaving the following:
"QObject::QObject(const QObject&) is private within this context"
"QGraphicsItem::QGraphicsItem(const QGraphicsItem&) is private within this context"