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] Parent and Child Window Communication in Qt - Child Window opens twice
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.3k Views 1 Watching
  • 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 Offline
    S Offline
    srivatsa
    wrote on last edited by
    #1

    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
    0
    • T Offline
      T Offline
      tzander
      wrote on last edited by
      #2

      did you double click on the button?

      did you add a signal connection in your designer file?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        srivatsa
        wrote on last edited by
        #3

        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
        0
        • T Offline
          T Offline
          tzander
          wrote on last edited by
          #4

          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
          0
          • S Offline
            S Offline
            srivatsa
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

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