How to store QListWidgetItem in QPushButton ?
-
I have a QListWidgetItem*. In that QListWidgetItem* I have created a QPushButton*. And I am expecting QListWidgetItem data should get store in QPushButton*.
I tried this way but it is worng:void setButton(QListWidget* inputWidget, QListWidgetItem* item) { btn = new QPushButton() //seeting icon //setting icon size btn->setData(Qt::UserRole, QVariant::fromValue((void *) btn); // error No setData() for QPushButton inputWidget->setItemWidget(item, btn); }
I am trying to connect btn with slot this way
connect(btn,SIGNAL(clicked(), this, SLOT(GetButtonData()))) ;In GetButtonData() I want to retrieve data stored in btn.
-
I have a QListWidgetItem*. In that QListWidgetItem* I have created a QPushButton*. And I am expecting QListWidgetItem data should get store in QPushButton*.
I tried this way but it is worng:void setButton(QListWidget* inputWidget, QListWidgetItem* item) { btn = new QPushButton() //seeting icon //setting icon size btn->setData(Qt::UserRole, QVariant::fromValue((void *) btn); // error No setData() for QPushButton inputWidget->setItemWidget(item, btn); }
I am trying to connect btn with slot this way
connect(btn,SIGNAL(clicked(), this, SLOT(GetButtonData()))) ;In GetButtonData() I want to retrieve data stored in btn.
@tushu said in How to store QListWidgetItem in QPushButton ?:
In that QListWidgetItem* I have created a QPushButton*.
No you haven't. You have created a
QPushButton *
in some free functionsetButton()
, which takes a parameter ofQListWidgetItem*
.btn->setData()
QPushButton
does not have anysetData()
method. Nor if it did would there be any point settingbtn->setData(btn)
, i.e. to itself, as you show. You cannot store "data" in aQPushButton
(other than, say, properties, which I don't think you are asking for).I really don't know what you want to do here.
-
@tushu said in How to store QListWidgetItem in QPushButton ?:
In that QListWidgetItem* I have created a QPushButton*.
No you haven't. You have created a
QPushButton *
in some free functionsetButton()
, which takes a parameter ofQListWidgetItem*
.btn->setData()
QPushButton
does not have anysetData()
method. Nor if it did would there be any point settingbtn->setData(btn)
, i.e. to itself, as you show. You cannot store "data" in aQPushButton
(other than, say, properties, which I don't think you are asking for).I really don't know what you want to do here.
@JonB Thank you for your reply
I have QListWidgetItem and QPushButton this way :
When I will click on button, I want QListWidgetItem data ( i.e. U1::clk) Then I will work on that data.
How to do this ?
( I just read that if we click on QListWidgetItem, we get its index and through that we can access data , so no need to store data on button. But not understanding how to do that )
-
@JonB Thank you for your reply
I have QListWidgetItem and QPushButton this way :
When I will click on button, I want QListWidgetItem data ( i.e. U1::clk) Then I will work on that data.
How to do this ?
( I just read that if we click on QListWidgetItem, we get its index and through that we can access data , so no need to store data on button. But not understanding how to do that )
@tushu
That sounds a little different, and a little clearer and better :)So you have a bunch of
QListWidgetItem
s, and on each one you will place aQPushButton
viaQListWidgetItem::setItemWidget(QPushButton)
, right? And then when the button is clicked you want to retrieve the data associated with theQListWidgetItem
(or itsQModelIndex
in the data model) it is on.I see two possible approaches:
-
When you go
inputWidget->setItemWidget(item, btn)
in your code above, take the data from the item or model and place (a copy of) that onto theQPushButton
. Now you don't need to map from the button to the item it is on. But note this only works if the data you want is static and known at the time the button is created. It's not useful if the data is not known or can change later.... -
When the button is clicked, go find (somehow) which item/index it has been placed on, and from that retrieve the current value of whatever data. This is dynamic, allowing for the data to be changing.
I don't think you should proceed till you identify which of these two approaches gives what you will need.
-