3

I have a QDialog containing a QTableView, along with a custom delegate showing a QComboBox for enum types. When the row is not selected, I still want the QComboBox to be visible (I would like to avoid using QTableView::openPersistentEditor()). To do so, the custom delegate forwards the paint event to the following method:

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. Here’s a screenshot:

Application screenshot

Is it possible for drawComplexControl() to consider the style sheet?

Thanks

XDnl
  • 470
  • 3
  • 14
  • 1
    Ok, I found the solution: `drawComplexControl()` works as expected only if we call it from the `QStyleSheetStyle` subclass (casting is not needed) AND we pass an instance of the widget with that style applied (in my question `srcWidget` points to a `QTableView`, which is **wrong**. I need instead a `QComboBox` instance) – XDnl Feb 03 '14 at 21:34

2 Answers2

1

I think that the only way is to grab widget with QPixmap::grabWidget(). And to use this image in delegate. Seems, that it is not possible to do because of QSS limitation

Community
  • 1
  • 1
Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
  • Thank you, I was afraid of that conclusion. The weird thing is that the `style` variable is actually a `QStyleSheetStyle` (a private Qt class), which is responsible to draw widgets according to the style sheet (and actually it does, with normal widgets!). From here [Qt Docs](http://qt-project.org/doc/qt-4.8/stylesheet.html): _When a style sheet is active, the QStyle returned by `QWidget::style()` is a wrapper "style sheet" style, not the platform-specific style. The wrapper style ensures that any active style sheet is respected (omissis)_ I hope this will get fixed sooner or later. – XDnl Feb 03 '14 at 10:59
0

I believe that you need to use dirty hacks with casting style() to private QStyleSheetStyle

DmitryARN
  • 648
  • 3
  • 10
  • How could that help? As far as I know, `drawComplexControl()` is virtual in `QStyle` and the class inheritance is as follows: `QStyleSheetStyle` ---> `QWindowsStyle` ---> `QCommonStyle` --> `QStyle`. Moreover, I tried to include `QStyleSheetStyle.h` in my code but I got many linker errors which I wasn't able to fix. – XDnl Feb 03 '14 at 20:59