I made a window and a button in it. I want the button to close the window when clicked, BUT I want to do it by a public slot that I created and which contains the close slot of the QWidget
, instead of doing it using the default QWidget::close()
. Here is my code.
window.h
#ifndef FENETRE_H
#define FENETRE_H
#include <QObject>
#include <QWidget>
#include <QPushButton>
class fenetre: public QWidget
{
Q_OBJECT
public:
fenetre();
public slots:
void another();
private:
QPushButton *button1;
};
#endif // FENETRE_H
window.cpp
#include "fenetre.h"
fenetre::fenetre():QWidget()
{
setFixedSize(300,300);
button1=new QPushButton("click",this);
connect(button1,SIGNAL(clicked()),this,SLOT(another()));
}
void fenetre::another()
{
fenetre().close();
}
main.cpp
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include "fenetre.h"
int main(int argc , char *argv[])
{
QApplication app(argc,argv);
fenetre fen;
fen.show();
return app.exec();
}