0

I am very new to Qt and it looks like i have some very basic misunderstanding about how this library works. I am currently reading a book by m. Schlee but i do not want to continue until i do understand how to make this simple program work.

#include <QtWidgets>
#include <QApplication>
#include <QStackedWidget>
#include <QPushButton>
#include <QObject>

struct wizard : public QObject
{
    QStackedWidget* p;

    wizard(QStackedWidget* pp) : p(pp) { }

public slots:
    void change()
    {
        int to = p->currentIndex();
        if (to == p->count() - 1)
            to = 0;
        else
            ++to;

        emit chIndex(to);
    }

signals:
    void chIndex(int);
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QStackedWidget qsw;
    QPushButton qpb("magic");

    qsw.resize(500, 500);
    qsw.move(500, 300);
    qsw.setWindowTitle("test qsw");

    qpb.move(330, 300);
    qpb.setWindowTitle("test qpb");

    QWidget* pw1 = new QWidget();
    QPalette pal1;
    pal1.setColor(pw1->backgroundRole(), Qt::blue);
    pw1->setPalette(pal1);
    pw1->resize(500, 500);
    pw1->setAutoFillBackground(true);

    QWidget* pw2 = new QWidget();
    QPalette pal2;
    pal2.setColor(pw2->backgroundRole(), Qt::yellow);
    pw2->setPalette(pal2);
    pw2->resize(500, 500);
    pw2->setAutoFillBackground(true);

    qsw.addWidget(pw1);
    qsw.addWidget(pw2);

    wizard stupidity(&qsw);

    QObject::connect(&qpb, SIGNAL(clicked()), &stupidity, SLOT(change()));
    QObject::connect(&stupidity, SIGNAL(chIndex(int)), &qsw, SLOT(setCurrentIndex(int)));

    qpb.show();
    qsw.show();

    return a.exec();
}

The idea is to launch 2 separate windows: one with painted background, and another with button that changes color (blue->yellow->blue->.. etc). They appear, but nothing happens if i press the button. Please help.

Kirill
  • 65
  • 7

1 Answers1

1

Except for struct being a class and a missing Q_OBJECT macro the code is fine

Try the following:

create a main.h file having this content:

#ifndef MAIN_H
#define MAIN_H
#include <QObject>
#include <QStackedWidget>

class wizard : public QObject
{
    Q_OBJECT
public:
    QStackedWidget* p;

    wizard(QStackedWidget* pp) : p(pp) { }

public slots:
    void change()
    {
        int to = p->currentIndex();
        if (to == p->count() - 1)
            to = 0;
        else
            ++to;

        emit chIndex(to);
    }

signals:
    void chIndex(int);
};

#endif // MAIN_H

change your main.cpp to the following content:

#include <QtWidgets>
#include <QApplication>
#include <QPushButton>

#include <main.h>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QStackedWidget qsw;
    QPushButton qpb("magic");

    qsw.resize(500, 500);
    qsw.move(500, 300);
    qsw.setWindowTitle("test qsw");

    qpb.move(330, 300);
    qpb.setWindowTitle("test qpb");

    QWidget* pw1 = new QWidget();
    QPalette pal1;
    pal1.setColor(pw1->backgroundRole(), Qt::blue);
    pw1->setPalette(pal1);
    pw1->resize(500, 500);
    pw1->setAutoFillBackground(true);

    QWidget* pw2 = new QWidget();
    QPalette pal2;
    pal2.setColor(pw2->backgroundRole(), Qt::yellow);
    pw2->setPalette(pal2);
    pw2->resize(500, 500);
    pw2->setAutoFillBackground(true);

    qsw.addWidget(pw1);
    qsw.addWidget(pw2);

    wizard stupidity(&qsw);

    QObject::connect(&qpb, SIGNAL(clicked()), &stupidity, SLOT(change()));
    QObject::connect(&stupidity, SIGNAL(chIndex(int)), &qsw, SLOT(setCurrentIndex(int)));

    qpb.show();
    qsw.show();

    return a.exec();
}

qmake doesn't work very well with Q_OBJECT macro directly in cpp file. Btw. run qmake after changes applied.

Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38