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