[quote author="TheDestroyer" date="1336490799"]The thing is that the way those GUI's work is different. That's why I could expect a different behaviour.[/quote]Not in such a fundamental way. A QWidget subclass still is a normal C++ class after the moc run, and when it has a pointer member you can do with it whatever you can also do in a non GUI class.
[quote author="TheDestroyer" date="1336490799"]No one knows how those meta files of the GUIs are managed..., and how they take information from other objects.[/quote]That's not right, what the meta object compiler does is relatively transparent when you look at the outputted moc_ files. No magic going on, it just adds some homebrew RTTI class and boilerplate code to every QObject class to make sigs/slots work. Specifically, it does not touch your settings pointer member.
I've tried it. Here are three files: main.pro, main.h and main.cpp:
main.pro:
@QT += core gui
TEMPLATE = app
HEADERS += main.h
SOURCES += main.cpp@
main.h:
@#ifndef HEADER_H
#define HEADER_H
#include <QtGui>
class Settings
{
public:
Settings():intValue(0), boolValue(false) {};
~Settings() {}
int intValue;
bool boolValue;
};
class QTestWidget : public QWidget
{
Q_OBJECT
public:
explicit QTestWidget(QWidget *parent, Settings *settings);
protected:
virtual void paintEvent(QPaintEvent *event);
private:
Settings *mSettings;
QTimer mRenderTimer;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow() { delete mSettings; }
private slots:
void changeBoolSetting();
void changeIntSetting();
private:
Settings *mSettings;
};
#endif // HEADER_H@
and main.cpp:
@#include "main.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
// === Implementation of MainWindow, QTestWidget and Settings ===
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
mSettings(new Settings)
{
setGeometry(50, 50, 100, 110);
QTestWidget *tw = new QTestWidget(this, mSettings);
QPushButton *b1 = new QPushButton(this);
QPushButton *b2 = new QPushButton(this);
tw->setGeometry(5, 5, 90, 70);
b1->setGeometry(25, 75, 25, 25);
b2->setGeometry(50, 75, 25, 25);
connect(b1, SIGNAL(clicked()), SLOT(changeBoolSetting()));
connect(b2, SIGNAL(clicked()), SLOT(changeIntSetting()));
}
void MainWindow::changeBoolSetting()
{
mSettings->boolValue = !mSettings->boolValue;
}
void MainWindow::changeIntSetting()
{
mSettings->intValue += 1;
}
QTestWidget::QTestWidget(QWidget *parent, Settings *settings) :
QWidget(parent),
mSettings(settings)
{
connect(&mRenderTimer, SIGNAL(timeout()), this, SLOT(update()));
mRenderTimer.start(500);
}
void QTestWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
painter.drawRect(rect().adjusted(0, 0, -1, -1));
painter.drawText(rect(),
Qt::AlignCenter,
QString::number(mSettings->intValue)+"\n"+
QString(mSettings->boolValue ? "true" : "false"));
painter.drawText(0, 13, QDateTime::currentDateTime().toString("hh:mm:ss:zzz"));
}@
run
qmake; make; ./main
and you'll see that it works as expected. Click the two buttons and see the QTestWidget update its display (in 500ms render-intervals), depending on it's injected Settings pointer.