How to detect page change in QWizard?
-
How can i detect a next button click or page change?
Here is my code:
#include <QtWidgets>
#include <QTranslator>
#include <QLocale>
#include <QLibraryInfo>
#include <iostream>QWizardPage *createIntroPage()
{
QWizardPage *page = new QWizardPage;
page->setTitle("Transporter Addon");QLabel *label = new QLabel("Denne installer vil installer Transporter Addon for dig."); label->setWordWrap(true); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(label); page->setLayout(layout); return page;
}
QWizardPage *createInstallPage()
{
QWizardPage *page = new QWizardPage; page->setTitle("Installere"); page->setSubTitle("Klik install for at instalere Transporter Addon"); page->setButtonText(QWizard::NextButton, "Install"); return page;
}
QWizardPage *createInstallingPage()
{
QWizardPage *page = new QWizardPage; page->setTitle("Installere"); page->setSubTitle("Klik install for at instalere Transporter Addon"); QProgressBar *bar = new QProgressBar(); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(bar); page->setLayout(layout); page->setButtonText(QWizard::NextButton, "Install"); return page;
}
QWizardPage *createDonePage()
{
QWizardPage *page = new QWizardPage; page->setTitle("Conclusion"); QLabel *label = new QLabel("You are now successfully registered. Have a " "nice day!"); label->setWordWrap(true); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(label); page->setLayout(layout); return page;
}
class Handler : public QObject
{
Q_OBJECTpublic slots: void next(){ std::cout << "Debug"; }
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);#ifndef QT_NO_TRANSLATION
QString translatorFileName = QLatin1String("qtbase_");
translatorFileName += QLocale::system().name();
QTranslator *translator = new QTranslator(&app);
if (translator->load(translatorFileName, QLibraryInfo::path(QLibraryInfo::TranslationsPath)))
app.installTranslator(translator);
#endifQWizard wizard; wizard.addPage(createIntroPage()); wizard.addPage(createInstallPage()); wizard.addPage(createInstallingPage()); wizard.addPage(createDonePage()); Handler handler; wizard.setWindowTitle("Transporter Addon Installer"); wizard.show(); return app.exec();
}
-
I dont have a header file, i only have the folowing code and it is in the main cpp file.
class Handler : public QObject { Q_OBJECT public slots: void next(); }; void Handler::next(){ std::cout << "Debug"; }
Do i have to move my class into a header file and include that header?
@Mads030306 said in How to detect page change in QWizard?:
Do i have to move my class into a header file and include that header?
Not necessarily, unless we find that
moc
does not like multiple classes withQ_OBJECT
in them in one source file or only likes them in a.h
file, which I do not know....Can we confirm that this
class Handler
you show is placed in the source file above/before the compiler meets thewizard.connect(&wizard, &QWizard::currentIdChanged, &handler, &Handler::next);
line?UPDATE
According to e.g. https://forum.qt.io/topic/12388/q_object-in-cpp-file we are not going to do well withQ_OBJECT
class in a.cpp
file. By far the simplest will be to split it:- Create a new
handler.cpp
&handler.h
file in your project (from the Qt Creator wizard which allows you to add a new class-file, so that the.pro
gets updated right). - Move your
Handler
definition code into the.cpp
. - Move your
Handler
declaration code into the.h
. - Do a complete
qmake
/rebuild, and see what you get.
When that is working you will want to change your
Handler::next()
slot to accept theint id
parameter thatQWizard::currentIdChanged(int id)
will be passing to it. From that you will be able to find out about which page change is happening. - Create a new
-
How can i detect a next button click or page change?
Here is my code:
#include <QtWidgets>
#include <QTranslator>
#include <QLocale>
#include <QLibraryInfo>
#include <iostream>QWizardPage *createIntroPage()
{
QWizardPage *page = new QWizardPage;
page->setTitle("Transporter Addon");QLabel *label = new QLabel("Denne installer vil installer Transporter Addon for dig."); label->setWordWrap(true); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(label); page->setLayout(layout); return page;
}
QWizardPage *createInstallPage()
{
QWizardPage *page = new QWizardPage; page->setTitle("Installere"); page->setSubTitle("Klik install for at instalere Transporter Addon"); page->setButtonText(QWizard::NextButton, "Install"); return page;
}
QWizardPage *createInstallingPage()
{
QWizardPage *page = new QWizardPage; page->setTitle("Installere"); page->setSubTitle("Klik install for at instalere Transporter Addon"); QProgressBar *bar = new QProgressBar(); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(bar); page->setLayout(layout); page->setButtonText(QWizard::NextButton, "Install"); return page;
}
QWizardPage *createDonePage()
{
QWizardPage *page = new QWizardPage; page->setTitle("Conclusion"); QLabel *label = new QLabel("You are now successfully registered. Have a " "nice day!"); label->setWordWrap(true); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(label); page->setLayout(layout); return page;
}
class Handler : public QObject
{
Q_OBJECTpublic slots: void next(){ std::cout << "Debug"; }
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);#ifndef QT_NO_TRANSLATION
QString translatorFileName = QLatin1String("qtbase_");
translatorFileName += QLocale::system().name();
QTranslator *translator = new QTranslator(&app);
if (translator->load(translatorFileName, QLibraryInfo::path(QLibraryInfo::TranslationsPath)))
app.installTranslator(translator);
#endifQWizard wizard; wizard.addPage(createIntroPage()); wizard.addPage(createInstallPage()); wizard.addPage(createInstallingPage()); wizard.addPage(createDonePage()); Handler handler; wizard.setWindowTitle("Transporter Addon Installer"); wizard.show(); return app.exec();
}
@Mads030306 said in How to detect page change in QWizard?:
How can i detect a next button click or page change?
Hello and welcome.
void QWizard::currentIdChanged(int id)
This signal is emitted when the current page changes, with the new current id.
?
-
Thanks.
I already tried using that signal but couldn't get it to work.
Which object is the sender and how do i create a slot?
-
Thanks.
I already tried using that signal but couldn't get it to work.
Which object is the sender and how do i create a slot?
@Mads030306 said in How to detect page change in QWizard?:
I already tried using that signal but couldn't get it to work.
Then show your code. "couldn't get it to work" might mean wouldn't compile, didn't
connect()
successfully, ran and called the slot but whatever you did in the slot didn't work or never called the slot.Which object is the sender and how do i create a slot?
The
QWizard
is the sender and you would create a slot for it like any slot for a signal. Let's see what you attempted. (Just for this bit, not your whole program!) -
class Handler : public QObject { Q_OBJECT public slots: void next(); }; void Handler::next(){ std::cout << "Debug"; }
//this is in my main function Handler /***/handler/* = new Handler()*/; wizard.connect(&wizard, SIGNAL ( QWizard::currentIdChanged(int) ), &handler, SLOT ( Handler::next ));
-
class Handler : public QObject { Q_OBJECT public slots: void next(); }; void Handler::next(){ std::cout << "Debug"; }
//this is in my main function Handler /***/handler/* = new Handler()*/; wizard.connect(&wizard, SIGNAL ( QWizard::currentIdChanged(int) ), &handler, SLOT ( Handler::next ));
@Mads030306 Please be more specific: did you put your Handler class in its own header/cpp files?
-
class Handler : public QObject { Q_OBJECT public slots: void next(); }; void Handler::next(){ std::cout << "Debug"; }
//this is in my main function Handler /***/handler/* = new Handler()*/; wizard.connect(&wizard, SIGNAL ( QWizard::currentIdChanged(int) ), &handler, SLOT ( Handler::next ));
@Mads030306 said in How to detect page change in QWizard?:
wizard.connect(&wizard, SIGNAL ( QWizard::currentIdChanged(int) ), &handler, SLOT ( Handler::next ));
Your syntax is wrong. Please, please change over to New Signal Slot Syntax --- which has been around for years ever since Qt5 --- and it will stop you doing this. Get rid of all
SIGNAL
/SLOT()
macros. Life will be so much easier/cleaner.I could tell you what you need in old style but it would be much better for you if you change over, so do that first....
-
@Mads030306 said in How to detect page change in QWizard?:
wizard.connect(&wizard, SIGNAL ( QWizard::currentIdChanged(int) ), &handler, SLOT ( Handler::next ));
Your syntax is wrong. Please, please change over to New Signal Slot Syntax --- which has been around for years ever since Qt5 --- and it will stop you doing this. Get rid of all
SIGNAL
/SLOT()
macros. Life will be so much easier/cleaner.I could tell you what you need in old style but it would be much better for you if you change over, so do that first....
@JonB
I have changed to the new method but still get the same error.wizard.connect(&wizard, &QWizard::currentIdChanged, &handler, &Handler::next);
Do i have to specify the parameter of &QWizard::currentIdChanged like before (SIGNAL ( QWizard::currentIdChanged(int) ))?
-
@JonB
I have changed to the new method but still get the same error.wizard.connect(&wizard, &QWizard::currentIdChanged, &handler, &Handler::next);
Do i have to specify the parameter of &QWizard::currentIdChanged like before (SIGNAL ( QWizard::currentIdChanged(int) ))?
@Mads030306
OK, that new syntax is a big improvement!With that for " still get the same error" do you still get that error which is a linker error or do you get a compiler error? If it's a compiler error, it's not "the same" (in which case please show the new error message).
Depending on answer, it looks like you are not linking with the module for
Handler
....Do i have to specify the parameter of &QWizard::currentIdChanged like before (SIGNAL ( QWizard::currentIdChanged(int) ))?
No. Rather the signature for slot
Handler::next(...)
must "match" the signature for signalQWizard::currentIdChanged()
. Show your signature forHandler::next(...)
definition if not sure. -
@Mads030306
OK, that new syntax is a big improvement!With that for " still get the same error" do you still get that error which is a linker error or do you get a compiler error? If it's a compiler error, it's not "the same" (in which case please show the new error message).
Depending on answer, it looks like you are not linking with the module for
Handler
....Do i have to specify the parameter of &QWizard::currentIdChanged like before (SIGNAL ( QWizard::currentIdChanged(int) ))?
No. Rather the signature for slot
Handler::next(...)
must "match" the signature for signalQWizard::currentIdChanged()
. Show your signature forHandler::next(...)
definition if not sure.@JonB
i get a compile error (the one in the picture in the other post).On the signal and slots documentation i found something about the error i get, i need to run something called moc but i dont know how to run it.
The Q_OBJECT macro is expanded by the preprocessor to declare several member functions that are implemented by the moc; if you get compiler errors along the lines of "undefined reference to vtable for LcdNumber", you have probably forgotten to run the moc or to include the moc output in the link command.
Also what is linking and how do i do it.
-
@JonB
i get a compile error (the one in the picture in the other post).On the signal and slots documentation i found something about the error i get, i need to run something called moc but i dont know how to run it.
The Q_OBJECT macro is expanded by the preprocessor to declare several member functions that are implemented by the moc; if you get compiler errors along the lines of "undefined reference to vtable for LcdNumber", you have probably forgotten to run the moc or to include the moc output in the link command.
Also what is linking and how do i do it.
@Mads030306 said in How to detect page change in QWizard?:
i need to run something called moc but i dont know how to run it.
Also what is linking and how do i do it.
All this is done for you by the build process, assuming your project is correctly defined in the
.pro
file.- Show your signature for
Handler::next(...)
. Also confirm in the.h
file it is in sectionpublic slots:
. I need to check that anyway. - Show your declaration of
class Handler
. Since it is using signals/slots, does it have a line reading justQ_OBJECT
immediately after the{
of the class declaration in the.h
file?
(Essentially just show the whole of the
class Handler
declaration in the.h
[not.cpp
!] file.) - Show your signature for
-
I dont have a header file, i only have the folowing code and it is in the main cpp file.
class Handler : public QObject { Q_OBJECT public slots: void next(); }; void Handler::next(){ std::cout << "Debug"; }
Do i have to move my class into a header file and include that header?
-
I dont have a header file, i only have the folowing code and it is in the main cpp file.
class Handler : public QObject { Q_OBJECT public slots: void next(); }; void Handler::next(){ std::cout << "Debug"; }
Do i have to move my class into a header file and include that header?
@Mads030306 said in How to detect page change in QWizard?:
I dont have a header file, i only have the folowing code and it is in the main cpp file
Please move your Handler class in its own header and cpp files! Classes which subclass QObject needs to be in their own header/cpp files in order for moc to be able to process them.
-
I dont have a header file, i only have the folowing code and it is in the main cpp file.
class Handler : public QObject { Q_OBJECT public slots: void next(); }; void Handler::next(){ std::cout << "Debug"; }
Do i have to move my class into a header file and include that header?
@Mads030306 said in How to detect page change in QWizard?:
Do i have to move my class into a header file and include that header?
Not necessarily, unless we find that
moc
does not like multiple classes withQ_OBJECT
in them in one source file or only likes them in a.h
file, which I do not know....Can we confirm that this
class Handler
you show is placed in the source file above/before the compiler meets thewizard.connect(&wizard, &QWizard::currentIdChanged, &handler, &Handler::next);
line?UPDATE
According to e.g. https://forum.qt.io/topic/12388/q_object-in-cpp-file we are not going to do well withQ_OBJECT
class in a.cpp
file. By far the simplest will be to split it:- Create a new
handler.cpp
&handler.h
file in your project (from the Qt Creator wizard which allows you to add a new class-file, so that the.pro
gets updated right). - Move your
Handler
definition code into the.cpp
. - Move your
Handler
declaration code into the.h
. - Do a complete
qmake
/rebuild, and see what you get.
When that is working you will want to change your
Handler::next()
slot to accept theint id
parameter thatQWizard::currentIdChanged(int id)
will be passing to it. From that you will be able to find out about which page change is happening. - Create a new
-
It works now.
Thank you for helping :)
Sorry for my dumb questions, im very new to c++ and qt.