qt5.7中怎么让qcombobox
时间: 2024-03-01 11:56:29 浏览: 84
在 Qt5.7 中,可以通过设置 QComboBox 的属性来实现一些常用的功能。例如,如果想要设置 QComboBox 的下拉框中的文本居中显示,可以使用以下代码:
```
comboBox->view()->setTextElideMode(Qt::ElideNone);
comboBox->view()->setTextAlignment(Qt::AlignCenter);
```
如果想要设置 QComboBox 的下拉框中的项的高度为 30px,可以使用以下代码:
```
comboBox->view()->setFixedHeight(30);
```
如果想要在 QComboBox 的下拉框中添加一些项,可以使用以下代码:
```
comboBox->addItem("Item 1");
comboBox->addItem("Item 2");
comboBox->addItem("Item 3");
```
如果想要获取当前选择的项的文本,可以使用以下代码:
```
QString text = comboBox->currentText();
```
希望这些代码可以帮助你实现 QComboBox 的常用功能。
相关问题
qt5.7中怎么让qcombobox实现一开始就往上展开
在 Qt5.7 中,如果想要让 QComboBox 在一开始就往上展开,可以使用以下代码:
```
comboBox->view()->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
comboBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
comboBox->setMaxVisibleItems(4);
comboBox->showPopup();
```
其中,`setVerticalScrollMode` 函数设置滚动模式为像素级滚动,`setVerticalScrollBarPolicy` 函数设置滚动条为一直显示,`setMaxVisibleItems` 函数设置最大可见项数为 4,`showPopup` 函数将下拉框展开。
使用以上代码,可以让 QComboBox 在一开始就往上展开。
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案例。您可以根据自己的需要进行修改和扩展。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)