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. Add a button in the rows of a QTableWidget
QtWS25 Last Chance

Add a button in the rows of a QTableWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 6.1k 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.
  • A Offline
    A Offline
    Andrea R.
    wrote on 16 Nov 2022, 14:37 last edited by
    #1

    I created a table in a window with Qt Designer.
    Unfortunately I can't insert a button in a cell (in the whole column).
    I've read several solutions, but when I apply and try them the window crashes without returning errors.
    What can I try?
    Thank you

    J 1 Reply Last reply 16 Nov 2022, 14:48
    0
    • A Andrea R.
      16 Nov 2022, 14:37

      I created a table in a window with Qt Designer.
      Unfortunately I can't insert a button in a cell (in the whole column).
      I've read several solutions, but when I apply and try them the window crashes without returning errors.
      What can I try?
      Thank you

      J Offline
      J Offline
      JonB
      wrote on 16 Nov 2022, 14:48 last edited by
      #2

      @Andrea-R
      Hello and welcome.

      We don't know what you might have tried! Did you call void QTableWidget::setCellWidget(int row, int column, QWidget *widget) passing it a newed QPushButton? Although there are reasons why this approach is not great, it should work for you at least starting out.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Andrea R.
        wrote on 16 Nov 2022, 15:13 last edited by
        #3

        I tried this way for column '6' without success

                for e in range(len(elencosel)):
                    n = elencosel[e][6]
                    cc = elencosel[e][2]
                    nf = elencosel[e][0]
                    sc = elencosel[e][9].strftime("%d/%m/%Y")
                    im = elencosel[e][14]
                    sim += im
                    cr = elencosel[e][13]
                    scr += cr
        
                    rowPosition = window.tablew.rowCount()
                    window.tablew.insertRow(rowPosition)
                    window.tablew.setItem(rowPosition, 0, QtWidgets.QTableWidgetItem(n))
                    window.tablew.setItem(rowPosition, 1, QtWidgets.QTableWidgetItem(cc))
                    window.tablew.setItem(rowPosition, 2, QtWidgets.QTableWidgetItem(nf))
                    window.tablew.setItem(rowPosition, 3, QtWidgets.QTableWidgetItem(sc))
                    window.tablew.setItem(rowPosition, 4, QtWidgets.QTableWidgetItem(str(im)))
                    window.tablew.setItem(rowPosition, 5, QtWidgets.QTableWidgetItem(str(cr)))
        
                    window.pb_note = QPushButton(window.tablew)
                    window.pb_note.setText('...')
                    window.tablew.setCellWidget(rowPosition,6,window.pb_note)
        
        
        
                    chkBoxItem = QTableWidgetItem()
                    chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
                    chkBoxItem.setCheckState(QtCore.Qt.Checked)
                    window.tablew.setItem(rowPosition, 7, chkBoxItem)
        
        J 1 Reply Last reply 16 Nov 2022, 15:18
        0
        • A Andrea R.
          16 Nov 2022, 15:13

          I tried this way for column '6' without success

                  for e in range(len(elencosel)):
                      n = elencosel[e][6]
                      cc = elencosel[e][2]
                      nf = elencosel[e][0]
                      sc = elencosel[e][9].strftime("%d/%m/%Y")
                      im = elencosel[e][14]
                      sim += im
                      cr = elencosel[e][13]
                      scr += cr
          
                      rowPosition = window.tablew.rowCount()
                      window.tablew.insertRow(rowPosition)
                      window.tablew.setItem(rowPosition, 0, QtWidgets.QTableWidgetItem(n))
                      window.tablew.setItem(rowPosition, 1, QtWidgets.QTableWidgetItem(cc))
                      window.tablew.setItem(rowPosition, 2, QtWidgets.QTableWidgetItem(nf))
                      window.tablew.setItem(rowPosition, 3, QtWidgets.QTableWidgetItem(sc))
                      window.tablew.setItem(rowPosition, 4, QtWidgets.QTableWidgetItem(str(im)))
                      window.tablew.setItem(rowPosition, 5, QtWidgets.QTableWidgetItem(str(cr)))
          
                      window.pb_note = QPushButton(window.tablew)
                      window.pb_note.setText('...')
                      window.tablew.setCellWidget(rowPosition,6,window.pb_note)
          
          
          
                      chkBoxItem = QTableWidgetItem()
                      chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
                      chkBoxItem.setCheckState(QtCore.Qt.Checked)
                      window.tablew.setItem(rowPosition, 7, chkBoxItem)
          
          J Offline
          J Offline
          JonB
          wrote on 16 Nov 2022, 15:18 last edited by JonB
          #4

          @Andrea-R
          I have never used setCellWidget() so I am not sure, but does it require you to have put a QTableWidgetItem into the cell before you can add a widget, else the cell is not shown? So

          window.tablew.setItem(rowPosition, 6, QtWidgets.QTableWidgetItem())  # add this line
          ...
          window.tablew.setCellWidget(rowPosition,6,window.pb_note)
          

          ?

          Hmm, don't know if that is actually required. If it makes no difference, try reducing your code down to the bare minimum, because setCellWidget() should certainly work....

          S 1 Reply Last reply 17 Nov 2022, 08:08
          0
          • A Offline
            A Offline
            Andrea R.
            wrote on 16 Nov 2022, 15:29 last edited by
            #5

            @JonB said in Add a button in the rows of a QTableWidget:

            window.tablew.setItem(rowPosition, 6, QtWidgets.QTableWidgetItem())

            No changes.
            From DEBUG I read: 'PyQt5.uic.parser:new top widget None'

            I created the window with the designer and I import it with uic.
            Maybe this is the problem?

            J 1 Reply Last reply 16 Nov 2022, 17:48
            0
            • A Andrea R.
              16 Nov 2022, 15:29

              @JonB said in Add a button in the rows of a QTableWidget:

              window.tablew.setItem(rowPosition, 6, QtWidgets.QTableWidgetItem())

              No changes.
              From DEBUG I read: 'PyQt5.uic.parser:new top widget None'

              I created the window with the designer and I import it with uic.
              Maybe this is the problem?

              J Offline
              J Offline
              JonB
              wrote on 16 Nov 2022, 17:48 last edited by
              #6

              @Andrea-R
              Assuming you mean that the non-pushbutton cells in the table show their items OK, but you see nothing (? no button) in the button column, then your code should be fine no matter whether you are using Designer/uic or not.

              I don't yet know what your problem is. You could try changing your window.pb_note = QPushButton(window.tablew) to remove the window.tablew parameter. I don't think it will solve but worth checking.

              I will knock up a standalone PyQt5 for this tomorrow if I have a chance.

              1 Reply Last reply
              0
              • J JonB
                16 Nov 2022, 15:18

                @Andrea-R
                I have never used setCellWidget() so I am not sure, but does it require you to have put a QTableWidgetItem into the cell before you can add a widget, else the cell is not shown? So

                window.tablew.setItem(rowPosition, 6, QtWidgets.QTableWidgetItem())  # add this line
                ...
                window.tablew.setCellWidget(rowPosition,6,window.pb_note)
                

                ?

                Hmm, don't know if that is actually required. If it makes no difference, try reducing your code down to the bare minimum, because setCellWidget() should certainly work....

                S Offline
                S Offline
                SimonSchroeder
                wrote on 17 Nov 2022, 08:08 last edited by
                #7

                @JonB said in Add a button in the rows of a QTableWidget:

                I have never used setCellWidget() so I am not sure, but does it require you to have put a QTableWidgetItem into the cell before you can add a widget, else the cell is not shown?

                No, don't do that! You should have either a table widget item or a cell widget inside one cell. Not sure what happens otherwise.

                Looking at your code I am not sure what the problem is. However, there is one question: How many columns does your table widget have? Is it (at least) 8? Otherwise adding the button and the checkbox will not work if there is not column for them.

                A 1 Reply Last reply 17 Nov 2022, 09:45
                0
                • S SimonSchroeder
                  17 Nov 2022, 08:08

                  @JonB said in Add a button in the rows of a QTableWidget:

                  I have never used setCellWidget() so I am not sure, but does it require you to have put a QTableWidgetItem into the cell before you can add a widget, else the cell is not shown?

                  No, don't do that! You should have either a table widget item or a cell widget inside one cell. Not sure what happens otherwise.

                  Looking at your code I am not sure what the problem is. However, there is one question: How many columns does your table widget have? Is it (at least) 8? Otherwise adding the button and the checkbox will not work if there is not column for them.

                  A Offline
                  A Offline
                  Andrea R.
                  wrote on 17 Nov 2022, 09:45 last edited by
                  #8

                  @SimonSchroeder

                  yes, 8 columns.

                  @JonB

                  it just crashes ('Process finished with exit code -1073741819 (0xC0000005))

                  I worked around the problem with a double click action on the row, but I would still like to insert the button, at least to understand where is the problem...😅

                  I will try in a clean minimum project if it works...

                  J 2 Replies Last reply 17 Nov 2022, 10:09
                  0
                  • A Andrea R.
                    17 Nov 2022, 09:45

                    @SimonSchroeder

                    yes, 8 columns.

                    @JonB

                    it just crashes ('Process finished with exit code -1073741819 (0xC0000005))

                    I worked around the problem with a double click action on the row, but I would still like to insert the button, at least to understand where is the problem...😅

                    I will try in a clean minimum project if it works...

                    J Offline
                    J Offline
                    JonB
                    wrote on 17 Nov 2022, 10:09 last edited by
                    #9

                    @Andrea-R
                    I have to go out now. I was planning to give a go at writing a minimal to show it working, for you to test at your site, in a couple of hours' time.

                    1 Reply Last reply
                    0
                    • A Andrea R.
                      17 Nov 2022, 09:45

                      @SimonSchroeder

                      yes, 8 columns.

                      @JonB

                      it just crashes ('Process finished with exit code -1073741819 (0xC0000005))

                      I worked around the problem with a double click action on the row, but I would still like to insert the button, at least to understand where is the problem...😅

                      I will try in a clean minimum project if it works...

                      J Offline
                      J Offline
                      JonB
                      wrote on 17 Nov 2022, 17:09 last edited by JonB
                      #10

                      @Andrea-R
                      Sorry, was busy....

                      The following works fine for me. PyQt 5.15.6, Qt 5.15.3, Ubuntu 22.04.

                      Screenshot from 2022-11-17 17-07-17.png

                      import sys
                      from PyQt5 import QtWidgets
                      
                      if __name__ == '__main__':
                          app = QtWidgets.QApplication(sys.argv)
                      
                          tw = QtWidgets.QTableWidget()
                          tw.setColumnCount(3)
                          for row in range(3):
                              rowPosition = tw.rowCount()
                              tw.insertRow(rowPosition)
                              tw.setItem(rowPosition, 0, QtWidgets.QTableWidgetItem("col #0"))
                              pb = QtWidgets.QPushButton()
                              pb.setText("Pushbutton")
                              tw.setCellWidget(rowPosition, 1, pb)
                              tw.setItem(rowPosition, 2, QtWidgets.QTableWidgetItem("col #2"))
                          tw.show()
                      
                          sys.exit(app.exec_())
                      

                      You should copy & paste to confirm it works for you.

                      One possible thought about your code:

                                  window.pb_note = QPushButton(window.tablew)
                                  window.pb_note.setText('...')
                                  window.tablew.setCellWidget(rowPosition,6,window.pb_note)
                      

                      You will be doing this multiple times. You don't want to store in window.pb_note, it will get replaced each time by Python and there is a a small chance it might actually be disposing your previous buttons. In any case it's not useful. Replace with a local variable pb_note:

                                  pb_note = QPushButton(window.tablew)
                                  pb_note.setText('...')
                                  window.tablew.setCellWidget(rowPosition,6,pb_note)
                      

                      But I'd be surprised if that's the issue.

                      1 Reply Last reply
                      3

                      2/10

                      16 Nov 2022, 14:48

                      topic:navigator.unread, 8
                      • Login

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