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. style sheets with centralWidget?

style sheets with centralWidget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 3.1k 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.
  • D Offline
    D Offline
    Darkware
    wrote on last edited by
    #1

    Inside my MainWindow constructor I have:

    setCentralWidget(new mainCont(this)); //a dummy widget to serve as a container to put other widgets into
    centralWidget()->setObjectName("mainContName");
    centralWidget()->setStyleSheet("#mainContName {background-image: url(./myBG.bmp)}");

    I am trying to set myBG.bmp as the background image of the centralWidget().

    This code does not work. If I were to take out "centralWidget()" and replace it with "this" to target the MainWindow, I can see that the code does work if I did want to apply it to MainWindow (but I don't). So I know my syntax is correct, it's just that I can't get it to work on centralWidget().

    If I take out "#mainContName" and the {}, it successfully applies the image to centralWidget() (yay!), however... it also applies it to every child within, which I do not want (hence why I wanted to specifically target an object my name so it only applies to that one object).

    Any ideas?

    I've checked the inheritance of the QWidget class to make sure it allows the usage of setObjectName() and setStyleSheet(), so no problems there. The documentation at http://doc.qt.io/qt-4.8/stylesheet.html suggests exactly what I'm doing from what I can see, so I'm not sure where I'm going wrong.

    R 1 Reply Last reply
    0
    • D Darkware

      Inside my MainWindow constructor I have:

      setCentralWidget(new mainCont(this)); //a dummy widget to serve as a container to put other widgets into
      centralWidget()->setObjectName("mainContName");
      centralWidget()->setStyleSheet("#mainContName {background-image: url(./myBG.bmp)}");

      I am trying to set myBG.bmp as the background image of the centralWidget().

      This code does not work. If I were to take out "centralWidget()" and replace it with "this" to target the MainWindow, I can see that the code does work if I did want to apply it to MainWindow (but I don't). So I know my syntax is correct, it's just that I can't get it to work on centralWidget().

      If I take out "#mainContName" and the {}, it successfully applies the image to centralWidget() (yay!), however... it also applies it to every child within, which I do not want (hence why I wanted to specifically target an object my name so it only applies to that one object).

      Any ideas?

      I've checked the inheritance of the QWidget class to make sure it allows the usage of setObjectName() and setStyleSheet(), so no problems there. The documentation at http://doc.qt.io/qt-4.8/stylesheet.html suggests exactly what I'm doing from what I can see, so I'm not sure where I'm going wrong.

      R Offline
      R Offline
      raf924
      wrote on last edited by
      #2

      @Darkware When you say "replace it with this" do you mean for the setObjectName too or just for the setStyleSheet ?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Darkware
        wrote on last edited by
        #3

        For both of them. It demonstrates that setStyleSheet() successfully targets MainWindow via the object name I give it and applies the style sheet, which leads to the conclusion that my syntax appears to work for MainWindow, but not for centralWidget()

        R 1 Reply Last reply
        0
        • D Darkware

          For both of them. It demonstrates that setStyleSheet() successfully targets MainWindow via the object name I give it and applies the style sheet, which leads to the conclusion that my syntax appears to work for MainWindow, but not for centralWidget()

          R Offline
          R Offline
          raf924
          wrote on last edited by
          #4

          @Darkware Try just replacing it just for setStyleSheet, the css might be looking only among the children of centralWidget for the name.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Darkware
            wrote on last edited by
            #5

            Doesn't work.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              The code from your first post works as expected for me (except I used a QWidget instead of mainCont).

              Can you tell us which Qt version you're using and what is mainCont?

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Darkware
                wrote on last edited by
                #7

                Qt 5.5.1
                Qt Creator 3.5.1

                class mainCont : public QWidget
                {
                Q_OBJECT

                public:
                    mainCont(QWidget *parent);
                

                };

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Darkware
                  wrote on last edited by
                  #8

                  I put
                  trace = new QLineEdit(objectName());
                  lay->addWidget(trace);
                  inside the mainCont() constructor to see if the setObjectName() was executing properly. If I do setObjectName("mainContName"); within mainCont(), trace prints the name out correctly. However, if I try to set the object name by going centralWidget()->setObjectName("mainContName"); from my MainWindow constructor, trace is empty. So something is wrong with how I'm doing centralWidget->.

                  Doing
                  setStyleSheet("QWidget#mainContName {background-image: url(./myBG.bmp)}");
                  inside mainCont() doesn't work though, despite setObjectName("mainContName"); working inside of it.

                  1 Reply Last reply
                  0
                  • Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    What is lay->? Sounds like you've got some problem with how you setup your widgets, unrelated to stylesheets.
                    Could you post the contents of your main window constructor? You're probably setting the content and then overwriting it with something else.

                    Btw. please surround code blocks with ```. It formats a lot better.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Darkware
                      wrote on last edited by Darkware
                      #10

                      mainWinClass::mainWinClass()
                      {
                      //menu
                      createActions();

                      mainCont *myMainCont = new mainCont(this);
                      setCentralWidget(myMainCont);
                      myMainCont->setStyleSheet("#mainContName {background-image: url(./myBG.bmp)}");
                      

                      }

                      mainCont::mainCont(QWidget *parent)
                      {
                      setObjectName("mainContName");

                      QVBoxLayout *lay = new QVBoxLayout();
                      
                      trace = new QLineEdit(objectName()); //prints "mainContName" correctly.
                      lay->addWidget(trace);
                      
                      //other code here
                      

                      }

                      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