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. How do I change the background color of my QWidget?

How do I change the background color of my QWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 14.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.
  • M Offline
    M Offline
    mghalayini
    wrote on last edited by
    #1

    Hi, I'm new to this. And I started learning C++ 2 weeks ago, and I'm finding qt overwhelming on the programming part. But anyway, if you created a new project, how do you change the background color of QWidget? I mean I looked at the qt documentation but I can't understand anything. They say I can use QPalette but where do I write the code? I really don't understand it. Please help.

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

      Hi and welcome to the forums.
      You can write the code in MainWindow after the setupUI function.

      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          QPalette pal = ui->widget->palette();
      
          // set black background
          pal.setColor(QPalette::Background, Qt::black);
          ui->widget->setAutoFillBackground(true);
          ui->widget->setPalette(pal);
          ui->widget->show();
      
      

      if you used UI files to make the app.
      You can also do it in your widgets constructor if you made a subclass.

      You can also use stylesheets to change color if you wish.

      https://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget

      1 Reply Last reply
      3
      • S Offline
        S Offline
        sihnatse
        wrote on last edited by sihnatse
        #3

        @mrjj your code generates compilation errors in nearly every line:
        error: C2039: 'widget': is not a member of 'Ui::MainWindow'
        error: C2039: 'Background': is not a member of 'QPalette'
        ...

        mrjjM JonBJ 2 Replies Last reply
        0
        • S sihnatse

          @mrjj your code generates compilation errors in nearly every line:
          error: C2039: 'widget': is not a member of 'Ui::MainWindow'
          error: C2039: 'Background': is not a member of 'QPalette'
          ...

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

          @sihnatse

          Hi
          You need to place a QWidget on Mainform ( just drag one there)
          and you also need to include
          #include <QPalette>

          1 Reply Last reply
          0
          • S sihnatse

            @mrjj your code generates compilation errors in nearly every line:
            error: C2039: 'widget': is not a member of 'Ui::MainWindow'
            error: C2039: 'Background': is not a member of 'QPalette'
            ...

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

            @sihnatse said in How do I change the background color of my QWidget?:

            error: C2039: 'widget': is not a member of 'Ui::MainWindow'

            You have to start by creating the right/corresponding .ui file from Designer if you expect the code snippet @mrjj showed to work for you....

            error: C2039: 'Background': is not a member of 'QPalette'

            It was right in 2019. You might find you need QPalette::Window now....

            S 1 Reply Last reply
            2
            • JonBJ JonB

              @sihnatse said in How do I change the background color of my QWidget?:

              error: C2039: 'widget': is not a member of 'Ui::MainWindow'

              You have to start by creating the right/corresponding .ui file from Designer if you expect the code snippet @mrjj showed to work for you....

              error: C2039: 'Background': is not a member of 'QPalette'

              It was right in 2019. You might find you need QPalette::Window now....

              S Offline
              S Offline
              sihnatse
              wrote on last edited by
              #6

              @JonB Why is .ui file needed here?

              @mrjj "You need to place a QWidget on Mainform ( just drag one there)" - Could please translate this to English?

              JonBJ 1 Reply Last reply
              0
              • S sihnatse

                @JonB Why is .ui file needed here?

                @mrjj "You need to place a QWidget on Mainform ( just drag one there)" - Could please translate this to English?

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

                @sihnatse
                The answer to both those is: since you see @mrjj used Ui::MainWindow and ui-> we can tell that he used Qt Designer (the Designer window inside Qt Creator) to design his UI and generated code from it. Because that is what the Designer-generated code looks like. And Designer creates a .ui file. (I see he also wrote "if you used UI files to make the app.", so that's a clue.)

                Either use Designer (I don't know whether you have ever done so) if you want to be in the same position as he was when he wrote the code, or if you prefer to write your UI code directly yourself you will need to adapt the code to however you do what you do.

                In this case ui->widget will be a QWidget *, so if you do it yourself change that to whatever in your code for the QWidget * whose background you want to affect.

                S 1 Reply Last reply
                2
                • JonBJ JonB

                  @sihnatse
                  The answer to both those is: since you see @mrjj used Ui::MainWindow and ui-> we can tell that he used Qt Designer (the Designer window inside Qt Creator) to design his UI and generated code from it. Because that is what the Designer-generated code looks like. And Designer creates a .ui file. (I see he also wrote "if you used UI files to make the app.", so that's a clue.)

                  Either use Designer (I don't know whether you have ever done so) if you want to be in the same position as he was when he wrote the code, or if you prefer to write your UI code directly yourself you will need to adapt the code to however you do what you do.

                  In this case ui->widget will be a QWidget *, so if you do it yourself change that to whatever in your code for the QWidget * whose background you want to affect.

                  S Offline
                  S Offline
                  sihnatse
                  wrote on last edited by sihnatse
                  #8

                  @JonB @mrjj Thanks. Now I see it works.

                  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