Alex Oraibi
Alex Oraibi

Reputation: 11

How to add a row in a PyQt5 in python using a button

Getting confused here after trying for hours to find what I am missing, why this code is not adding rows after hitting the 'add row' button!! if I work on adding the rows without hitting the button it works fine(using init functions) but with the button use, it does not.

import sys
from PyQt5.QtWidgets import *


class TabWidget(QDialog):
    def __init__(self):
        super().__init__()
        self.table=AccountTable()

        self.setWindowTitle('Tab Widget Application')

        tabwidget = AccountTable()
        connectButton = QPushButton('add a row')
        connectButton.clicked.connect(self.onConnectButtonClicked)
        vbox=QVBoxLayout()
        vbox.addWidget(tabwidget)
        vbox.addWidget(connectButton)
        self.setLayout(vbox)

    def onConnectButtonClicked(self):
            self.table.add_trade(2)

class AccountTable(QTableWidget):
    headerls = [
        'Capital $ Value', 'Capital-Trading $ Value', 'First-Trading $ Value', 'Second-Trading $ Value',
        'Time Elapsed',  'PNL', 'Capital $ Return', 'withdrawable $ ', 'Closing Date' ]

    def __init__(self, parent=None):
        QTableWidget.__init__(self, parent)
        self.setColumnCount(len(self.headerls))
        self.setHorizontalHeaderLabels(self.headerls)
        self.setAlternatingRowColors(True)
        self.resizeColumnsToContents()



    def add_trade(self,h):
            for i in range(h):
                row = self.rowCount()
                self.insertRow(row)


if __name__ == '__main__':
    app=QApplication(sys.argv)
    tabwidget = TabWidget()
    tabwidget.show()
    app.exec()

Upvotes: 0

Views: 430

Answers (2)

Péter Leéh
Péter Leéh

Reputation: 2119

I'm not sure this is what you're looking for. I changed some lines, mostly making tabwidget a class attribute. Here is the full code, I indicated changes at the end of the lines with # c.

import sys
from PyQt5.QtWidgets import *


class TabWidget(QDialog):
    def __init__(self):
        super().__init__()
        self.table=AccountTable()
        self.setWindowTitle('Tab Widget Application')

        self.tabwidget = AccountTable() # c
        connectButton = QPushButton('add a row')
        connectButton.clicked.connect(self.onConnectButtonClicked)
        vbox=QVBoxLayout()
        vbox.addWidget(self.tabwidget) # c
        vbox.addWidget(connectButton)
        self.setLayout(vbox)

    def onConnectButtonClicked(self):
        self.currentRowCount = self.tabwidget.rowCount() # c
        self.tabwidget.insertRow(self.currentRowCount) # c

class AccountTable(QTableWidget):
    headerls = [
        'Capital $ Value', 'Capital-Trading $ Value', 'First-Trading $ Value', 'Second-Trading $ Value',
        'Time Elapsed',  'PNL', 'Capital $ Return', 'withdrawable $ ', 'Closing Date' ]

    def __init__(self, parent=None):
        QTableWidget.__init__(self, parent)
        self.setColumnCount(len(self.headerls))
        self.setHorizontalHeaderLabels(self.headerls)
        self.setAlternatingRowColors(True)
        self.resizeColumnsToContents()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    tabwidget = TabWidget()
    tabwidget.show()
    app.exec()

Upvotes: 1

Nj Nafir
Nj Nafir

Reputation: 568

Python button click command has different aspect, when you normally using the method name it will get it but can't process or return anything from that code

connectButton.clicked.connect(self.onConnectButtonClicked)

If you use lambda it will create event related function and your method can process and return anything and also you can pass data, that time the callback will be

connectButton.clicked.connect(lambda: self.onConnectButtonClicked())

For more you can visit the site at this link

Upvotes: 0

Related Questions