PyQt5.如何根据几个参数填充表格小部件?

Posted

技术标签:

【中文标题】PyQt5.如何根据几个参数填充表格小部件?【英文标题】:PyQt5.How to fill the table widget based on several arguments? 【发布时间】:2017-10-11 10:26:26 【问题描述】:

我正在开发一个客户记录程序,该程序将用于美容院。

.

所以,我需要使用这个对话窗口来填充我的表格小部件。

并且每条记录都需要放置在准确的时间位置。那么,如何实现呢?

这是我由 QtDesigner 创建的界面和对话框代码。 表格小部件

    self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
    self.tableWidget.setGeometry(QtCore.QRect(330, 0, 521, 781))
    self.tableWidget.setObjectName("tableWidget")
    self.tableWidget.setColumnCount(5)
    self.tableWidget.setRowCount(25)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(0, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(1, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(2, item)
    item = QtWidgets.QTableWidgetItem()
    #and so on...
    MainWindow.setCentralWidget(self.centralwidget)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "Главная"))
    self.addclient.setText(_translate("MainWindow", "Записать клиента"))
    self.uslugi.setText(_translate("MainWindow", "Список услуг"))
    self.mastera.setText(_translate("MainWindow", "Список мастеров"))
    item = self.tableWidget.horizontalHeaderItem(0)
    item.setText(_translate("MainWindow", "Время"))
    item = self.tableWidget.horizontalHeaderItem(1)
    item.setText(_translate("MainWindow", "Мастер"))
    item = self.tableWidget.horizontalHeaderItem(2)
    item.setText(_translate("MainWindow", "Услуга"))
    item = self.tableWidget.horizontalHeaderItem(3)
    item.setText(_translate("MainWindow", "Имя "))
    item = self.tableWidget.horizontalHeaderItem(4)
    item.setText(_translate("MainWindow", "Номер"))
    __sortingEnabled = self.tableWidget.isSortingEnabled()
    self.tableWidget.setSortingEnabled(False)
    item = self.tableWidget.item(0, 0)
    item.setText(_translate("MainWindow", "8:00"))
    item = self.tableWidget.item(1, 0)
    item.setText(_translate("MainWindow", "8:30"))
    item = self.tableWidget.item(2, 0)
    #...
    self.tableWidget.setSortingEnabled(__sortingEnabled)

和对话框窗口

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(281, 176)
        self.comboBox = QtWidgets.QComboBox(Dialog)
        self.comboBox.setGeometry(QtCore.QRect(90, 10, 71, 22))
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox_2 = QtWidgets.QComboBox(Dialog)
        self.comboBox_2.setGeometry(QtCore.QRect(10, 10, 71, 22))
        self.comboBox_2.setObjectName("comboBox_2")
        self.comboBox_2.addItem("")
        self.dateTimeEdit = QtWidgets.QDateTimeEdit(Dialog)
        self.dateTimeEdit.setGeometry(QtCore.QRect(10, 60, 194, 22))
        self.dateTimeEdit.setObjectName("dateTimeEdit")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(10, 40, 91, 16))
        self.label_2.setObjectName("label_2")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(150, 110, 113, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.label_3 = QtWidgets.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(150, 90, 121, 16))
        self.label_3.setObjectName("label_3")
        self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
        self.lineEdit_2.setGeometry(QtCore.QRect(10, 110, 113, 20))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.label_4 = QtWidgets.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(10, 90, 111, 16))
        self.label_4.setObjectName("label_4")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(150, 140, 111, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(Dialog)
        self.pushButton_2.setGeometry(QtCore.QRect(10, 140, 111, 23))
        self.pushButton_2.setObjectName("pushButton_2")

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Новый клиент"))
        self.comboBox.setItemText(0, _translate("Dialog", "Услуга"))
        self.comboBox_2.setItemText(0, _translate("Dialog", "Мастер"))
        self.label_2.setText(_translate("Dialog", "Дата и время:"))
        self.label_3.setText(_translate("Dialog", "Контактный номер:"))
        self.label_4.setText(_translate("Dialog", "Имя клиента"))
        self.pushButton.setText(_translate("Dialog", "Добавить клиента"))
        self.pushButton_2.setText(_translate("Dialog", "Отмена записи"))

所以我需要将对话框输入字段中的信息准确地放置在表格小部件中。我将不胜感激任何帮助!很抱歉滥用 QtDesigner :D

这是一个打开对话窗口的记录客户端按钮功能

class MyDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
    super(MyDialog, self).__init__(parent)
    self.ui = Ui_Dialog()
    self.ui.setupUi(self)

class MyWin(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MyWin, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.addclient.clicked.connect(self.addclientfunction)

def addclientfunction(self):
    self.dialog = MyDialog()
    self.dialog.show()

整个 MyWidnow 类

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(860, 803)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
    self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 331, 191))
    self.calendarWidget.setObjectName("calendarWidget")
    self.addclient = QtWidgets.QPushButton(self.centralwidget)
    self.addclient.setGeometry(QtCore.QRect(0, 350, 331, 81))
    font = QtGui.QFont()
    font.setFamily("Verdana")
    font.setPointSize(10)
    self.addclient.setFont(font)
    self.addclient.setObjectName("addclient")
    self.uslugi = QtWidgets.QPushButton(self.centralwidget)
    self.uslugi.setEnabled(True)
    self.uslugi.setGeometry(QtCore.QRect(0, 270, 331, 81))
    font = QtGui.QFont()
    font.setFamily("Verdana")
    font.setPointSize(10)
    self.uslugi.setFont(font)
    self.uslugi.setObjectName("uslugi")
    self.mastera = QtWidgets.QPushButton(self.centralwidget)
    self.mastera.setGeometry(QtCore.QRect(0, 190, 331, 81))
    font = QtGui.QFont()
    font.setFamily("Verdana")
    font.setPointSize(10)
    self.mastera.setFont(font)
    self.mastera.setObjectName("mastera")
    self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
    self.tableWidget.setGeometry(QtCore.QRect(330, 0, 521, 781))
    self.tableWidget.setObjectName("tableWidget")
    self.tableWidget.setColumnCount(5)
    self.tableWidget.setRowCount(25)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(0, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(1, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(2, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(3, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(4, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(5, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(6, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(7, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(8, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(9, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(10, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(11, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(12, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(13, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(14, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(15, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(16, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(17, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(18, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(19, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(20, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(21, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(22, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(23, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setVerticalHeaderItem(24, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    font = QtGui.QFont()
    font.setStyleStrategy(QtGui.QFont.PreferAntialias)
    item.setFont(font)
    self.tableWidget.setHorizontalHeaderItem(0, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setHorizontalHeaderItem(1, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setHorizontalHeaderItem(2, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setHorizontalHeaderItem(3, item)
    item = QtWidgets.QTableWidgetItem()
    self.tableWidget.setHorizontalHeaderItem(4, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(0, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(1, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(2, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(3, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(4, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(5, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(6, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(7, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(8, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(9, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(10, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(11, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(12, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(13, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(14, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(15, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(16, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(17, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(18, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(19, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(20, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(21, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(22, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(23, 0, item)
    item = QtWidgets.QTableWidgetItem()
    item.setTextAlignment(QtCore.Qt.AlignCenter)
    self.tableWidget.setItem(24, 0, item)
    MainWindow.setCentralWidget(self.centralwidget)
    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", "Главная"))
    self.addclient.setText(_translate("MainWindow", "Записать клиента"))
    self.uslugi.setText(_translate("MainWindow", "Список услуг"))
    self.mastera.setText(_translate("MainWindow", "Список мастеров"))
    item = self.tableWidget.horizontalHeaderItem(0)
    item.setText(_translate("MainWindow", "Время"))
    item = self.tableWidget.horizontalHeaderItem(1)
    item.setText(_translate("MainWindow", "Мастер"))
    item = self.tableWidget.horizontalHeaderItem(2)
    item.setText(_translate("MainWindow", "Услуга"))
    item = self.tableWidget.horizontalHeaderItem(3)
    item.setText(_translate("MainWindow", "Имя "))
    item = self.tableWidget.horizontalHeaderItem(4)
    item.setText(_translate("MainWindow", "Номер"))
    __sortingEnabled = self.tableWidget.isSortingEnabled()
    self.tableWidget.setSortingEnabled(False)
    item = self.tableWidget.item(0, 0)
    item.setText(_translate("MainWindow", "8:00"))
    item = self.tableWidget.item(1, 0)
    item.setText(_translate("MainWindow", "8:30"))
    item = self.tableWidget.item(2, 0)
    item.setText(_translate("MainWindow", "9:00"))
    item = self.tableWidget.item(3, 0)
    item.setText(_translate("MainWindow", "9:30"))
    item = self.tableWidget.item(4, 0)
    item.setText(_translate("MainWindow", "10:00"))
    item = self.tableWidget.item(5, 0)
    item.setText(_translate("MainWindow", "10:30"))
    item = self.tableWidget.item(6, 0)
    item.setText(_translate("MainWindow", "11:00"))
    item = self.tableWidget.item(7, 0)
    item.setText(_translate("MainWindow", "11:30"))
    item = self.tableWidget.item(8, 0)
    item.setText(_translate("MainWindow", "12:00"))
    item = self.tableWidget.item(9, 0)
    item.setText(_translate("MainWindow", "12:30"))
    item = self.tableWidget.item(10, 0)
    item.setText(_translate("MainWindow", "13:00"))
    item = self.tableWidget.item(11, 0)
    item.setText(_translate("MainWindow", "13:30"))
    item = self.tableWidget.item(12, 0)
    item.setText(_translate("MainWindow", "14:00"))
    item = self.tableWidget.item(13, 0)
    item.setText(_translate("MainWindow", "14:30"))
    item = self.tableWidget.item(14, 0)
    item.setText(_translate("MainWindow", "15:00"))
    item = self.tableWidget.item(15, 0)
    item.setText(_translate("MainWindow", "15:30"))
    item = self.tableWidget.item(16, 0)
    item.setText(_translate("MainWindow", "16:00"))
    item = self.tableWidget.item(17, 0)
    item.setText(_translate("MainWindow", "16:30"))
    item = self.tableWidget.item(18, 0)
    item.setText(_translate("MainWindow", "17:00"))
    item = self.tableWidget.item(19, 0)
    item.setText(_translate("MainWindow", "17:30"))
    item = self.tableWidget.item(20, 0)
    item.setText(_translate("MainWindow", "18:00"))
    item = self.tableWidget.item(21, 0)
    item.setText(_translate("MainWindow", "18:30"))
    item = self.tableWidget.item(22, 0)
    item.setText(_translate("MainWindow", "19:00"))
    item = self.tableWidget.item(23, 0)
    item.setText(_translate("MainWindow", "19:30"))
    item = self.tableWidget.item(24, 0)
    item.setText(_translate("MainWindow", "20:00"))
    self.tableWidget.setSortingEnabled(__sortingEnabled)

【问题讨论】:

对话窗口应该用哪个按钮打开? 你可以放置整个 MainWindow_UI 小部件 录制客户端按钮,但我已经编码了。 我想执行你的代码,但是我很难重建你的代码,请把它完整 添加了完整的 MyWindow 类 【参考方案1】:

如果你是初学者,不建议修改设计的代码,所以最好创建新的类来实现逻辑并使用设计,这将是Dialog和MainWindow。

对话框: 为了轻松处理一些任务,我们将 addClient 和 cancel 按钮连接到接受和拒绝槽,以便​​我们可以以非模态方式使用对话框,并使用 exec_() 如果接受则返回一个代码,如果接受则返回另一个代码取消。必须创建一个收集输入信息的方法。

主窗口: 单击的 Record Client 信号连接到显示对话框并收集对话框数据的插槽。创建一个逻辑来获取正确的行并添加元素。

class Dialog(QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        self.setupUi(self)
        # cancel
        self.pushButton_2.clicked.connect(self.reject)
        # accept
        self.pushButton.clicked.connect(self.accept)
        self.dateTimeEdit.setDisplayFormat("M/d/yy hh:mm")

    def data(self):
        master = self.comboBox_2.currentText()
        service = self.comboBox.currentText()
        name = self.lineEdit_2.text()
        contact_number = self.lineEdit.text()
        time = self.dateTimeEdit.time()
        return master, service, name, contact_number, time

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.addclient.clicked.connect(self.onAddClient)

    def onAddClient(self):
        dialog = Dialog(self)
        if dialog.exec_() == QDialog.Accepted:
            master, service, name, contact_number, time = dialog.data()
            row = int(QTime(8, 0).secsTo(time)/(30*60))
            if row >= 0:
                for k, text in enumerate([master, service, name, contact_number]):
                    self.tableWidget.setItem(row, k+1, QTableWidgetItem(text))

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

输出:

【讨论】:

应该有前缀QtWidgets。在名称 QDialog、QMainWindow 之前。完美运行,谢谢! 有多种导入方式。我使用:from PyQt5.QtWidgets import QDialog, QMainWindow:P 如果你想像管理页面一样管理表格,我建议使用 QTableView,将信息保存在数据库中,使用 QSqlTableModel。 哦,是的,我以其他方式导入了它,这就是为什么出现错误,您的方式确实更方便:D 看这个例子:doc.qt.io/qt-5/qtwidgets-itemviews-addressbook-example.html

以上是关于PyQt5.如何根据几个参数填充表格小部件?的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5 - 在表格小部件的列中创建一个检查项,即使行数与旋转框不同

PyQt5,让用户调整小部件的大小

从 Qt 中的文本文件填充表格小部件

PyQt5 QWidget不填充父

在 PyQt5/Pyside2 中动态添加小部件到流布局

如何在 PyQt5 中用另一个小部件替换一个小部件