简体   繁体   English

Qt:QSS和drawComplexControl()

[英]Qt: QSS and drawComplexControl()

I have a QDialog containing a QTableView, along with a custom delegate showing a QComboBox for enum types. 我有一个包含QTableView的QDialog以及一个显示枚举类型的QComboBox的自定义委托。 When the row is not selected, I still want the QComboBox to be visible (I would like to avoid using QTableView::openPersistentEditor()). 当未选择该行时,我仍然希望QComboBox可见(我想避免使用QTableView :: openPersistentEditor())。 To do so, the custom delegate forwards the paint event to the following method: 为此,自定义委托将paint事件转发到以下方法:

QStyleOptionViewItem &option) const
{
    painter->save();

    QStyleOptionComboBox comboBoxOption;
    comboBoxOption.rect = option.rect;
    comboBoxOption.state = option.state;
    comboBoxOption.state |= QStyle::State_Enabled;
    comboBoxOption.editable = false;
    comboBoxOption.currentText = enumInfo.valueToKey(curValue);

    // The cast is successful, and srcWidget is the QTableView
    QWidget *srcWidget = qobject_cast<QWidget *>(option.styleObject);

    // style->metaObject()->className() = QStyleSheetStyle
    QStyle *style = srcWidget ? srcWidget->style() : QApplication::style();

    // However, the QSS is ignored here (while srcWidget->styleSheet() correctly
    // returns the style I've set in Qt Designer)
    style->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter, srcWidget);
    style->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter, srcWidget);

    painter->restore();
}

The problem is that I've styled the combo box control using QSS, but drawComplexControl() seems to ignore that, despite using the QTableView's style. 问题是我已经使用QSS设置了组合框控件的样式,但是尽管使用了QTableView的样式,但drawComplexControl()似乎忽略了这一点。 Here's a screenshot: 这是屏幕截图:

应用截图

Is it possible for drawComplexControl() to consider the style sheet? drawComplexControl()是否可以考虑样式表?

Thanks 谢谢

I think that the only way is to grab widget with QPixmap::grabWidget(). 我认为唯一的方法是使用QPixmap :: grabWidget()来获取小部件。 And to use this image in delegate. 并在委托中使用此图像。 Seems, that it is not possible to do because of QSS limitation 似乎由于QSS限制而无法执行

我相信您需要使用带有将style()转换为私有QStyleSheetStyle的肮脏技巧

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

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