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

时间:2017-10-11 10:26:27

标签: python pyqt pyqt5 qtablewidget qdialog

我正在制作一个客户记录程序,将用于美容院。

Main interface

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

每条记录都需要放在准确的时间位置。那么,它怎么能实现呢?

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

res1.say("Whatever!")

和对话窗口

    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)

所以我需要在Dialog的输入字段中将inftomation放置在表格小部件中的确切时间内。我会感激任何帮助!对不起QtDesigner滥用:D

这是一个Record客户端按钮功能,用于打开对话框窗口

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", "Отмена записи"))

整个MyWidnow课程

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()

1 个答案:

答案 0 :(得分: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_())

输出:

enter image description here

enter image description here