如何更改QComboBox项目的高度大小?
How to change QComboBox items height size?
如何更改QComboBox项目的高度大小?
我只想改变高度 - 我需要它更大。
奇怪的是,没有任何用于此目的的功能。
第一个选项是设置一个新的弹出窗口,例如QListView并使用Qt样式表更改大小:
#include <QApplication>
#include <QComboBox>
#include <QListView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QComboBox combo;
QListView *view = new QListView(&combo);
view->setStyleSheet("QListView::item{height: 100px}");
combo.setView(view);
combo.addItems({"A", "B", "C", "D", "E", "F"});
combo.show();
return a.exec();
}
另一种选择是将委托设置为调整大小的弹出窗口:
#include <QApplication>
#include <QComboBox>
#include <QStyledItemDelegate>
#include <QAbstractItemView>
class PopupItemDelegate: public QStyledItemDelegate
{
public:
using QStyledItemDelegate::QStyledItemDelegate;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
QSize s = QStyledItemDelegate::sizeHint(option, index);
s.setHeight(60);
return s;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QComboBox combo;
combo.view()->setItemDelegate(new PopupItemDelegate(&combo));
combo.addItems({"A", "B", "C", "D", "E", "F"});
combo.show();
return a.exec();
}
您可以通过setView
方法控制高度并QSS
。
self.comboBox.setView(QtWidgets.QListView())
QSS
QListView::item {
height: 30px;
}
示例代码:
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
class MainWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.__ui__()
self.__style__()
def __ui__(self):
self.layout = QtWidgets.QVBoxLayout()
self.comboBox = QtWidgets.QComboBox()
self.comboBox.setView(QtWidgets.QListView())
self.comboBox.addItems(["one", "too", "three", "four", "five", "six"])
self.layout.addWidget(self.comboBox)
self.setLayout(self.layout)
def __style__(self):
self.comboBox.setStyleSheet("QListView::item {height:30px;}")
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = MainWidget()
widget.show()
sys.exit(app.exec_())
相关文章:
- 如何在Qt C++中更改光标
- 为什么++(*p)更改指针值
- 如何通过派生类函数更改基类中的向量
- 函数何时会在c++中包含stack_Unwind_Resume调用
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- TDateTime格式在C++Builder中不会更改
- 查找后更改类变量
- 为什么我无法更改"set<set>"循环中的值<int>
- Xaudio2在更改缓冲区或循环时弹出声音
- Python中的for循环与C++有何不同
- 如何在Qbutton的帮助下更改Q对话框的宽度
- 在更改for循环的第三部分后,未使用for循环结果
- C++.我想将更改后的特定字符行保存在字符串中
- 如何更改QComboBox项目的高度大小?
- 更改高度贴图,使其在 4x4 网格上显示 16 个 hieghtmap
- 如何在 MDI MFC C++ Windows 应用程序中动态更改 CFormView 宽度或高度?
- C++ OpenSceneGraph 更改相机眼睛高度
- Win32 C++-检查窗口位置X/PositionY和宽度/高度是否已更改
- 确定TMenuItem中文本的高度以决定通过TMenuItem.OnMeasureItem更改MenuItem的高度
- 添加图标时,CMFCStatusBar会更改高度