Elseine
Elseine

Reputation: 753

PopUP Window in Qt

I am creating an application in Qt and I have a problem. I have a main window and I want that when I push a button, a popup window appears. I dont know how can I do it. I tried to call the show() method when I push the button but dont work. I think that I must use the exec() method from QApplication but I dont know how can call it if I created it in the main class.

My classes:

#include "mainwindow.h"
#include "dialog.h"
#include <QApplication>
#include "popup1.h"

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

return a.exec();
}

MainWindow:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QApplication>
int posiciones[10];
std::string port="";
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
for (int i=1; i<10; i++){
    if(i==7){
        posiciones[i]=90;
    }
    posiciones[i]=0;
}
//Mandar el vector para mover
ui->setupUi(this);
}

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

PopUp:

#include "popup1.h"
#include "ui_popup1.h"

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

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

Anyone knows how can I show the popup window? Thanks for your time.

Upvotes: 7

Views: 46036

Answers (2)

McLan
McLan

Reputation: 2688

  1. Include your pop-up class in your MainWindow.h:

    include "popup.h"

  2. Define a pointer to your pop-up class in the MainWindow.h:

    popup1 *mpPopUp1;

  3. Create an object in the MainWindow.cpp file:

    mpPopUp1 = new popup1;

  4. Define a slot in MainWindow.h and call it, for example, showPopUp():

    void showPopUp();

  5. Create the slot showPopUp() in your MainWindow.cpp and write the following statement inside it:

    mpPopUp1 ->show();

  6. Connect your pushButton to the slot showPopUp():

    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(showPopUp()));

  7. Run you app, click on pushButton and voila

Upvotes: 4

Mykola Niemtsov
Mykola Niemtsov

Reputation: 540

Connect your button signal clicked() with exec() slot of your popup window:

connect(pushButton, SIGNAL(clicked()), popupWindow, SLOT(exec()));

Where pushButton - pointer to your button, and popupWindow - pointer to your popup window. You can write this code in QMainWindow constructor.

Upvotes: 13

Related Questions