首页 > 解决方案 > 当表格中的组合框发生变化时如何发出信号?

问题描述

每当文本发生任何变化时,我如何发出信号(或现在打印文本) QComboBoxQComboBox每次将一行添加到 a 时,都会在两列中创建一个新的QTableWidget。最终,该信号将触发绘图的重绘。

现在,我可以从QComboBox已经存在的具有特定实例名称的文本中打印文本,但是当在表中动态创建组合框时,我不知道该怎么做。

主要代码:

import sys

from PyQt5.QtWidgets import (
    QMainWindow,
    QApplication,
    QComboBox,
)

from cb_in_tables import Ui_MainWindow


class MyWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.comboBox.addItems(['', '1', '2', '3'])

        self.ui.pushButton.clicked.connect(self.addRow)
        self.ui.comboBox.currentTextChanged.connect(self.printComboBoxText)

    def printComboBoxText(self, text):
        print(text)

    def addRow(self):
        """Add new row to data table and populate with widgets."""
        # Insert row
        rowPos = self.ui.tableWidget.rowCount()
        self.ui.tableWidget.insertRow(rowPos)

        x_combobox = QComboBox()
        x_combobox.addItems(['', 'A', 'B', 'C'])
        self.ui.tableWidget.setCellWidget(rowPos, 0, x_combobox)

        y_combobox = QComboBox()
        y_combobox.addItems(['', 'X', 'Y', 'Z'])
        self.ui.tableWidget.setCellWidget(rowPos, 1, y_combobox)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = MyWindow()
    main.show()
    sys.exit(app.exec_())

界面文件:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'cb_in_tables.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(291, 299)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(30, 60, 231, 161))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(2)
        self.tableWidget.setRowCount(0)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(30, 20, 113, 32))
        self.pushButton.setObjectName("pushButton")
        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(160, 20, 104, 31))
        self.comboBox.setObjectName("comboBox")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 291, 24))
        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", "X"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("MainWindow", "Y"))
        self.pushButton.setText(_translate("MainWindow", "Add Row"))


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

标签: pythonpython-3.xpyqtpyqt5

解决方案


没有什么变化:

x_combobox.currentTextChanged.connect(self.printComboBoxText)
y_combobox.currentTextChanged.connect(self.printComboBoxText)

推荐阅读