简体   繁体   English

PyQt5 Qtablewidget并连接到按钮

[英]PyQt5 Qtablewidget and connecting to buttons

I have created a Qtablewidget as a class, and add_button to add rows, a delete_button to remove rows from table down upwards. 我创建了一个Qtablewidget作为类,并使用add_button添加行,使用delete_button从表中向下删除行。 I would like to connect functions to buttons, but it doesn't work correctly. 我想将功能连接到按钮,但是不能正常工作。 I have used getattr method to call the function, still does not work. 我已使用getattr方法调用该函数,但仍然无法正常工作。

The table 桌子

在此处输入图片说明

to explain more, those scriptlines are giving attributte errors. 为了进一步说明,这些脚本行给出了属性错误。 when they are called by button.clicked.connect method. 当它们由button.clicked.connect方法调用时。

add_button.clicked.connect(self._addrow)
delete_button.clicked.connect(self._removeItem)

The script is as below: 脚本如下:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets, Qt


class loadtable(QtWidgets.QTableWidget):
    def __init__(self, parent=None):
        super(loadtable, self).__init__(parent)

        self.setColumnCount(5)
        self.setRowCount(1)
        self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))   
        headertitle = ("A","B","C","D","E")
        self.setHorizontalHeaderLabels(headertitle)
        self.verticalHeader().setVisible(False)
        self.horizontalHeader().setHighlightSections(False)
        self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
        self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
        self.setColumnWidth(0, 130)
        combox_lay = QtWidgets.QComboBox(self)
        combox_lay.addItems(["I","II"])
        self.setCellWidget(0, 4, combox_lay)


        self.cellChanged.connect(self._cellclicked)
        #self.cellChanged.connect(self._addrow)
        #self.cellDoubleClicked.connect(self._removerow)

    def _cellclicked(self):
        self.value = self.currentItem()
        self.value.setTextAlignment(Qt.AlignCenter)
        #if self.value is not None:
           #return self.value.setTextAlignment(Qt.AlignCenter)        

    def _addrow(self):
        rowcount = self.rowCount()
        print(rowcount)
        self.setRowCount(rowcount+1)
        combox_add = QtWidgets.QComboBox(self)
        combox_add.addItems(["I","II"])
        self.setCellWidget(rowcount, 4, combox_add)

    def _removerow(self):
        self.removeRow(1)


class thirdtabloads(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(thirdtabloads, self).__init__(parent)      
        table = loadtable()


        button_layout = QtWidgets.QVBoxLayout()
        add_button = QtWidgets.QPushButton("Add")
        add_button.clicked.connect(self._addrow)
        delete_button = QtWidgets.QPushButton("Delete")
        delete_button.clicked.connect(self._removeItem)
        button_layout.addWidget(add_button, alignment=QtCore.Qt.AlignBottom)
        button_layout.addWidget(delete_button, alignment=QtCore.Qt.AlignTop)


        tablehbox = QtWidgets.QHBoxLayout()
        tablehbox.setContentsMargins(10,10,10,10)
        tablehbox.addWidget(table)

        grid = QtWidgets.QGridLayout(self)
        grid.addLayout(button_layout, 0, 1)
        grid.addLayout(tablehbox, 0, 0)        



if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = thirdtabloads()
    w.show()
    sys.exit(app.exec_())

To add a row you must use insertRow() , to remove the last row use removeRow() and pass the last row and remember that the numbering starts from 0 so the last row is self.rowCount() - 1. 要添加行,您必须使用insertRow()来删除最后一行,使用removeRow()并传递最后一行,并记住编号从0开始,所以最后一行是self.rowCount() - 1.

On the other hand, your connection is incorrect, I ask you: Who owns the slot _addrow() and _removerow() ? 另一方面,您的连接不正确,我问您: 谁拥有_addrow()_removerow()插槽? belongs to LoadTable, so to access them we need an object of that class, ie table._addrow and table._removerow . 属于LoadTable,因此要访问它们,我们需要该类的对象,即table._addrowtable._removerow

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class LoadTable(QtWidgets.QTableWidget):
    def __init__(self, parent=None):
        super(LoadTable, self).__init__(1, 5, parent)
        self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))   
        headertitle = ("A","B","C","D","E")
        self.setHorizontalHeaderLabels(headertitle)
        self.verticalHeader().hide()
        self.horizontalHeader().setHighlightSections(False)
        self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)

        self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
        self.setColumnWidth(0, 130)

        combox_lay = QtWidgets.QComboBox(self)
        combox_lay.addItems(["I","II"])
        self.setCellWidget(0, 4, combox_lay)

        self.cellChanged.connect(self._cellclicked)

    @QtCore.pyqtSlot(int, int)
    def _cellclicked(self, r, c):
        it = self.item(r, c)
        it.setTextAlignment(QtCore.Qt.AlignCenter)        

    @QtCore.pyqtSlot()
    def _addrow(self):
        rowcount = self.rowCount()
        self.insertRow(rowcount)
        combox_add = QtWidgets.QComboBox(self)
        combox_add.addItems(["I","II"])
        self.setCellWidget(rowcount, 4, combox_add)

    @QtCore.pyqtSlot()
    def _removerow(self):
        if self.rowCount() > 0:
            self.removeRow(self.rowCount()-1)


class ThirdTabLoads(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(ThirdTabLoads, self).__init__(parent)    

        table = LoadTable()

        add_button = QtWidgets.QPushButton("Add")
        add_button.clicked.connect(table._addrow)

        delete_button = QtWidgets.QPushButton("Delete")
        delete_button.clicked.connect(table._removerow)

        button_layout = QtWidgets.QVBoxLayout()
        button_layout.addWidget(add_button, alignment=QtCore.Qt.AlignBottom)
        button_layout.addWidget(delete_button, alignment=QtCore.Qt.AlignTop)


        tablehbox = QtWidgets.QHBoxLayout()
        tablehbox.setContentsMargins(10, 10, 10, 10)
        tablehbox.addWidget(table)

        grid = QtWidgets.QGridLayout(self)
        grid.addLayout(button_layout, 0, 1)
        grid.addLayout(tablehbox, 0, 0)        


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = ThirdTabLoads()
    w.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM