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. Adding Qpush button to each row of a QTableView -Python
Forum Updated to NodeBB v4.3 + New Features

Adding Qpush button to each row of a QTableView -Python

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 3.1k 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.
  • A Offline
    A Offline
    Achab61
    wrote on last edited by
    #1

    I'm quite new in QT usage in Python and I have the following need:

    I created a form in QT Designer containing a QTableView to manage data loaded from a SQLite table.
    What I would need to achieve is to add a QPushButton beside each row of the QTableView to handle the modification request of a field on a specific row.
    I already did a similar thing using the QTableWidget but, as I understood, the QTableView manage the update of data base data according to the 'edit strategy' option.
    I'm wondering whether there's also a possibility to trigger the update in another way, such as on a QPushButton click which, in my case, should be placed beside each row of the QTableView.

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

      Hi,

      How did you do it with QTableWidget ?

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Achab61
        wrote on last edited by
        #3

        Here below the part of the code with QTableWidget (referenced as tableWidget_mat) where the QPushbutton is added (in column 3) beside each row of the QTableWidget.
        'result' contains the table loaded from a SQL db

        class IndexedButtonWidget(QPushButton):
        def init(self, text='', parent=None):
        super(QPushButton, self).init(text, parent=parent)
        self.button_row = 0
        self.button_column = 0
        self.button_text=""

        tablerow=0
        for row_number, row_data in enumerate(result):
        self.tableWidget_mat.insertRow(row_number+1)
        for column_number, data in enumerate(row_data):
        self.tableWidget_mat.setItem(row_number, column_number,QTableWidgetItem(str(data)))

                    self.btn_sell = IndexedButtonWidget('Modifica')
                    header_item = QtWidgets.QTableWidgetItem("")
                    self.btn_sell.button_row = tablerow
                    self.btn_sell.button_column = 3
                    self.btn_sell.button_text='Modifica'
                    self.btn_sell.clicked.connect(self.handleButtonClicked)
                    self.tableWidget_mat.setCellWidget(row_number,3,self.btn_sell) 
                    self.tableWidget_mat.setHorizontalHeaderItem(3, header_item)
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Why one button per row since you are calling the same slot ?

          A single save button would make more sense.

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

          1 Reply Last reply
          1
          • A Offline
            A Offline
            Achab61
            wrote on last edited by
            #5

            You're right

            1 Reply Last reply
            0

            • Login

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