当前位置:网站首页>Style setting when there is a separator in the qcombobox drop-down menu
Style setting when there is a separator in the qcombobox drop-down menu
2022-06-25 23:36:00 【The struggle history of polar bear】
QComboBox There is a separator in the drop-down menu Separator Style settings when
First on the renderings
The effect of using no style , Ugly 
With the effect of style , Is it what you want ?
You can't find the drop-down list in the drop-down box on the Internet , Style settings with delimiters , So I start from the source code , Finally, this effect is achieved , If you feel good , You can give me a reward !!!
Code up
Test data code
QHBoxLayout* lay = new QHBoxLayout(this);
// Create a drop-down box object for testing
QComboBox* cbo = new QComboBox(this);
lay->addWidget(cbo, 0, Qt::AlignTop | Qt::AlignHCenter);
// Add test data
cbo->addItem("the 1st data");
cbo->insertSeparator(1); // Added delimiter
cbo->addItem("the 2nd data");
cbo->addItem("the 3rd data");
cbo->insertSeparator(3);
cbo->addItem("the 4th data");
cbo->addItem("the 5th data");
cbo->addItem("the 6th data");
cbo->insertSeparator(6);
cbo->addItem("the 7th data");
cbo->addItem("the 8th data");
cbo->addItem("the 9th data");
cbo->addItem("the 10th data");
cbo->insertSeparator(10);
cbo->addItem("the 11th data");
cbo->addItem("the 12th data");
cbo->addItem("the 13th data");
cbo->addItem("the 14th data");
Style code
// First add a background color to the whole window
this->setStyleSheet(QString("QWidget#%1{background-color: #0b1127}").arg(this->objectName()));
// Start setting the style of the drop-down box
QString strStyle(
"QComboBox{ border-image:url(:/image/comboBox.png); font-family: \"Noto Sans S Chinese\"; font-size:16px; color: #ffffff; padding-left: 16px;}"
"QComboBox::drop-down{ background: transparent; } "
"QComboBox QFrame{border-image: url(:/image/cboDropFrame.png);}"
"QComboBox QAbstractItemView{ padding-left: 8px; outline:0px; color: #ffffff; font-size:14px; font-family: \"Noto Sans S Chinese\"; } "
"QComboBox QAbstractItemView::item { height: 16px; margin-top: 8px; margin-bottom:8px; } "
"QComboBox QAbstractItemView::item:selected, QAbstractItemView::item:hover { background-color: rgba(0, 188, 212, 0.4); } "
"QScrollBar::vertical{ background:transparent; margin: 0px 0px 0px 0px; width: 6px; }"
"QScrollBar::handle:vertical{ background-color:#00BCD4; border-radius:3px; }"
"QScrollBar::add-line:vertical{ height: 0px; }"
"QScrollBar::sub-line:vertical{ height: 0px; }"
"QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical{background: transparent;}"
);
cbo->setStyleSheet(strStyle);
cbo->setMinimumSize(QPixmap(":/image/comboBox.png").size()); // Modify the minimum size of the drop-down box
// The following sentence can make the drop-down menu apply the style of the style sheet
// cbo->setItemDelegate(new QStyledItemDelegate()); // But this style proxy class doesn't make separators look good , You need to inherit the style of his custom separator
cbo->setItemDelegate(new MStyledItemDelegate()); // When there is a delimiter , Use this custom style proxy
// The following two sentences can set the transparent background of the drop-down menu
cbo->view()->parentWidget()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);
cbo->view()->parentWidget()->setAttribute(Qt::WA_TranslucentBackground);
The key is coming.
Custom style proxy class
MStyledItemDelegate::MStyledItemDelegate(QObject *parent /*= 0*/) : QStyledItemDelegate(parent)
{
// Set the default color for the separator
clrSeparator = QColor("#00BCD4");
}
void MStyledItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (isSeparator(index)) {
QRect rect = option.rect;
if (const QAbstractItemView *view = qobject_cast<const QAbstractItemView*>(option.widget))
rect.setWidth(view->viewport()->width());
QStyleOption opt;
opt.rect = rect;
QPoint pt1(rect.left(), rect.top() + rect.height() / 2);
QPoint pt2(rect.right(), pt1.y());
painter->save();
painter->setPen(clrSeparator);
painter->drawLine(pt1, pt2);
painter->restore();
}
else {
QStyledItemDelegate::paint(painter, option, index);
}
}
QSize MStyledItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (isSeparator(index)) {
int pm = 1; // The height of the custom separator is 1px
return QSize(pm, pm);
}
return QStyledItemDelegate::sizeHint(option, index);
}
bool MStyledItemDelegate::isSeparator(const QModelIndex &index)
{
return index.data(Qt::AccessibleDescriptionRole).toString() == QLatin1String("separator");
}
Finish off work …
Source link
Finally, the source code is linked ,Qt5 + VS2013 Written test engineering
https://download.csdn.net/download/chenxipu123/12623816
Wechat Reward Code

边栏推荐
- Basic operator
- Ad20 learning notes I
- #23class介绍
- Why is BeanUtils not recommended?
- Comp212 distributed protocol
- How to download the software package of CDH version
- Informatics Orsay all in one 1353: expression bracket matching | Luogu p1739 expression bracket matching
- [untitled] open an item connection. If it cannot be displayed normally, Ping the IP address
- Summary of common JDBC exceptions and error solutions
- OpenJudge NOI 2.1 15:Counterfeit Dollar
猜你喜欢

The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars

UE4_UE5结合offline voice recognition插件做语音识别功能

CTS RTS RX TX in serial port flow control UART (direct communication between serial port module and MCU)

Qt 中文和英文分别使用不同的字体

指针强化与提高

Efr32bg22 ble module (low power Bluetooth communication module) at command test

CSDN添加页内跳转和页外指定段落跳转

How to use drawing comparison function in CAD

Idea FAQ collection

jdbc常见异常及错误解决办法汇总
随机推荐
头歌 第3关:使用线程锁(Lock)实现线程同步
Idea auto generator generates constructor get/set methods, etc
音频基础知识以及PCM转WAV
C1. k-LCM (easy version)-Codeforces Round #708 (Div. 2)
leetcode_ 136_ A number that appears only once
hiberate核心API/配置文件/一级缓存详解
Ad20 learning notes I
C. Fibonacci Words-April Fools Day Contest 2021
记一次beego通过go get命令后找不到bee.exe的坑
对卡巴斯基发现的一个将shellcode写入evenlog的植入物的复现
OBS-Studio-27.2.4-Full-Installer-x64.exe 下载
B. Box Fitting-CodeCraft-21 and Codeforces Round #711 (Div. 2)
#23class介绍
Multithreaded learning 1
The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars
C. Fibonacci Words-April Fools Day Contest 2021
Comp212 distributed protocol
Xinchida nd04 nd04c nrf52832 (52810) ble module (low power Bluetooth communication module) at command test
首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千
Leetcode (605) -- flower planting