[SOLVED]Closing Newwindow.



  • Hi Guys,

    i have created a Main window . On main window i have a pushbutton, by click event on pushbutton1 a new window will open. I am trying to add a Close Button in form of (pushbutton2)on Newwindow. But it is not functioning. Basically i would like to add a Pushbutton2 on Newwindow , So by Click event on Pushbutton2 Newwindow should close.

    Thanks.



  • you should connect the signal and slot.eg:
    connect(btn2,SIGNAL(clicked(bool)),this,SLOT(close()));
    all the code is:

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_pushButton_clicked();//btn1 click slot
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "mywindow.h"
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
       MyWindow *newwindow = new MyWindow(this);//create a new window
       newwindow->setWindowTitle("new window");
       newwindow->show();
    }
    

    mywindow.h

    #ifndef MYWINDOW_H
    #define MYWINDOW_H
    
    #include <QMainWindow>
    #include <QPushButton>
    class MyWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MyWindow(QWidget *parent = 0);
    
    signals:
    
    public slots:
    
    private:
        QPushButton *btn2;//one btn on newwindow
    };
    
    #endif // MYWINDOW_H
    
    

    mywindow.cpp

    #include "mywindow.h"
    
    MyWindow::MyWindow(QWidget *parent) : QMainWindow(parent)
    {
        btn2 = new QPushButton(this);
        btn2->setText("btn2");
        connect(btn2,SIGNAL(clicked(bool)),this,SLOT(close()));//connect btn2's signal an slot
    }
    


  • @joeQ Thanks man.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.