Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to store QListWidgetItem in QPushButton ?
Forum Update on Monday, May 27th 2025

How to store QListWidgetItem in QPushButton ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 401 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tushu
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • T tushu

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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 function setButton(), which takes a parameter of QListWidgetItem*.

      btn->setData()

      QPushButton does not have any setData() method. Nor if it did would there be any point setting btn->setData(btn), i.e. to itself, as you show. You cannot store "data" in a QPushButton (other than, say, properties, which I don't think you are asking for).

      I really don't know what you want to do here.

      T 1 Reply Last reply
      1
      • JonBJ JonB

        @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 function setButton(), which takes a parameter of QListWidgetItem*.

        btn->setData()

        QPushButton does not have any setData() method. Nor if it did would there be any point setting btn->setData(btn), i.e. to itself, as you show. You cannot store "data" in a QPushButton (other than, say, properties, which I don't think you are asking for).

        I really don't know what you want to do here.

        T Offline
        T Offline
        tushu
        wrote on last edited by
        #3

        @JonB Thank you for your reply

        I have QListWidgetItem and QPushButton this way :

        Btn.PNG

        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 )

        JonBJ 1 Reply Last reply
        0
        • T tushu

          @JonB Thank you for your reply

          I have QListWidgetItem and QPushButton this way :

          Btn.PNG

          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 )

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @tushu
          That sounds a little different, and a little clearer and better :)

          So you have a bunch of QListWidgetItems, and on each one you will place a QPushButton via QListWidgetItem::setItemWidget(QPushButton), right? And then when the button is clicked you want to retrieve the data associated with the QListWidgetItem (or its QModelIndex 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 the QPushButton. 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.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved