当前位置:网站首页>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

边栏推荐
- 【opencv450 samples】创建图像列表yaml
- OBS-Studio-27.2.4-Full-Installer-x64. Exe Download
- When are the three tools used for interface testing?
- 记录一下Qt将少量图片输出为MP4的思路及注意事项
- CAD中图纸比较功能怎么用
- 库项目和App项目中清单文件的包名不要相同
- A. Balance the Bits--Codeforces Round #712 (Div. 1)
- CSDN原力值
- ACM. Hj16 shopping list ●●
- 【无标题】打开一个项目连接,无法正常显示时,ping一下ip
猜你喜欢

qtcreator 格式化代码

jdbc常见异常及错误解决办法汇总

Qt自定义实现的日历控件

UE4\UE5 蓝图节点Delay与Retriggerable Delay的使用与区别

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

QComboBox下拉菜单中有分隔符Separator时的样式设置

漏刻有时API接口实战开发系列(13):小鹅通云服务PHP-API二维数组传参解决方案

Beacon realizes asset management and indoor positioning based on 5.2 ultra-low power Bluetooth module efr32 (bg22ax)

24class static member

character string
随机推荐
二进制、16进制、大端小端
自定义QComboBox下拉框,右对齐显示,下拉列表滑动操作
qtcreator 格式化代码
My C language learning process
信息学奥赛一本通 1353:表达式括号匹配(stack) | 洛谷 P1739 表达式括号匹配
Hibernate architecture introduction and environment construction (very detailed)
Classic image segmentation network: UNET supports libtorch deployment reasoning [with code]
What is CDN acceleration
Leetcode(435)——无重叠区间
RepOptimizer: 其实是RepVGG2
Episode 3: thread synchronization using thread lock
How to add cartoon characters to the blog park?
golang Make a list of intervals with sequential numbers
hiberate核心API/配置文件/一级缓存详解
Kotlin空指针Bug
【无标题】打开一个项目连接,无法正常显示时,ping一下ip
Solving typeerror: Unicode objects must be encoded before hashing
Analysis on the control condition and mode of go cooperation overtime exit
hiberate实体类CURD、事务操作汇总
头歌 第3关:使用线程锁(Lock)实现线程同步