Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PopUP Window in Qt

Tags:

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.

like image 922
Elseine Avatar asked Aug 05 '13 07:08

Elseine


People also ask

How do you make a pop up window QT?

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.

What is dialog in Qt?

Qt provides a set of ready-made dialogs for file, font, color-selection and more. QColorDialog. Dialog widget for specifying colors. QFileDialog. Dialog that allow users to select files or directories.


2 Answers

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.

like image 115
Mykola Niemtsov Avatar answered Nov 14 '22 10:11

Mykola Niemtsov


  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

like image 39
McLan Avatar answered Nov 14 '22 09:11

McLan