关于 QComboBox 和 QCompleter 中的 QListView 的行高问题

在 qss 中可以设置 QComboBox 子项的行高,前提是需要在代码中设置

combox1_->setView(new QListView());

在 qss 中设置

QComboBox QAbstractItemView {
    background-color: white;
    border: 1px solid #d2d2d2;
    color: #111111;
    border-radius: 0;    
}

QComboBox QAbstractItemView::item {
    min-height: 30px;
}

但是 QComboBox 设置的 QCompleter 的 QListView 却不能通过 qss 设置行高,即使通过

completer_->setPopup(new QListView());

设置也不行,只能设置 QListView 的样式,却不能设置 QListView::item 的样式

 completer_->popup()->setStyleSheet("QListView{background: rgb(35,35,35);color:white; font-size: 12pt; font-family: Calibri Light; font-weight:normal}"        
     );

这样改 QCompleter 的行高需要写一个 QItemDelegate

class ComboBoxDelegate : public QItemDelegate
{
public:
    explicit ComboBoxDelegate(QObject * parent = nullptr) : QItemDelegate(parent) {}
    ~ComboBoxDelegate() = default;

    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        QSize size = QItemDelegate::sizeHint(option, index);
        size.setHeight(qMax(40, size.height()));

        return size;
    }
};

然后设置

QAbstractItemView* view = completer_->popup();
view->setItemDelegate(new ComboBoxDelegate());

完整代码

#if defined (_MSC_VER) && (_MSC_VER >=1600)
#pragma execution_character_set("utf-8")
#endif


#include "widget.h"
#include <QComboBox>
#include <QItemDelegate>
#include <QCompleter>
#include <QLineEdit>
#include <QAbstractItemView>
#include <QScrollBar>
#include <QListView>

class ComboBoxDelegate : public QItemDelegate
{
public:
    explicit ComboBoxDelegate(QObject * parent = nullptr) : QItemDelegate(parent) {}
    ~ComboBoxDelegate() = default;

    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        QSize size = QItemDelegate::sizeHint(option, index);
        size.setHeight(qMax(40, size.height()));

        return size;
    }
};

Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
    setWindowTitle(tr("QComboBox-01"));   

    layout_ = new QVBoxLayout;

    auto combox_1 = new QComboBox;   
    combox_1->setEditable(true);
    combox_1->lineEdit()->setPlaceholderText(tr("选择或搜索"));
    combox_1->setItemDelegate(new ComboBoxDelegate());

    QStringList item_list_;

    for (int i = 0; i < 10; ++i) {        
        combox_1->addItem(QString("test_%1").arg(i));
        item_list_ << QString("test_%1").arg(i);
    }

    auto completer_ = new QCompleter(item_list_, this);   
    completer_->setCompletionColumn(combox_1->modelColumn());
    completer_->setCompletionMode(QCompleter::PopupCompletion);
    completer_->setCaseSensitivity(Qt::CaseInsensitive);
    completer_->setFilterMode(Qt::MatchContains);
    //completer_->setPopup(new QListView());
    completer_->popup()->verticalScrollBar()->setStyleSheet(
        "QScrollBar:vertical {background: rgb(35,35,35);width: 15px;margin: 20px 0px 20px 0px;}"
        "QScrollBar::handle:vertical{background: rgb(100,100,100); width: 20px;}"
        "QScrollBar::add-line:vertical{background: rgb(100,100,100); height: 15px; subcontrol-position: bottom;subcontrol-origin: margin;}"
        "QScrollBar::sub-line:vertical {background: rgb(100,100,100);height: 15px;subcontrol-position: top;subcontrol-origin: margin;}"
        "QScrollBar:down-arrow:vertical {width: 10px;height: 10px;background: rgb(100,100,100);image: url(:/images/images/downArrow.png);}"
        "QScrollBar:up-arrow:vertical {width: 10px;height: 10px;background: rgb(100,100,100);image: url(:/images/images/upArrow.png);}"
        "QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {background: none;}");
    completer_->popup()->setStyleSheet("QListView{background: rgb(35,35,35);color:white; font-size: 12pt; font-family: Calibri Light; font-weight:normal}"        
     );

    QAbstractItemView* view = completer_->popup();
    view->setItemDelegate(new ComboBoxDelegate());

    combox_1->setCompleter(completer_);
    combox_1->setCurrentIndex(-1);
    combox_1->setInsertPolicy(QComboBox::NoInsert);
 

    layout_->addWidget(combox_1);
    

    setLayout(layout_);
}

推荐阅读更多精彩内容

  • 寂寞的云 你仰望天空, 只为那一抹蓝, 却不曾在意 那片片白云 不经意间 已追寻风的脚步 飘向远方 我也曾想 驻...
    只愿为你等待阅读 43评论 0 0
  • 无论世界多么大,「关系」这个词都足够概括我们所拥有的除物质以外的一切。在其中摸爬滚打够久了会发现,面对关系,我们依...
    9b8505fb9e4c阅读 37评论 0 0
  • 1.安装第三方包 setting -> Project XXX -> 点击pip,我当时出现的列表为空,然后去查了...
    无愠无殇阅读 111评论 0 0
  • 不知道你是否有这样的经历:打开一本书,然后去倒杯水,可是水太热了,等凉凉再喝,然后刷一会朋友圈,刷一会淘宝,等水凉...
    丸子爱肉阅读 76评论 0 0