Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED] Parent and Child Window Communication in Qt - Child Window opens twice

    General and Desktop
    2
    5
    4159
    Loading More Posts
    • 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.
    • S
      srivatsa last edited by

      I have two windows, one parent and one child. In parent window, I have a Next button, which onClick()'ed, opens up child window, but in my case two child windows are opening, what is the mistake am doing!?

      Here are my codes:

      .h files

      mainwindow.h

      @#ifndef MAINWINDOW_H
      #define MAINWINDOW_H

      #include <QMainWindow>
      #include <info.h>
      #include <QtGui>

      namespace Ui {
      class MainWindow;
      }

      class MainWindow : public QMainWindow {
      Q_OBJECT
      public:
      MainWindow(QWidget *parent = 0);
      ~MainWindow();

      protected:
      void changeEvent(QEvent *e);

      private:
      Ui::MainWindow *ui;
      void setSignals();

      private slots:
      void process();
      };

      #endif // MAINWINDOW_H@

      info.h

      @#ifndef INFO_H
      #define INFO_H

      #include <QMainWindow>

      namespace Ui {
      class info;
      }

      class info : public QMainWindow {
      Q_OBJECT
      public:
      info(QWidget *parent = 0);
      ~info();

      protected:
      void changeEvent(QEvent *e);

      private:
      Ui::info *ui;
      };

      #endif // INFO_H@

      .cpp files

      mainwindow.cpp

      @#include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QtGui/QApplication>

      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ui->setupUi(this);
      setSignals();
      }

      MainWindow::~MainWindow()
      {
      delete ui;
      }

      void MainWindow::changeEvent(QEvent *e)
      {
      QMainWindow::changeEvent(e);
      switch (e->type()) {
      case QEvent::LanguageChange:
      ui->retranslateUi(this);
      break;
      default:
      break;
      }
      }

      void MainWindow::setSignals(){
      connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(process()));
      connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(close()));
      }

      void MainWindow::process(){
      info *i;
      i = new info;
      this -> hide();
      i -> show();
      }@

      info.cpp

      @#include "info.h"
      #include "ui_info.h"

      info::info(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::info)
      {
      ui->setupUi(this);
      }

      info::~info()
      {
      delete ui;
      }

      void info::changeEvent(QEvent *e)
      {
      QMainWindow::changeEvent(e);
      switch (e->type()) {
      case QEvent::LanguageChange:
      ui->retranslateUi(this);
      break;
      default:
      break;
      }
      }@

      main.cpp

      @#include <QtGui/QApplication>
      #include "mainwindow.h"

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();
      return a.exec();
      }@

      1 Reply Last reply Reply Quote 0
      • T
        tzander last edited by

        did you double click on the button?

        did you add a signal connection in your designer file?

        1 Reply Last reply Reply Quote 0
        • S
          srivatsa last edited by

          Thank you Thomas for your reply. I didn't double click on button... Signals and Slots are assigned in the first Window, are they required in second window? Please explain...

          Yeah here is the ui_mainwindow.h file:

          @pushButton = new QPushButton(widget);
          pushButton->setObjectName(QString::fromUtf8("pushButton"));

              horizontalLayout->addWidget(pushButton);
          
              pushButton_2 = new QPushButton(widget);
              pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
          
              horizontalLayout->addWidget(pushButton_2);
          
              MainWindow->setCentralWidget(centralWidget);
              mainToolBar = new QToolBar(MainWindow);
              mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
              MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
          
              retranslateUi(MainWindow);
              QObject::connect(pushButton, SIGNAL(clicked()), MainWindow, SLOT(process()));
              QObject::connect(pushButton_2, SIGNAL(clicked()), MainWindow, SLOT(close()));
          
              QMetaObject::connectSlotsByName(MainWindow);@
          
          1 Reply Last reply Reply Quote 0
          • T
            tzander last edited by

            Ah, that explains it :)

            Your ui_mainwindow.h file has a connect, which means you added it in the QtDesigner.
            You again have the same connect in void MainWindow::setSignals(), which I guess you typed manually.
            So with two connects, you get two calls to process()

            Remove one set of connects to solve this :)

            1 Reply Last reply Reply Quote 0
            • S
              srivatsa last edited by

              :) Yeah you are absolutely right.... thanks a lot...

              1 Reply Last reply Reply Quote 0
              • First post
                Last post