qt自定义复杂的QComboBox的案例
时间: 2023-05-26 11:02:13 浏览: 118
Qt 自定义的Combobox
以下是一个自定义的复杂QComboBox案例,它有两个QTableView,可以用来选择多个项目。
首先,我们需要创建一个类来实现这个QComboBox。
```python
import os
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QComboBox, QWidget, QVBoxLayout, QHBoxLayout, QTableView, QPushButton, \
QMessageBox, QVBoxLayout, QHBoxLayout
from PyQt5 import QtCore, QtGui, QtWidgets
class ComplexComboBox(QComboBox):
def __init__(self, parent=None):
super(ComplexComboBox, self).__init__(parent)
self.data = []
self.viewOne = None
self.viewTwo = None
self.initUI()
def initUI(self):
hbox = QHBoxLayout()
hbox.setSpacing(0)
hbox.setContentsMargins(0, 0, 0, 0)
self.setLayout(hbox)
self.viewOne = QTableView(self)
self.viewOne.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.viewOne.setEditTriggers(QtWidgets.QTableView.NoEditTriggers)
self.viewOne.verticalHeader().setVisible(False)
self.viewTwo = QTableView(self)
self.viewTwo.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.viewTwo.setEditTriggers(QtWidgets.QTableView.NoEditTriggers)
self.viewTwo.verticalHeader().setVisible(False)
vbox = QVBoxLayout()
vbox.addWidget(self.viewOne)
vbox.addWidget(self.viewTwo)
groupBox = QtWidgets.QGroupBox("Choose flights")
groupBox.setLayout(vbox)
self.setView(groupBox)
self.viewOne.clicked.connect(self.onViewOneClicked)
self.viewTwo.clicked.connect(self.onViewTwoClicked)
def setData(self, data):
self.data = data
modelOne = QStandardItemModel(self.viewOne)
modelTwo = QStandardItemModel(self.viewTwo)
count = 0
for item in data:
modelOne.setItem(count, 0, QStandardItem(item[0]))
modelOne.setItem(count, 1, QStandardItem(item[1]))
count += 1
self.viewOne.setModel(modelOne)
def setChecked(self, checkList):
for idx in range(len(self.data)):
if checkList[idx] == 1:
self.viewOne.selectRow(idx)
self.viewTwo.selectRow(idx)
def getViewTwoSelectedIndices(self):
selected = self.viewTwo.selectedIndexes()
indices = []
for sel in selected:
indices.append(sel.row())
return indices
def onViewOneClicked(self, index):
self.viewTwo.selectRow(index.row())
def onViewTwoClicked(self, index):
self.viewOne.selectRow(index.row())
```
接下来,我们创建一个窗口并在其中使用这个ComboBox。
```python
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(300, 300)
self.combo = ComplexComboBox(self)
self.combo.setGeometry(50, 50, 200, 30)
self.combo.setData([('flight1', '10:00'), ('flight2', '12:30'), ('flight3', '3:00 PM'), ('flight4', '6:45 PM')])
layout = QVBoxLayout()
layout.addWidget(self.combo)
btn = QPushButton('OK', self)
btn.clicked.connect(self.showSelected)
layout.addWidget(btn)
self.setLayout(layout)
def showSelected(self):
indices = self.combo.getViewTwoSelectedIndices()
for idx in indices:
QMessageBox.information(self, 'Selected Flight', 'You have selected flight at {}\n'.format(self.combo.data[idx][1]))
```
我们通过setData方法填充ComboBox的数据。您可以看到,我们填充了一个由时间和日期组成的列表。
接下来,我们可以使用getViewTwoSelectedIndices方法来获得用户选择的项目列表。
最后,当用户单击OK按钮时,我们调用showSelected方法来显示用户选择的航班的时间。
整个程序代码如下:
```python
import os
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QComboBox, QWidget, QVBoxLayout, QHBoxLayout, QTableView, QPushButton, \
QMessageBox, QVBoxLayout, QHBoxLayout
from PyQt5 import QtCore, QtGui, QtWidgets
class ComplexComboBox(QComboBox):
def __init__(self, parent=None):
super(ComplexComboBox, self).__init__(parent)
self.data = []
self.viewOne = None
self.viewTwo = None
self.initUI()
def initUI(self):
hbox = QHBoxLayout()
hbox.setSpacing(0)
hbox.setContentsMargins(0, 0, 0, 0)
self.setLayout(hbox)
self.viewOne = QTableView(self)
self.viewOne.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.viewOne.setEditTriggers(QtWidgets.QTableView.NoEditTriggers)
self.viewOne.verticalHeader().setVisible(False)
self.viewTwo = QTableView(self)
self.viewTwo.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.viewTwo.setEditTriggers(QtWidgets.QTableView.NoEditTriggers)
self.viewTwo.verticalHeader().setVisible(False)
vbox = QVBoxLayout()
vbox.addWidget(self.viewOne)
vbox.addWidget(self.viewTwo)
groupBox = QtWidgets.QGroupBox("Choose flights")
groupBox.setLayout(vbox)
self.setView(groupBox)
self.viewOne.clicked.connect(self.onViewOneClicked)
self.viewTwo.clicked.connect(self.onViewTwoClicked)
def setData(self, data):
self.data = data
modelOne = QStandardItemModel(self.viewOne)
modelTwo = QStandardItemModel(self.viewTwo)
count = 0
for item in data:
modelOne.setItem(count, 0, QStandardItem(item[0]))
modelOne.setItem(count, 1, QStandardItem(item[1]))
count += 1
self.viewOne.setModel(modelOne)
def setChecked(self, checkList):
for idx in range(len(self.data)):
if checkList[idx] == 1:
self.viewOne.selectRow(idx)
self.viewTwo.selectRow(idx)
def getViewTwoSelectedIndices(self):
selected = self.viewTwo.selectedIndexes()
indices = []
for sel in selected:
indices.append(sel.row())
return indices
def onViewOneClicked(self, index):
self.viewTwo.selectRow(index.row())
def onViewTwoClicked(self, index):
self.viewOne.selectRow(index.row())
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(300, 300)
self.combo = ComplexComboBox(self)
self.combo.setGeometry(50, 50, 200, 30)
self.combo.setData([('flight1', '10:00'), ('flight2', '12:30'), ('flight3', '3:00 PM'), ('flight4', '6:45 PM')])
layout = QVBoxLayout()
layout.addWidget(self.combo)
btn = QPushButton('OK', self)
btn.clicked.connect(self.showSelected)
layout.addWidget(btn)
self.setLayout(layout)
def showSelected(self):
indices = self.combo.getViewTwoSelectedIndices()
for idx in indices:
QMessageBox.information(self, 'Selected Flight', 'You have selected flight at {}\n'.format(self.combo.data[idx][1]))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
```
这是一个完整的自定义QComboBox案例。您可以根据自己的需要进行修改和扩展。
阅读全文