qt create 自定义一个QComboBox
时间: 2023-09-03 20:08:42 浏览: 51
要自定义一个QComboBox,可以继承QComboBox类并重写需要的方法。以下是一个简单的例子:
```cpp
class MyComboBox : public QComboBox
{
public:
MyComboBox(QWidget *parent = nullptr) : QComboBox(parent) {}
protected:
virtual void showPopup() override
{
// Do something before showing the popup
QComboBox::showPopup();
// Do something after showing the popup
}
};
```
在这个例子中,我们继承了QComboBox,并重写了showPopup()方法,在弹出下拉框之前和之后都做了一些事情。
然后,你可以像使用普通的QComboBox一样使用自定义的MyComboBox类。例如:
```cpp
MyComboBox *comboBox = new MyComboBox();
comboBox->addItem("Item 1");
comboBox->addItem("Item 2");
// ...
```
当你调用comboBox->showPopup()时,它将会调用MyComboBox中重写的showPopup()方法。
相关问题
qt自定义复杂的QComboBox的案例
以下是一个自定义的复杂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案例。您可以根据自己的需要进行修改和扩展。
qt create 使用QComboBox举例
QComboBox是一个常用的Qt控件,它提供了一个下拉框供用户选择。
以下是一个简单的例子,演示如何使用QComboBox:
```cpp
#include <QComboBox>
#include <QVBoxLayout>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget();
QComboBox *comboBox = new QComboBox();
comboBox->addItem("Item 1");
comboBox->addItem("Item 2");
comboBox->addItem("Item 3");
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(comboBox);
window->setLayout(layout);
window->show();
return app.exec();
}
```
在这个例子中,我们创建了一个窗口,然后创建了一个QComboBox并添加了三个选项。我们使用QVBoxLayout将QComboBox添加到窗口中,并显示窗口。
当用户选择一个选项时,可以使用当前选项的索引来获取选项的值:
```cpp
int currentIndex = comboBox->currentIndex();
QString currentItemText = comboBox->currentText();
```
currentIndex返回当前选中项的索引,currentText返回当前选中项的文本。