Qt operates by creating a QWidget
window, and then an event loop.
Your code to create a window, and then a button that triggers another function is trivial.
Many of the examples Qt provides show this functionality.
To get started, Download Qt. Download the Qt Add-On for Visual Studio. Note, that the express edition of visual studio doesn't work with the Qt Add-On.
After you have all that done, create a Qt project from the Qt classes that are available. In this case, I would not choose QML/Qt Quick, but just a Qt Widgets class, such as QWidget
or QDialog
or QMainWindow
.
Using the Qt Add-On should ensure that you have the libraries found and available to Visual Studio.
Make sure that you can get a Hello World example is buildable and running using a Qt Widget of some sort.
The code it generates for you should look like this:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To add in a button, you need to place it on a Widget and then connect a SLOT to its clicked()
signal. The following is a compact way to do it.
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QDebug>
#include <QApplication>
static void someFunction ()
{
qDebug() << "Button was clicked";
// run my other function that requires a parameter of 5
qDebug() << "Run my other function with 5!";
// otherFunction(5);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QPushButton *button = new QPushButton("Click Me");
QObject::connect(button, &QPushButton::clicked, someFunction);
QVBoxLayout *vbox = new QVBoxLayout();
vbox->addWidget(button);
w.setLayout(vbox);
w.show();
return a.exec();
}
To make a powerful GUI, you will want to leverage more than just a functor, and use QObjects and let it use the moc compiler and create your own signals and slots for all your custom functions.
Connecting to a functor is new with Qt 5.
http://qt-project.org/wiki/New_Signal_Slot_Syntax
http://woboq.com/blog/new-signals-slots-syntax-in-qt5.html
A more verbose way to do it, but allows for more expandability and OOP, is to subclass QWidget or QMainWindow, like it generated for you, and put a method in your subclassed function that you put under your slots
list in your header file.
http://qt-project.org/doc/qt-5/signalsandslots.html
Take the time to understand signals and slots and you can create almost any GUI you can imagine using Qt. Good luck.
Hope that helps.