I SubClassed QComboBox以便对其进行自定义。在开始定制之前,我编写了以下paintEvent()方法来测试样式表是否被正确应用。但是,如果我使用此paintEvent,则不会显示所选项目(已经选择的项目或我新选择的项目)。
class MyComboBox: public QComboBox
{
Q_OBJECT
public:
MyComboBox(QWidget *parent = 0): QComboBox(parent){}
virtual void paintEvent(QPaintEvent* pEvent)
{
QStylePainter painter(this);
QStyleOptionComboBox opt;
initStyleOption(&opt);
painter.drawComplexControl(QStyle::CC_ComboBox, opt);
QVariant itemData = this->itemData( this->currentIndex(), Qt::DisplayRole);
if(!itemData.isNull() && qVariantCanConvert<QPen>(itemData))
style()->drawItemText(&painter,this->rect(),Qt::AlignLeft,(this->palette()), true, itemData.toString());
}
};如何修复paintEvent?另外,paintEvent上是否有针对不同Qt控件的文档?
发布于 2012-10-03 17:31:01
我认为你这里的问题是你没有在你的派生类中首先调用它就覆盖了基类方法。您应该在派生类方法中调用它。
https://stackoverflow.com/questions/12703679
复制相似问题