Results 1 to 11 of 11

Thread: Change background color of a custom widget

  1. #1
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default Change background color of a custom widget

    Hello.

    Creating a QWidget and change its background colour works fine. But if I try the same with a custom widget which inherits for QWidget it won't work.

    This is an minamal example which shows this behaviour:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MyWidget : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MyWidget(QWidget *parent = 0);
    16. };
    17.  
    18. class MainWindow : public QMainWindow
    19. {
    20. Q_OBJECT
    21.  
    22. public:
    23. explicit MainWindow(QWidget *parent = 0);
    24. ~MainWindow();
    25.  
    26. private slots:
    27. void on_pushButton_clicked();
    28.  
    29. private:
    30. Ui::MainWindow *ui;
    31. QWidget *m_pMyWidget;
    32. };
    33.  
    34. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MyWidget::MyWidget(QWidget *parent) :
    5. QWidget(parent)
    6. {
    7.  
    8. }
    9.  
    10. MainWindow::MainWindow(QWidget *parent) :
    11. QMainWindow(parent),
    12. ui(new Ui::MainWindow)
    13. {
    14. ui->setupUi(this);
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    21.  
    22. void MainWindow::on_pushButton_clicked()
    23. {
    24. //m_pMyWidget = new QWidget(this); // <= Works !!!
    25. m_pMyWidget = new MyWidget(this); // <= Does not work !!!
    26. m_pMyWidget->setGeometry(0,0,300,100);
    27. m_pMyWidget->setStyleSheet("background-color:black;");
    28. m_pMyWidget->show();
    29. }
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Change background color of a custom widget

    show your MyWidget implementation.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default Re: Change background color of a custom widget

    It's already in my first post: second Qt Code Line 4 to Line 8.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Change background color of a custom widget

    works perfectly for me...
    Try a clean and rebuild.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default Re: Change background color of a custom widget

    I've tried clean and rebuild. But it is still not working. I've tried Qt 4.8.1 (with MinGW and MSVC2008) and Qt 4.7.4 (with MinGW 4.4 and MSVC2008) all four in QtCreator 2.4.0

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Change background color of a custom widget

    Did you post your real code?
    Post your poroject here in a zip and I will try it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default Re: Change background color of a custom widget

    Yes this is the real code. I've attached the zip file.
    Attached Files Attached Files

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Change background color of a custom widget

    Remove the Q_OBJECT macro from your MyWidget subclass - you wont have signals/slots then.
    I don't have the time or mental resources to think why this is a problem though - or what you are doing wrong.
    Also, you are shadowing your own MainWindow class name with the Ui::MainWindow - give them different names.

    [corrected]
    Last edited by high_flyer; 5th July 2012 at 12:45.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default Re: Change background color of a custom widget

    If I remove the Q_OBJECT macro changing the background colour works. But then it seems that I can't define signals:

    class MyWidget : public QWidget
    {
    //Q_OBJECT

    public:
    explicit MyWidget(QWidget *parent = 0);

    signals:
    void mySignal();

    };
    The compiler says:

    mainwindow.h:20: Error: Class declarations lacks Q_OBJECT macro.
    mingw32-make.exe[1]: *** [release/moc_mainwindow.cpp] Error 1
    mingw32-make.exe: *** [release] Error 2

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Change background color of a custom widget

    sorry, when you use a QWidget direct subclass you need to re implement the paintEvent() as follows:
    http://qt-project.org/doc/qt-4.8/sty...qwidget-widget

    (I wonder why when Q_OBJECT is omitted from the subclass it does work....? don't see where the connection is at the moment...)
    Last edited by high_flyer; 5th July 2012 at 13:16.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Sep 2010
    Posts
    36
    Thanks
    1

    Default Re: Change background color of a custom widget

    Edit: This seems to work.

    Thank you!
    Last edited by rubikon; 5th July 2012 at 13:26.

Similar Threads

  1. Replies: 1
    Last Post: 17th August 2010, 16:17
  2. Change frame background color
    By Ishmael in forum Newbie
    Replies: 1
    Last Post: 7th June 2010, 05:38
  3. Change QPushButton Background Color, Qt4.3
    By Rayven in forum Qt Programming
    Replies: 5
    Last Post: 4th July 2009, 07:14
  4. how to change background color of QMenu??
    By anupamgee in forum Qt Programming
    Replies: 3
    Last Post: 28th April 2009, 12:41
  5. how to change QTextEdit background color ?
    By mismael85 in forum Qt Programming
    Replies: 9
    Last Post: 26th June 2008, 22:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.