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 add separate pushbutton for each item in listWidget ?
Forum Updated to NodeBB v4.3 + New Features

how to add separate pushbutton for each item in listWidget ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 940 Views 1 Watching
  • 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
    TM9412
    wrote on last edited by TM9412
    #1

    Hi ,

    I want to show multiple item in in QT UI and each item has push button.
    How to show this ?

    QT UI

    item 1 PushButton1
    item 2 PushButton2
    item 3 PushButton3
    item 4 PushButton4

    I know from listWidget i can shows the iteam but how to add push button inside it ?
    how to add separate pushbutton for each item in listWidget ?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      What is listWidget which you refer to? There is no such (built-in) QML component.

      There are so many ways to do what you ask that I don't know how to answer. Are the items dynamic? Are they loaded from a model? Or is this a static (known in advance) part of the UI?

      A very simple answer (but as said - I'm only guessing what you want to achieve...):

      ListView {
        model: 4
        delegate: Rectangle {
          border.width: 1
          border.color: "#000000"
          width: 100
          height: 30
          Text {
            text: qsTr("Item %1").arg(index)
          }
          PushButton {
            text: qsTr("PushButton %1").arg(index)
            onClicked: console.log("Button clicked", index)
          }
        }
      }
      

      (Z(:^

      T 1 Reply Last reply
      5
      • sierdzioS sierdzio

        What is listWidget which you refer to? There is no such (built-in) QML component.

        There are so many ways to do what you ask that I don't know how to answer. Are the items dynamic? Are they loaded from a model? Or is this a static (known in advance) part of the UI?

        A very simple answer (but as said - I'm only guessing what you want to achieve...):

        ListView {
          model: 4
          delegate: Rectangle {
            border.width: 1
            border.color: "#000000"
            width: 100
            height: 30
            Text {
              text: qsTr("Item %1").arg(index)
            }
            PushButton {
              text: qsTr("PushButton %1").arg(index)
              onClicked: console.log("Button clicked", index)
            }
          }
        }
        
        T Offline
        T Offline
        TM9412
        wrote on last edited by
        #3

        @sierdzio

        this is the Qlistwidget item in QT, not in QML
        https://doc.qt.io/archives/qt-4.8/qlistwidgetitem.html

        yes this item are dynamic,
        this is the sample code but here i want clicked button in place of checkbox.

                 for (int i = 0; i < 14; ++i) {
        
        QListWidgetItem *item = new QListWidgetItem;
        item->setText("app " + QString::number(i) + " version " + QString::number(i));
        item->setSelected(false);
        item->setCheckState(Qt::Unchecked);
        
        ui->listWidget->addItem(item);
        
                 }
        
        J.HilkJ sierdzioS 2 Replies Last reply
        0
        • T TM9412

          @sierdzio

          this is the Qlistwidget item in QT, not in QML
          https://doc.qt.io/archives/qt-4.8/qlistwidgetitem.html

          yes this item are dynamic,
          this is the sample code but here i want clicked button in place of checkbox.

                   for (int i = 0; i < 14; ++i) {
          
          QListWidgetItem *item = new QListWidgetItem;
          item->setText("app " + QString::number(i) + " version " + QString::number(i));
          item->setSelected(false);
          item->setCheckState(Qt::Unchecked);
          
          ui->listWidget->addItem(item);
          
                   }
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          @TM9412 said in how to add separate pushbutton for each item in listWidget ?:

          this is the Qlistwidget item in QT, not in QML

          Than, you were in the wrong sub forum, therefore I moved it from QML/Quick to General.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          3
          • T TM9412

            @sierdzio

            this is the Qlistwidget item in QT, not in QML
            https://doc.qt.io/archives/qt-4.8/qlistwidgetitem.html

            yes this item are dynamic,
            this is the sample code but here i want clicked button in place of checkbox.

                     for (int i = 0; i < 14; ++i) {
            
            QListWidgetItem *item = new QListWidgetItem;
            item->setText("app " + QString::number(i) + " version " + QString::number(i));
            item->setSelected(false);
            item->setCheckState(Qt::Unchecked);
            
            ui->listWidget->addItem(item);
            
                     }
            
            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            @TM9412 said in how to add separate pushbutton for each item in listWidget ?:

            @sierdzio

            this is the Qlistwidget item in QT, not in QML
            https://doc.qt.io/archives/qt-4.8/qlistwidgetitem.html

            yes this item are dynamic,
            this is the sample code but here i want clicked button in place of checkbox.

                     for (int i = 0; i < 14; ++i) {
            
            QListWidgetItem *item = new QListWidgetItem;
            item->setText("app " + QString::number(i) + " version " + QString::number(i));
            item->setSelected(false);
            item->setCheckState(Qt::Unchecked);
            
            ui->listWidget->addItem(item);
            
                     }
            

            You can switch to QListView and use your own styled delegate: https://doc.qt.io/qt-5/qstyleditemdelegate.html. And example how to do it (with QSpinBox) is here: https://doc.qt.io/qt-5/model-view-programming.html#delegate-classes

            (Z(:^

            1 Reply Last reply
            5

            • Login

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