【问题标题】:How to draw a native checkmark without the QCheckBox box如何在没有 QCheckBox 框的情况下绘制本机复选标记
【发布时间】:2015-07-31 08:48:27
【问题描述】:

我想在 Qt 中绘制一个类似于 QCheckBoxes 中使用的本机复选标记的复选标记。我还希望能够像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 );

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

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

那么有没有办法只绘制QCheckBox 的复选标记而不使用该框?

【问题讨论】:

  • 你通过什么风格选项?这很重要。您需要查看您正在使用的样式的源代码,并查看原始绘图实现需要哪些选项。我至少希望看到合适的rectstate
  • @KubaOber:state 包含QStyle::State_Enabled 标志和State_OnState_OffState_NoChange 标志之一。我确实没有初始化rect。当将真正的QRect 设置为rect 时,我确实看到了前两个drawPrimitive 调用的一些内容,但它们绘制了完整的QCheckBox 而不仅仅是复选标记。
  • 深入了解样式代码。该平台很可能只为整个控件提供图像,而不仅仅是复选标记。 OTOH,取决于平台,复选标记可能只是某种字体的字形。那是什么平台,具体是什么风格?
  • 尝试在 Qt 代码中调试以了解这部分代码的工作原理。

标签: c++ qt qt4


【解决方案1】:

我知道这是一篇旧帖子,但对于其他人来说,这样做就像在 QCheckBox's 指示器上设置样式表一样简单。

用这样的一行设置它的样式表:

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

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

【讨论】:

    猜你喜欢
    • 2018-05-29
    • 1970-01-01
    • 2012-09-16
    • 2013-01-30
    • 1970-01-01
    • 2010-09-23
    • 2015-05-13
    • 2020-02-26
    • 2018-12-23
    相关资源
    最近更新 更多