PDA

View Full Version : Change background color of a custom widget



rubikon
5th July 2012, 09:19
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:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MyWidget : public QWidget
{
Q_OBJECT

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

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButton_clicked();

private:
Ui::MainWindow *ui;
QWidget *m_pMyWidget;
};

#endif // MAINWINDOW_H


#include "mainwindow.h"
#include "ui_mainwindow.h"

MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{

}

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
//m_pMyWidget = new QWidget(this); // <= Works !!!
m_pMyWidget = new MyWidget(this); // <= Does not work !!!
m_pMyWidget->setGeometry(0,0,300,100);
m_pMyWidget->setStyleSheet("background-color:black;");
m_pMyWidget->show();
}


What am I doing wrong?

high_flyer
5th July 2012, 09:44
show your MyWidget implementation.

rubikon
5th July 2012, 09:49
It's already in my first post: second Qt Code Line 4 to Line 8.

high_flyer
5th July 2012, 10:10
works perfectly for me...
Try a clean and rebuild.

rubikon
5th July 2012, 10:27
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

high_flyer
5th July 2012, 10:29
Did you post your real code?
Post your poroject here in a zip and I will try it.

rubikon
5th July 2012, 10:45
Yes this is the real code. I've attached the zip file.

high_flyer
5th July 2012, 11:58
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]

rubikon
5th July 2012, 12:36
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

high_flyer
5th July 2012, 13:16
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/stylesheet-reference.html#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...)

rubikon
5th July 2012, 13:21
Edit: This seems to work.

Thank you!