Hello there :)
I'm using the QtCreator IDE to build my project but it will not be a problem helping me out.
Let's say that there is a class called QListWidget. An object of this class can accept items like this:
listwidget->addItem(QListWidgetItem *item)
QListWidgetItem is another class. Every QListWidgetItem object has functions such as setToolTip() and setStatusTip(), but I also want to store there another string, not visible by the user.
So, I thought that subclassing QListWidgetItem was a good idea, and I implemented it as simply as I could:
1 2 3 4 5
|
class MyListItem : public QListWidgetItem
{
public:
QString image_path;
};
|
Then, I used the following code:
1 2 3 4
|
MyListItem * item = new MyListItem();
item->setText("This is a new item inside a listwidget and this is visible to the user text");
item->image_path="This is text not visible to the user, I store it as a public variable";
ui->listWidget->addItem(item);
|
I don't feel I've won something like this, though. Once my 'item' is stored inside the listWidget, I cannot recall the image_path function. The QListWidget class has a function:
ui->listWidget->item(int position)
that returns a specific QListWidgetItem. But it is not a MyListItem, thus I cannot call the 'image_path' variable to see its value.
I am obviously missing somethinig. How can what I ask for be implemented?
I know that I ask a question depending on classes of a specific IDE, but I think that the question can be generalized. When a Class accepts specific items, and you subclass the class of these items in order to store more data in them, how can you recall these data while the first class returns the normal class of the items you have subclass and not your own class?