繁体   English   中英

如何在没有QCheckBox框的情况下绘制本机复选标记

[英]How to draw a native checkmark without the QCheckBox box

我想在Qt中绘制一个复选标记,看起来像QCheckBox es中使用的本机复选标记。 我还希望能够像QCheckBox一样绘制部分检查的状态。 总结:我想画一个QCheckBox的内容,但不要画这个盒子。 我正在Qt 4.8中工作。

我尝试通过这些调用绘制原始元素的复选标记:

// Draws nothing (or nothing that I can see).
QStyleOption styleOption;
styleOption.state |= QStyle::State_Enabled;
styleOption.state |= QStyle::State_On;
QApplication::style()->drawPrimitive( QStyle::PE_IndicatorItemViewItemCheck, &styleOption, &painter );

// Also draws nothing.
QApplication::style()->drawPrimitive( QStyle::PE_IndicatorViewItemCheck, &styleOption, &painter );

当我使用以下调用时,我确实看到了图形,但是有一个完整的QCheckBox ,它周围是不需要的“框”。

// Draws the complete QCheckBox.
QStyleOptionButton styleOptionButton;
styleOptionButton.state |= QStyle::State_Enabled;
styleOptionButton.state |= QStyle::State_On;
QApplication::style()->drawControl( QStyle::CE_CheckBox, &styleOptionButton, &painter );

// Also draws the complete QCheckBox.
QStyleOptionButton styleOptionButton2;
QCheckBox dummy;
styleOptionButton2.initFrom( &dummy );
QApplication::style()->drawPrimitive( QStyle::PE_IndicatorCheckBox, &styleOptionButton2, inPainter );

// Also draws the complete QCheckBox.
QStyleOption styleOption;
styleOption.state |= QStyle::State_Enabled;
styleOption.state |= QStyle::State_On;
styleOption.rect = QRect( 0, 0, 16, 16 );
QApplication::style()->drawPrimitive( QStyle::PE_IndicatorItemViewItemCheck, &styleOption, &painter );

当我使用以下调用来绘制其他原始元素时,它们确实会被绘制,但不是QCheckBox使用的复选标记(它们很丑陋)。

// Draws an ugly check mark.
QStyleOption styleOption;
styleOption.state |= QStyle::State_Enabled;
QApplication::style()->drawPrimitive( QStyle::PE_IndicatorMenuCheckMark, &styleOption, &painter );

那么有没有办法只画一个没有框的QCheckBox的复选标记呢?

我知道这是一篇过时的文章,但是对于任何其他这样做的人,就像在QCheckBox's指示器上设置样式表一样简单。

用如下行设置其样式表:

QCheckBox box;
box.setStyleSheet(QString("QCheckBox::indicator{ border: rgba(0, 0, 0, 0); }"));

这样做会将边界指示器设置为透明。 您也可以将选中/悬停的样式表属性覆盖为自定义选中标记或任何自定义图标。 将这些效果结合在一起可以产生非常现代的前端。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM