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. PyQT5 - Button to add row to a QTableView
Forum Update on Monday, May 27th 2025

PyQT5 - Button to add row to a QTableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 13.0k 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.
  • M Offline
    M Offline
    mootytootyfrooty
    wrote on 14 Apr 2018, 00:19 last edited by mootytootyfrooty
    #1

    Hello, for some reason this doesn't work for me in my app:

    # within the app's class
    def __init__(self):
        super(myApp, self).init()
        self.ui = Ui_MainWindow() # loaded with uic
        self.ui.setupUi(self)
    
        self.model = QStandardItemModel()
        self.table = QTableView()
        self.table.setModel(self.model)
        self.ui.pageLayout.addWidget(self.table)
    
        self.addRowBtn = QtWidget.QPushButton()
        self.addRowBtn.clicked.connect(self.addRowBtn_clicked)
        self.ui.pageLayout.addWidget(self.addRowBtn)
    
    
    def addRow(self, table, model):
        indices = table.selectionModel().selectedRows()
        
        # in case none selected or no table to select
        if len(indices) == 0 :
            model.insertRow(0)
        else:
             for index in sorted(indices):
                 model.insertRow(index)
    
    
    @QtCore.pyqtSlot()
    def addRowBtn_clicked(self):
       self.addRow(self.table,self.model) 
    

    I made a button for the addRow function and connected it through a pyqtslot but nothing happens when I click it. No errors either. When the program starts the table is completely blank, how do I add the first blank cell to the tableview then keep adding? I have a near identical function for adding columns, too. Very simple!

    Thank you.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Apr 2018, 21:25 last edited by
      #2

      Hi,

      Can you show how you setup your signals and slots ?

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

      M 1 Reply Last reply 15 Apr 2018, 05:53
      0
      • S SGaist
        14 Apr 2018, 21:25

        Hi,

        Can you show how you setup your signals and slots ?

        M Offline
        M Offline
        mootytootyfrooty
        wrote on 15 Apr 2018, 05:53 last edited by
        #3

        @SGaist Sure, sorry, I assumed the problem was with the function itself, updated the original with more detail.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 15 Apr 2018, 06:25 last edited by
          #4

          @mootytootyfrooty said in PyQT5 - Button to add row to a QTableView:

          insertRow

          Hi
          What model do you use for the view ?

          M 1 Reply Last reply 16 Apr 2018, 18:58
          0
          • M mrjj
            15 Apr 2018, 06:25

            @mootytootyfrooty said in PyQT5 - Button to add row to a QTableView:

            insertRow

            Hi
            What model do you use for the view ?

            M Offline
            M Offline
            mootytootyfrooty
            wrote on 16 Apr 2018, 18:58 last edited by
            #5

            @mrjj QStandardItemModel

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 16 Apr 2018, 19:19 last edited by
              #6

              Hi
              Maybe its due to python syntax but i dont see you set the items text anywhere ?
              bool QStandardItemModel::insertRow(int row, const QModelIndex & parent = QModelIndex())
              only inserts an empty row (of items)
              The other insertRow takes a list, like

              QStandardItemModel m(3,3);
              QList<QStandardItem*> newRow;
              for (int i=0;i<model.colCount();i++)
              {
                  QStandardItem* itm = new QStandardItem(QString("data for col %1").arg(i));
                  newRow.append(itm);
              }
              model.append(newRow);
              

              Where you new the items yourself and hence can give them text.

              When you use
              bool insertRow ( int row, const QModelIndex & parent = QModelIndex() )
              you need to acces the items and set the text after.

              1 Reply Last reply
              1

              1/6

              14 Apr 2018, 00:19

              • Login

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