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. Empty style sheet and *.ui
QtWS25 Last Chance

Empty style sheet and *.ui

Scheduled Pinned Locked Moved General and Desktop
14 Posts 3 Posters 6.4k 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.
  • P Offline
    P Offline
    p-himik
    wrote on last edited by
    #1

    Some of the windows of my application must have a header widget. And this widget's style sheet is constant.

    I'm trying to accomplish this by creating the class named HeaderWidget, adding a form to it (single widget) and setting style sheet on it.
    Then i redefine paintEvent to draw style sheet.
    Next i create widgets on all relevant windows and promote them to HeaderWidget. And it works fine.

    When i change promoted widget's style sheet it, of course, redefines HeaderWidget's style sheet. But! When i erasing promoted widget's style sheet back to empty uic.exe doesn't just remove the "widget->setStyleSheet("...");" line but replaces "..." with "". And thus still redefines HeaderWidget's style sheet.

    Can it be considered as a bug? Or what should i do to accomplish this task? BTW, i really don't want to create custom widget plugin for Qt Creator since.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p-himik
      wrote on last edited by
      #2

      And additional question.
      When i add a style sheet like this:
      @w->setStyleSheet( "background-color: red;" );@
      not only invoking widget w will have red background but also every child of it.
      Can i somehow mark lines in the style sheet that they are relevant only to widget which invokes setStyleSheet() (so only w will have red background, no matter what object name it has)?

      1 Reply Last reply
      0
      • P Offline
        P Offline
        p-himik
        wrote on last edited by
        #3

        The ugliest solution ever.

        @HeaderWidget::HeaderWidget( QWidget* parent ) :
        QWidget( parent ),
        ui( new Ui::HeaderWidget )
        {
        ui->setupUi( this );
        }

        void HeaderWidget::setStyleSheet( const QString& styleSheet )
        {
        static QString ss;
        static QString defaultSS;
        if( defaultSS.isEmpty() )
        {
        HeaderWidget tmp;
        ui->setupUi( &tmp );
        defaultSS = tmp.styleSheet();
        }
        ss = styleSheet.isEmpty() ? defaultSS : styleSheet;
        ss.replace( "HeaderWidget", objectName() );
        QWidget::setStyleSheet( ss );
        }

        void HeaderWidget::setObjectName( const QString& objectName )
        {
        YB_DEBUG_FUNC_BEGIN;
        QWidget::setObjectName( objectName );
        setStyleSheet( "" );
        }@

        I have a feeling that i will burn in hell for this.

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          bq. not only invoking widget w will have red background but also every child of it.

          Have a look at the" stylesheet Reference and examples":http://doc.qt.nokia.com/4.7/stylesheet-examples.html. You can even use the name of your widget to fine tune it.

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • P Offline
            P Offline
            p-himik
            wrote on last edited by
            #5

            If you mean this:
            @HeaderWidget
            {
            background-color: red;
            }@
            This doesn't work.
            And if by "name of your widget" you mean objectName() this won't work too since this problem appers not only for headers (which are, of course, single on every window) but for example for address boxes too (and window can contain many of them).

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

              [quote author="p-himik" date="1316849264"]The ugliest solution ever.
              ...
              I have a feeling that i will burn in hell for this.[/quote]

              You will, because neither QObject::setObjectName() nor QWidget::setStyleSheet() is declared virtual, which means that both method implementations will be skipped when called through base class pointers.
              [quote author="p-himik" date="1316853767"]If you mean this:
              @HeaderWidget
              {
              background-color: red;
              }@
              This doesn't work.
              [/quote]
              But it should.
              [quote author="p-himik" date="1316846718"]Can i somehow mark lines in the style sheet that they are relevant only to widget which invokes setStyleSheet() (so only w will have red background, no matter what object name it has)?[/quote]

              No, but you can use a "property selector":http://doc.qt.nokia.com/latest/stylesheet-syntax.html#selector-types to select single widget objects of the same type.
              @
              HeaderWidget
              {
              ...
              }
              HeaderWidget[type="redBackgroundType"]
              {
              background-color: red;
              }
              HeaderWidget* widget = new HeaderWidget;
              widget->setProperty("type", "redBackgroundType");
              @

              1 Reply Last reply
              0
              • EddyE Offline
                EddyE Offline
                Eddy
                wrote on last edited by
                #7

                Can you show us a bitmap of what you want to accomplish. Eg. A screen shot of your ui file?

                I don't understand why you are subclassing QWidget and not one of it's descendants that looks like what you want.

                Qt Certified Specialist
                www.edalsolutions.be

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  p-himik
                  wrote on last edited by
                  #8

                  bq. You will, because neither QObject::setObjectName() nor QWidget::setStyleSheet() is declared virtual, which means that both method implementations will be skipped when called through base class pointers.

                  I know about absence of "virtual" modifier but these functions won't ever be called through base class pointers (even in ui_*.h file).

                  bq. But it should.

                  Here is a picture.
                  !http://dl.dropbox.com/u/26596688/notworking.JPG(useless style sheet for header widget)!

                  Eddy, all i want is ability to create multiple widgets of the same my class on a single window. And style sheet for every widget must be defined globally (my previous approach was to just copy style sheet over and over. But that's sucks). The reason why i don't want to create Qt Designer's plugin is that appearance of my widget could be changed a few times per day.
                  Wherever i write style sheet for my widget it'll be overwritten by parent's style sheet with ui->setupUi( this ) call.
                  And i subclass QWidget because there is no other widget that looks the way i need (already mentioned address box. Separate fields for region, state, city, etc. And style sheet different from the rest of the widgets).

                  bq. No, but you can use a property selector [doc.qt.nokia.com] to select single widget objects of the same type.

                  I'll try in a few minutes.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    p-himik
                    wrote on last edited by
                    #9

                    Forgot to mention - there is not only a window with multiple widget. There is multiple windows. So this style sheet have to be written in a place somehow related to HeaderWidget (i can assign it to QApplication, but it removes a great ability of WYSIWYG-like Qt Designer to show you changes as they are made).

                    [quote author="Lukas Geyer" date="1316855095"]No, but you can use a "property selector":http://doc.qt.nokia.com/latest/stylesheet-syntax.html#selector-types to select single widget objects of the same type.[/quote]
                    Tried. Didn't work either which is logically since HeaderWidget's style sheet is overwritten by parent widget's .ui file independently of what properties i used.

                    Again, overwriting with empty style sheet happens only when i press OK button in style sheet editor for widget promoted to HeaderWidget. If i don't touch anything (just place widget, adjust it's size and geometry and promote it to HeaderWidget) style sheet won't be overwritten and everything is OK.
                    Maybe it's worth to add a few lines to Qt Designer's sources so that it will remove "< property name="styleSheet">" completely from .ui file if there is no style sheet? Now it just places
                    @
                    < property name="styleSheet">
                    <string notr="true"/>
                    </property>@
                    there thus overwriting any style sheet provided by HeaderWidget.

                    And why reply editor removes "< property name="styleSheet">" when there is no space after left bracket?

                    1 Reply Last reply
                    0
                    • EddyE Offline
                      EddyE Offline
                      Eddy
                      wrote on last edited by
                      #10

                      I think we have come at a stadium that it is better to make a small compilable example and show us the code. This can help to fine tune the problem.

                      Qt Certified Specialist
                      www.edalsolutions.be

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        p-himik
                        wrote on last edited by
                        #11

                        Here it is
                        http://dl.dropbox.com/u/26596688/testProject.zip

                        1 Reply Last reply
                        0
                        • EddyE Offline
                          EddyE Offline
                          Eddy
                          wrote on last edited by
                          #12

                          I tried your code out and it works if you use it in main on the qapplication like this :

                          @int main(int argc, char *argv[])
                          {
                          QApplication a(argc, argv);
                          a.setStyleSheet("CommonWidget { ......
                          MainWindow w;
                          w.show();

                          return a.exec&#40;&#41;;
                          

                          }@

                          I omitted the @,*[class="CommonWidget"] @

                          Now throughout your application if there is a commonWidget it will be styled using this stylesheet.

                          Qt Certified Specialist
                          www.edalsolutions.be

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            p-himik
                            wrote on last edited by
                            #13

                            Eddy, thank you for your answer but i've written:
                            [quote author="p-himik" date="1316858503"]... (i can assign it to QApplication, but it removes a great ability of WYSIWYG-like Qt Designer to show you changes as they are made).[/quote]

                            Of course if there is no other way to do it, i'll do it this way.
                            And i still sure that Qt Designer must not insert “< property name=“styleSheet”>” tag into the .ui file when there is no any style sheet set.

                            1 Reply Last reply
                            0
                            • EddyE Offline
                              EddyE Offline
                              Eddy
                              wrote on last edited by
                              #14

                              Sorry i didn't see that.

                              Afaik stylesheets are not used the way you want. The power of stylesheets is exactly that you can give the whole of your application the same look in one place.

                              Qt Certified Specialist
                              www.edalsolutions.be

                              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