在這裏,我有我的表窗口小部件,用戶將在稍後補項目:PyQt5:填充組合框與從表中的微件
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(238, 260)
self.tableWidget = QtWidgets.QTableWidget(Form)
self.tableWidget.setGeometry(QtCore.QRect(10, 10, 221, 241))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(7)
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.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 1, item)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
這裏是我的其中包含一個組合框對話框窗口:
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)
那麼,我該如何使用Table Widget中的項目完全填充comboBox_2? 我正在考慮某種循環,但不知道如何實現它。 我已經爲對話框窗口和表格窗口小部件類創建了類。
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
QtWidgets.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("d/M/yy hh:mm")
class Masters(QtWidgets.QDialog,Ui_Form):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.setupUi(self)
的完整代碼(不包括GUI)
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
QtWidgets.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("d/M/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 Services(QtWidgets.QDialog,Ui_SForm):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.setupUi(self)
class Masters(QtWidgets.QDialog,Ui_Form):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.setupUi(self)
class Table2ListProxyModel(QtCore.QAbstractProxyModel):
def __init__(self, parent=None):
QtCore.QAbstractProxyModel.__init__(self, parent)
self.mapping = []
def setSourceModel(self, sourceModel):
sourceModel.dataChanged.connect(lambda: self.fixModel())
QtCore.QAbstractProxyModel.setSourceModel(self, sourceModel)
self.fixModel()
def mapFromSource(self, sourceIndex):
if sourceIndex.isValid():
key = self.mapping.index((sourceIndex.row(), sourceIndex.column()))
return self.index(key, 0)
return QtCore.QModelIndex()
def mapToSource(self, proxyIndex):
if proxyIndex.isValid():
r, c = self.mapping[proxyIndex.row()]
return self.sourceModel().index(r, c)
return QtCore.QModelIndex()
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.mapping)
def columnCount(self, parent=QtCore.QModelIndex()):
return 1
def index(self, row, column, parent=QtCore.QModelIndex()):
return self.createIndex(row, column)
def parent(self, child):
return QtCore.QModelIndex()
def fixModel(self):
self.layoutAboutToBeChanged.emit()
self.mapping = []
for c in range(self.sourceModel().columnCount()):
for r in range(self.sourceModel().rowCount()):
ix = self.sourceModel().index(r, c)
data = self.sourceModel().data(ix)
if data is not None and data != "":
self.mapping.append((r, c))
self.layoutChanged.emit()
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.addclient.clicked.connect(self.onAddClient)
self.uslugi.clicked.connect(self.AddService)
self.mastera.clicked.connect(self.AddMaster)
def AddService(self):
services = Services(self)
services.show()
def AddMaster(self):
masters = Masters(self)
masters.show()
def onAddClient(self):
dialog = Dialog(self)
if dialog.exec_() == QtWidgets.QDialog.Accepted:
master, service, name, contact_number, time = dialog.data()
row = int(QtCore.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, QtWidgets.QTableWidgetItem(text))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog = Dialog()
master = Masters()
proxy = Table2ListProxyModel()
proxy.setSourceModel(master.tableWidget.model())
dialog.comboBox_2.setModel(proxy)
w = MainWindow()
w.show()
sys.exit(app.exec_())
你試過我的回答嗎?它已經做到了你想要的。目前,它從*一個*表中填充兩個組合框,但可以很容易地更改爲每個表使用不同的表。另一個答案旨在做一些完全不同的事情,即用組合框填充表格中的所有項目,而不是一列。 – ekhumoro
@ekhumoro感謝您的回答!是的,我做過了。但它沒有爲我第一次解決。但我看到你編輯它,所以我現在也試着嘗試。由於某種原因,第一次組合框未填滿。也許我把它整合錯了。 – akeg