Reputation: 3
So this is the code of the main.cpp:
#include <QApplication>
#include <QtGui>
#include <mainwidget.h>
int main(int argc, char **argv)
{
QPushButton button ("Hello world!");
button.show();
mainWidget widget;
widget.show();
return app.exec();
}
I want a button from the "widget" to close the "Hello world!" window. I have already added that button in "widget" and made a function for it in mainwidget.h (which also shows a message box) and connected them. I just want to know how can that same button close the Hello World window. I guess I have to add something to the function in mainwidget.h but I don't know what it is. The instructor says we should use the QWidget close() function.
Upvotes: 0
Views: 752
Reputation: 2005
I will answer to your question but before I'll explain you how a basic Qt application looks like.
In Qt, a basic main is like :
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Here as you can see a QApplication is created then a MainWindow. So What's the MainWindow class ?
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H
It's the MainWindow.h. As you can see MainWindow inherit to the QMainWindow class. It's the main window class to create a graphic user interface. the Q_OBJECT define is for qmake. Indeed qmake I'll create a moc_mainwindow.cpp for this class to manage the Qt signals. Now you get an empty window if you create an Empty constructor and destructor like:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
}
MainWindow::~MainWindow()
{
}
then after you wish to write "Hello world !" in the window so in Qt to write a text you can use a QLabel. So to write "Hello World !" you get:
#include "mainwindow.h"
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget *widget = new QLabel("Hello world !", this);
}
MainWindow::~MainWindow()
{
}
then after to create a button as you made you ll use the QPushButton class. so you get:
#include "mainwindow.h"
#include <QLabel>
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget *widget = new QLabel("Hello world !", this);
setCentralWidget(widget);
QPushButton *button = new QPushButton("close", this);
}
MainWindow::~MainWindow()
{
}
(I Choose to set the QLabel to the central Widget to don't get the label behind the button but after and in real Qt application a QMainWindow's central widget is mostly usualy a QWidget I'll explain you why after) now you have a button. But when you click on it nothing append. Why ? Because nothing link the Button and the Window. to link it in Qt we use the connect function. [http://qt-project.org/doc/qt-4.8/qobject.html][1]
so with connect to close the window when you click on the button you get:
#include "mainwindow.h"
#include <QLabel>
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget *widget = new QLabel("Hello world !", this);
setCentralWidget(widget);
QPushButton *button = new QPushButton("close", this);
connect(button, SIGNAL(clicked()), this, SLOT(close()));
}
MainWindow::~MainWindow()
{
}
As you can see with connect. In first parameter, we put the object which ll send the signal here the button. in second parameter, we put the signal to link here it's clicked() to do this we write SIGNAL(clicked()). In third, the object which will receive the signal, here the window to close. In fourth parameter the function to launch when the signal is received. We write this SLOT(close()).
Why SIGNAL and SLOT ? because in Qt to create a signal we use signal: in the .hh and to create a slot et use (public or protected or private) slots: example:
Class Object
{
Q_OBJECT
...
signals:
void aSignal();
public slots:
void aSlot();
...
};
NOTE: the signal and the slot must have same return and parameters.
after to organize your objects you ll use the labels in the QWidget in the centralWidget so you have:
#include "mainwindow.h"
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget* centralWid = new QWidget(this);
setCentralWidget(centralWid);
QVBoxLayout *layout = new QVBoxLayout(centralWid);
QWidget *widget = new QLabel("Hello world !", this);
QPushButton *button = new QPushButton("close", this);
layout->addWidget(widget);
layout->addWidget(button);
centralWid->setLayout(layout);
connect(button, SIGNAL(clicked()), this, SLOT(close()));
}
MainWindow::~MainWindow()
{
}
Upvotes: 2