Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QSS and drawComplexControl()

    General and Desktop
    2
    6
    2705
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L
      LightFractal last edited by

      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

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 notice

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • L
          LightFractal last edited by

          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?

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            What does srcWidget->style() return ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • L
              LightFractal last edited by

              [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:

              1. You have to call drawComplexControl() from a QStyleSheetStyle (and that's ok, srcWidget->style() is actually a QStyleSheetStyle).

              2. 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.

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                Why not get the combo box from your widget then ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post