Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSS and drawComplexControl()
QtWS25 Last Chance

QSS and drawComplexControl()

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.2k Views
  • 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 Offline
    L Offline
    LightFractal
    wrote on 2 Feb 2014, 17:00 last edited by
    #1

    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
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Feb 2014, 21:11 last edited by
      #2

      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
      0
      • L Offline
        L Offline
        LightFractal
        wrote on 2 Feb 2014, 21:14 last edited by
        #3

        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
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 3 Feb 2014, 21:44 last edited by
          #4

          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
          0
          • L Offline
            L Offline
            LightFractal
            wrote on 3 Feb 2014, 21:53 last edited by
            #5

            [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
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 3 Feb 2014, 22:24 last edited by
              #6

              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
              0
              • J JonB referenced this topic on 20 Mar 2024, 19:28

              5/6

              3 Feb 2014, 21:53

              • Login

              • Login or register to search.
              5 out of 6
              • First post
                5/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved