Arun K
Arun K

Reputation: 423

Opening child window after n seconds from formation of parent window in Qt C++

I have three windows Mainwindow,firstwindow and secondwindow.I have a pushbutton in mainwindow,When i clicked the push button it opens the firstwindow.Up to this part everything is ok. What i need is , I need to open secondwindow automaticaly after 2sec of the formation of firstwindow

What is happening now is when i clicking the push button in mainwindow, secondwindow will appear first,and the first window will form only after execution of second window.

I am using qt5 with qtcreator

MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

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


void MainWindow::on_clickMeButton_clicked()
{
    FirstWindow * dlg = new FirstWindow;
    dlg->show();
}

FirstWindow.cpp

#include "firstwindow.h"
#include "ui_firstwindow.h"

FirstWindow::FirstWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FirstWindow)
{

    ui->setupUi(this);
    connect(this,SIGNAL(winMessage()),this,SLOT(openNewWindow()));
    emit winMessage();

}



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

void FirstWindow::openNewWindow()
{
    dlg = new SecondWindow;
    dlg->show();

}

SecondWindow.cpp

**

#include "secondwindow.h"
#include "ui_secondwindow.h"
SecondWindow::SecondWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SecondWindow)
{
    ui->setupUi(this);
}
SecondWindow::~SecondWindow()
{
    delete ui;
}

**

Upvotes: 0

Views: 560

Answers (1)

Luca Carlon
Luca Carlon

Reputation: 9986

My understading is you want something like this:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QPushButton>

class FirstWindow : public QWidget
{
    Q_OBJECT
public:
    FirstWindow() : QWidget() { resize(100, 100); }
};

class SecondWindow : public QWidget
{
    Q_OBJECT
public:
    SecondWindow() : QWidget() { resize(100, 100); }
};

class MainWindow : public QPushButton
{
    Q_OBJECT
public:
    MainWindow(QWidget* parent = nullptr);
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include <QTimer>
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QPushButton(parent)
{
    setText("Click me");
    connect(this, &MainWindow::clicked, this, [this] {
        FirstWindow* first = new FirstWindow;
        first->show();
        QTimer::singleShot(2000, this, [] {
            (new SecondWindow)->show();
        });
    });
}

If you prefer to separate the implementations, you can start the timer in the first window; the timer will give time to return to the event loop and actually show the first window.

#include <QTimer>
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QPushButton(parent)
{
    setText("Click me");
    connect(this, &MainWindow::clicked, this, [] {
        FirstWindow* first = new FirstWindow;
        first->show();
    });
}

FirstWindow::FirstWindow()
{
    resize(100, 100);
    QTimer::singleShot(2000, this, [] {
        (new SecondWindow)->show();
    });
}

or more precisely, you may wait for the actual show event:

#include <QTimer>
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QPushButton(parent)
{
    setText("Click me");
    connect(this, &MainWindow::clicked, this, [] {
        FirstWindow* first = new FirstWindow;
        first->show();
    });
}

void FirstWindow::showEvent(QShowEvent *)
{
    QTimer::singleShot(2000, this, [] {
        (new SecondWindow)->show();
    });
}

Upvotes: 1

Related Questions