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. changing window background color
Forum Updated to NodeBB v4.3 + New Features

changing window background color

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 750 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.
  • J Offline
    J Offline
    jediapingveno
    wrote on last edited by
    #1

    Hi,

    I am trying to change the appearance of the windows produced by QT.

    I tried using style sheets to change the background color of a window, as follows:

    QApplication *app = new QApplication(argc, argv);
    
    Control *control = new Control();
    app->setStyleSheet("Control { background: black;} ");
    

    That works until I add widgets to the control window. Then the window background becomes white again.

      auto *vbox = new QVBoxLayout();
    
      auto *btn1 = new QPushButton("button 1");
      vbox->addWidget(btn1);
    
      auto *lbl1 = new QLabel("label 1");
      vbox->addWidget(lbl1);
    
      setLayout(vbox);
    

    How do I keep background color when I add buttons to the window?

    Regards,

    Jed

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

      Hi and welcome to devnet,

      Can you provide a minimal compilable example that shows that behaviour ?

      Which version of Qt is it ?
      On which OS ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jediapingveno
        wrote on last edited by
        #3

        I am using manjaro linux with the XFCE desktop. QT version 5.15.2.

        The three program files are as follows:

        main.cpp:

        #include <QApplication>
        #include <QWidget>
        #include "control.h"
        
        int main(int argc, char **argv)
        {
          QApplication *app = new QApplication(argc, argv);
        
          Control *control = new Control();
        
          QString style1 = "Control {background: black; } ";
        
          app->setStyleSheet(style1);
        
          control->move(500,300);
          control->setGeometry(300,300,400,500);
        
          control -> show();
        
          return app -> exec();
        }
        
        

        control.h:

        #pragma once
        
        #include <QWidget>
        #include <QPushButton>
        
        class Control : public QWidget
        {
          Q_OBJECT
            
          public:
            Control(QWidget *parent = 0);
        };
        
        

        control.cpp:

        #include "control.h"
        
        #include <QVBoxLayout>
        #include <QPushButton>
        #include <QLabel>
        
        Control::Control(QWidget *parent) : QWidget(parent)
        {
          setWindowTitle("Control Window");
        
          auto *vbox = new QVBoxLayout();
        /*
          // make buttons
          auto *btn1 = new QPushButton("button 1");
          vbox->addWidget(btn1);
        
          auto *btn2 = new QPushButton("button 2");
          vbox->addWidget(btn2);
        
          // make labels
          auto *lbl1 = new QLabel("label 1");
          vbox->addWidget(lbl1);
        */
          setLayout(vbox);
        }
        

        If this is compiled as is, the background is black, but if you add in the commented out code, the background reverts to white.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          It's working fine for me with the same version of Qt on macOS 10.13.6.

          Is this the Qt version provided with your distribution ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

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