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. Get current application QStyle
Forum Updated to NodeBB v4.3 + New Features

Get current application QStyle

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.8k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    Sixshaman
    wrote on last edited by
    #1

    Suppose I have a custom application style:

    class MyStyle: public QProxyStyle
    {
    public:
        MyStyle(QStyle* style);
       ~MyStyle();
    
        void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const Q_DECL_OVERRIDE;
    }
    

    Now I set the style for the whole application:

    qApp->setStyle(new MyStyle());
    

    I need a way to get it back. Unfortunately, QApplication::style() doesn't return the style I created, meaning the returned copy is not an instance of MyStyle:

    const QStyle* style = qApp->style();
    const MyStyle* myStyle = dynamic_cast<const MyStyle*>(style); //Returns nullptr
    

    I've tried using QStyle::proxy, but it didn't work either. It seems the only way to get current application QStyle is storing a pointer somewhere. But probably I'm missing something. Is there a way to do it without dirty hacks?

    JonBJ 1 Reply Last reply
    0
    • S Sixshaman

      Suppose I have a custom application style:

      class MyStyle: public QProxyStyle
      {
      public:
          MyStyle(QStyle* style);
         ~MyStyle();
      
          void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const Q_DECL_OVERRIDE;
      }
      

      Now I set the style for the whole application:

      qApp->setStyle(new MyStyle());
      

      I need a way to get it back. Unfortunately, QApplication::style() doesn't return the style I created, meaning the returned copy is not an instance of MyStyle:

      const QStyle* style = qApp->style();
      const MyStyle* myStyle = dynamic_cast<const MyStyle*>(style); //Returns nullptr
      

      I've tried using QStyle::proxy, but it didn't work either. It seems the only way to get current application QStyle is storing a pointer somewhere. But probably I'm missing something. Is there a way to do it without dirty hacks?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Sixshaman
      Not sure, but do you have to use the static method and not the instance (qApp->style()) one?

      http://doc.qt.io/qt-5/qapplication.html#style
      QStyle *QApplication::style() [static]

      S 1 Reply Last reply
      0
      • JonBJ JonB

        @Sixshaman
        Not sure, but do you have to use the static method and not the instance (qApp->style()) one?

        http://doc.qt.io/qt-5/qapplication.html#style
        QStyle *QApplication::style() [static]

        S Offline
        S Offline
        Sixshaman
        wrote on last edited by
        #3

        @JonB
        The method is static anyway, but I use the qApp->style() form.

        JonBJ 1 Reply Last reply
        0
        • S Sixshaman

          @JonB
          The method is static anyway, but I use the qApp->style() form.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Sixshaman
          OK, I'm not a C++-er. Can't do that in "nice" languages :) OOI, what does qDebug() << QApplication::style()->metaObject()->className(); give you?

          S 1 Reply Last reply
          0
          • JonBJ JonB

            @Sixshaman
            OK, I'm not a C++-er. Can't do that in "nice" languages :) OOI, what does qDebug() << QApplication::style()->metaObject()->className(); give you?

            S Offline
            S Offline
            Sixshaman
            wrote on last edited by Sixshaman
            #5

            @JonB
            Oh. It outputs QStyleSheetStyle.

            You see, we use both stylesheets and QStyle's. Stylesheets are nessessary if you need a bigger palette for elements, but it's impossible to change the entire appearance of complex controls using stylesheets.

            It seems that QStyleSheetStyle always overrides QProxyStyle and it doesn't matter if I call qApp->setStyleSheet(styleSheet); before or after qApp->setStyle(myStyle);.

            So probably the only way to solve my case is storing the pointer.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Sixshaman said in Get current application QStyle:

              const MyStyle* myStyle = dynamic_cast<const MyStyle*>(style); //Returns nullptr

              Does

              MyStyle* myStyle = qobject_cast<MyStyle*>(style);
              also return null ?

              S 1 Reply Last reply
              0
              • mrjjM mrjj

                @Sixshaman said in Get current application QStyle:

                const MyStyle* myStyle = dynamic_cast<const MyStyle*>(style); //Returns nullptr

                Does

                MyStyle* myStyle = qobject_cast<MyStyle*>(style);
                also return null ?

                S Offline
                S Offline
                Sixshaman
                wrote on last edited by
                #7

                @mrjj
                Yes.

                mrjjM 1 Reply Last reply
                0
                • S Sixshaman

                  @mrjj
                  Yes.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Sixshaman
                  Thats odd as if i modify the
                  NorwegianWoodStyle example like

                  void WidgetGallery::changeStyle(const QString &styleName)
                  //! [5] //! [6]
                  {
                      if (styleName == "NorwegianWood") {
                          QApplication::setStyle(new NorwegianWoodStyle);
                          NorwegianWoodStyle *mine = qobject_cast<NorwegianWoodStyle *>(QApplication::style());        
                  
                      } else {
                          QApplication::setStyle(QStyleFactory::create(styleName));
                      }
                      changePalette();
                  }
                  
                  

                  It does report the expected type and cast do not fail.

                  1 Reply Last reply
                  0

                  • Login

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