如何更改 QComboBox 项目的高度大小? [英] How to change QComboBox items height size?

查看:74
本文介绍了如何更改 QComboBox 项目的高度大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改 QComboBox 项目的高度大小?

How to change QComboBox items height size?

我只想改变高度 - 我需要更大的高度.

I want change only height - I need it larger.

奇怪的是,没有任何用于此目的的函数.

Its strange that there are no any functions for this purposes.

推荐答案

第一个选项是设置一个新的弹出窗口,例如 QListView 并使用 Qt 样式表更改大小:

A first option is to set a new popup, for example a QListView and change the size using Qt Style Sheet:

#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();
}

另一个选项是为调整大小的弹出窗口设置一个委托:

Another option is to set a delegate to the popup that resizes:

#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();
}

这篇关于如何更改 QComboBox 项目的高度大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆