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.
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.
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.
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.
Include your pop-up class in your MainWindow.h
:
include "popup.h"
Define a pointer to your pop-up class in the MainWindow.h
:
popup1 *mpPopUp1;
Create an object in the MainWindow.cpp
file:
mpPopUp1 = new popup1;
Define a slot in MainWindow.h
and call it, for example, showPopUp()
:
void showPopUp();
Create the slot showPopUp()
in your MainWindow.cpp
and write the following statement inside it:
mpPopUp1 ->show();
Connect your pushButton
to the slot showPopUp()
:
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(showPopUp()));
Run you app, click on pushButton
and voila
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With