如何从qtableWidget水平头发送信号?

时间:2019-07-24 13:50:04

标签: python header pyqt5 signals qtablewidget

选择水平标题时,我想从qtableWidget中删除一行。 我尝试了以下命令,但不适用于pyqt5:

tableWidget.horizontalHeader().sectionClicked.connect(MyCallable)

实际上,Qt文档没有为qTableWidget提及此信号。因此,我正在寻找一个等效的功能,即当选择水平标题时将发送通知的功能。

下面是一个可重现的示例:

from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(350, 250)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(20, 20, 300, 200))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(2)
        self.tableWidget.setRowCount(2)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
# =============================================================================
        self.tableWidget.horizontalHeader().sectionClicked.connect(self.call)
# =============================================================================
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 200, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(_translate("MainWindow", "First name"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("MainWindow", "Surname"))
        it = QtWidgets.QTableWidgetItem("John")
        self.tableWidget.setItem(0, 0, it)
        it = QtWidgets.QTableWidgetItem("Heen")
        self.tableWidget.setItem(0, 1, it)
        it = QtWidgets.QTableWidgetItem("Mark")
        self.tableWidget.setItem(1, 0, it)
        it = QtWidgets.QTableWidgetItem("Thomson")
        self.tableWidget.setItem(1, 1, it)

    def call(self):
        print("row selected")



if __name__ == "__main__":
    import sys
    if not QtWidgets.QApplication.instance():
        app = QtWidgets.QApplication(sys.argv)
    else:
        app = QtWidgets.QApplication.instance()
    MainWindow = QtWidgets.QMainWindow()
    ui        = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()

    app.exec_()
    sys.exit(app.exec_())

0 个答案:

没有答案