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. Connecting QPushButton to QListWidget
Qt 6.11 is out! See what's new in the release blog

Connecting QPushButton to QListWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 1.3k 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.
  • W Offline
    W Offline
    walle19
    wrote on last edited by walle19
    #1

    Hi All,

    Platform: Linux Kubuntu 14
    QT version: 4.7.1

    I have a QListWidget and want to perform the following:
    After pressing (single click) on an element on the list (it will be highlighted) I want to press on a QPushButton that is connected to it via signal slot mechanism, and the slot will perform a specific task on the selected element.

    I tried achieving this with the following:

    connect(open_element_button, SIGNAL(clicked(QModelIndex)),this,SLOT(onOpenElementButtonPushed(QModelIndex)));
    

    Where open_element_button is QPushButton
    but it didn't work.
    I'm pretty sure the SIGNAL(clicked(QModelIndex)) part isn't accurate.

    Any suggestions ?
    Thanks!

    JonBJ 1 Reply Last reply
    0
    • W walle19

      Hi All,

      Platform: Linux Kubuntu 14
      QT version: 4.7.1

      I have a QListWidget and want to perform the following:
      After pressing (single click) on an element on the list (it will be highlighted) I want to press on a QPushButton that is connected to it via signal slot mechanism, and the slot will perform a specific task on the selected element.

      I tried achieving this with the following:

      connect(open_element_button, SIGNAL(clicked(QModelIndex)),this,SLOT(onOpenElementButtonPushed(QModelIndex)));
      

      Where open_element_button is QPushButton
      but it didn't work.
      I'm pretty sure the SIGNAL(clicked(QModelIndex)) part isn't accurate.

      Any suggestions ?
      Thanks!

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

      @walle19 said in Connecting QPushButton to QListWidget:

      Any suggestions ?

      Yes: start by changing over to new style signals & slots before you do anything else.

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Clicked has no such parameter so you can stop that. Retrieve the selected item in the slot connected to the clicked signal.

        Other suggestion: urgently update your distribution and Qt version. Qt 4.7.1 is way more than outdated. If you are stuck to Qt 4 then at least update to the last release of the series (which has also reached end of life a long time ago).

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        W 1 Reply Last reply
        2
        • SGaistS SGaist

          Hi,

          Clicked has no such parameter so you can stop that. Retrieve the selected item in the slot connected to the clicked signal.

          Other suggestion: urgently update your distribution and Qt version. Qt 4.7.1 is way more than outdated. If you are stuck to Qt 4 then at least update to the last release of the series (which has also reached end of life a long time ago).

          W Offline
          W Offline
          walle19
          wrote on last edited by
          #4

          @SGaist Thanks for the reply.
          After retrieving the selected item, is there a way to emit a signal that it has been double clicked ?
          My goal is to provide a QPushButton that will perform the functionality of double clicking an item in the QListWidget.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Well, you should rather have a slot connected to the QListWidget::itemDoubleClicked signal and then call the same function from the slot connect to your QPushButton::clicked signal.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            W 1 Reply Last reply
            1
            • SGaistS SGaist

              Well, you should rather have a slot connected to the QListWidget::itemDoubleClicked signal and then call the same function from the slot connect to your QPushButton::clicked signal.

              W Offline
              W Offline
              walle19
              wrote on last edited by
              #6

              @SGaist Yes that was what I was going for.
              Now consider I am using QAbstractItemView::doubleClicked(const QModelIndex & index).
              I looked for a way to get the QModelIndex in my QPushButton SLOT().
              So initially I thought I will call QListWidget::currentItem() and then QListWidget::indexFromItem(QListWidgetItem * item).
              This would be ideal but that last method is protected and I cannot call it.

              Any idea ?

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @walle19 said in Connecting QPushButton to QListWidget:

                Any idea ?

                QListWidget has a signal QListWidget::itemDoubleClicked(QListWidgetItem*).

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                W 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @walle19 said in Connecting QPushButton to QListWidget:

                  Any idea ?

                  QListWidget has a signal QListWidget::itemDoubleClicked(QListWidgetItem*).

                  W Offline
                  W Offline
                  walle19
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher Thanks for your reply. This method is good,
                  But Is there a way to retrieve the QModelIndex of the QListWidgetItem ?

                  eyllanescE 1 Reply Last reply
                  0
                  • W walle19

                    @Christian-Ehrlicher Thanks for your reply. This method is good,
                    But Is there a way to retrieve the QModelIndex of the QListWidgetItem ?

                    eyllanescE Offline
                    eyllanescE Offline
                    eyllanesc
                    wrote on last edited by
                    #9

                    @walle19 Use indexFromItem() method: QModelIndex index = listwidget->indexFromItem(item);

                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                    W 1 Reply Last reply
                    1
                    • eyllanescE eyllanesc

                      @walle19 Use indexFromItem() method: QModelIndex index = listwidget->indexFromItem(item);

                      W Offline
                      W Offline
                      walle19
                      wrote on last edited by
                      #10

                      @eyllanesc isn't this a protected method ?
                      I am getting a compile error when compiling it.

                      ......./Qt-4.7.1/include/QtGui/qlistwidget.h:287:17: error: ‘QModelIndex QListWidget::indexFromItem(QListWidgetItem*) const’ is protected
                           QModelIndex indexFromItem(QListWidgetItem *item) const
                      
                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Why do you need the index at all? Either use a QListWidget and work only with QListWidgetItems or use a QListView and use an own model but do not mix both.
                        Also why are you using Qt4?

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        W 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          Why do you need the index at all? Either use a QListWidget and work only with QListWidgetItems or use a QListView and use an own model but do not mix both.
                          Also why are you using Qt4?

                          W Offline
                          W Offline
                          walle19
                          wrote on last edited by
                          #12

                          @Christian-Ehrlicher Hi,
                          Using QT4 is a constraint.
                          I need to use index as the chosen row number for a already defined SLOT() that gets a QModelIndex as argument when doubleClicked(QModelIndex) SIGNAL() is sent.
                          For now I use a workaround - In the QPushButton SLOT() I pass the QAbstractItem::doubleClicked(QModelIndex) SLOT() a non valid QModelIndex.
                          I check the validity - if valid I use index.row(), if not I call QListWidget::currentRow().

                          Thanks for all the help

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Then use the other signal from the QListWidget which gives you a QListWidgetItem. As I said - mixing them is cumbersome - either use the convenience model or an own but don't mix it.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            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