Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Closing Newwindow.
QtWS25 Last Chance

[SOLVED]Closing Newwindow.

Scheduled Pinned Locked Moved General and Desktop
push buttonmedia playerqt5
3 Posts 2 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JasonB
    wrote on 29 Jun 2015, 09:44 last edited by JasonB
    #1

    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.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      joeQ
      wrote on 29 Jun 2015, 10:45 last edited by joeQ
      #2

      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
      }
      

      Just do it!

      J 1 Reply Last reply 29 Jun 2015, 14:09
      0
      • J joeQ
        29 Jun 2015, 10:45

        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
        }
        
        J Offline
        J Offline
        JasonB
        wrote on 29 Jun 2015, 14:09 last edited by
        #3

        @joeQ Thanks man.

        1 Reply Last reply
        0

        1/3

        29 Jun 2015, 09:44

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved