QSS and drawComplexControl()
-
Hi!
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:
@
void CustomEnum::paint(QPainter *painter, const 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);
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:
[img]http://i.imgur.com/IC62DBz.jpg[/img]
Is it possible for drawComplexControl() to consider the style sheet?
Thanks
-
Hi,
AFAIK, widget using style sheet doesn't use the application style but the QStyleSheetStyle which is a private class.
You can use use it but beware that it can change and even be removed without noticeHope it helps
-
Shouldn't the style variabile in my code point to a QStyleSheetStyle already?
After all, it's the QTableView which has the stylesheet property set (from Qt Designer).EDIT: Indeed the following code
@
const char *clsName = style->metaObject()->className();
@yields "QStyleSheetStyle"! Why is the styling being ignored?
-
What does srcWidget->style() return ?
-
[quote author="SGaist" date="1391463873"]What does srcWidget->style() return ?[/quote]
srcWidget points to the QTableView, which has the styleSheet property set via Qt Designer.
srcWidget->style() returns a QStyle instance and srcWidget->style()->metaObject()->className() yields "QStyleSheetStyle".However, I have some news: I found that drawComplexControl actually works, but only if two conditions are met, namely:
-
You have to call drawComplexControl() from a QStyleSheetStyle (and that's ok, srcWidget->style() is actually a QStyleSheetStyle).
-
You have to pass an instance of the same widget type you want to draw as the last parameter of drawComplexControl(). In my example, I pass srcWidget which is a QTableView. Apparently, Qt doesn't like that. In order to work, I need to pass a QComboBox instance!
With this snippet, the code works:
@
QWidget *srcWidget = qobject_cast<QWidget *>(option.styleObject);
// Important! We need to set the combobox as child of an already layouted widget, otherwise the code will not work
QComboBox *cbo = new QComboBox(srcWidget);
QStyle *style = srcWidget->style();
style->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter, cbo);
style->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter, cbo);
delete cbo;
@Of course, this solution is horrible beyond words (repeatedly creating a widget inside a paint event!).
So it seems that my problem is shifted towards providing a suitable instance to drawComplexControl().Any ideas would be appreciated.
-
-
Why not get the combo box from your widget then ?
-