Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved PyQT5 - Button to add row to a QTableView

    General and Desktop
    3
    6
    11380
    Loading More Posts
    • 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
      mootytootyfrooty last edited by mootytootyfrooty

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • M
          mootytootyfrooty @SGaist last edited by

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

          1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion last edited by

            @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 Reply Quote 0
            • M
              mootytootyfrooty @mrjj last edited by

              @mrjj QStandardItemModel

              1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion last edited by

                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 Reply Quote 1
                • First post
                  Last post