Qt World Summit: Submit your Presentation


How do I change the background color of my QWidget?



  • 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.


  • Lifetime Qt Champion

    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



  • @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'
    ...


  • Lifetime Qt Champion

    @sihnatse

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



  • @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....



  • @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?



  • @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.



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


Log in to reply