PopUP Window in Qt - 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.

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

Related

Qt: Modal from a modal (Mac)

I have a Qt modal dialog (a complex form) that needs to itself pop up a modal dialog (a QMessageBox with setModal(true)). This modal should be on top of the parent modal form, and prevent interaction.
This all works swimmingly up to a point - the second modal appears, and prevents interaction with the widgets in the parent.
However, the parent form can still receive focus - that is, I can click in it, and the window receives focus (even if I can't interact with the widgets). This becomes a problem if you task away to another application at this point - when you task back the QMessageBox is behind the parent which has focus (and you can't interact with the parent). Basically, you have to move the parent to reveal the QMessageBox before dismissing it.
Is there a way to have a modal on top of a modal on prevent this problem on Mac OS with window focus and tasking away? (Not tested other OS BTW).
Example code to reproduce the problem:
Dialog.cpp
pushButton is just a standard pushbutton.
#include "dialog.h"
#include "ui_dialog.h"
#include <QMessageBox>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QObject::connect(ui->pushButton, &QPushButton::clicked, this, &Dialog::showMeAModal);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::showMeAModal()
{
QMessageBox box(this);
box.setText(tr("modal"));
box.setModal(true);
box.setWindowFlags(box.windowFlags() | Qt::Popup);
box.exec();
}
Dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
public slots:
void showMeAModal();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
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;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
Dialog dialog(&w);
dialog.setModal(true);
dialog.show();
return a.exec();
}
I've tried:
Qt::Popup on the Message Box
Changing parent to MainWindow
Qt::WindowStaysOnTopHint on Message Box
...to no avail.

Bogus widget in Qt Creator

I'm a Qt newbie, working in Qt Creator 3.1.2 (Ubuntu Linux), Qt 5.3.1.
My program has a form with a button (pushButton) which changes the value of a text field (plainTextEdit) on being pressed. Both pushButton and plainTextEdit have been added in graphical mode. Connection between the button and its slot (on_pushButton_clicked()) has been set up via the graphical interface too.
The problem is, the program produces a bogus plainTextEdit, i.e. a different one, in the upper left corner, where the output goes to, while the "main" one stays clean. The question hence is, how I can avoid it? In general, how should I connect graphical widgets and their counterparts in the code? Here is my program:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QPlainTextEdit>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QPushButton *pushButton;
QPlainTextEdit *plainTextEdit;
};
#endif // MAINWINDOW_H
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
plainTextEdit = new QPlainTextEdit(this);
// whenever I remove the previous line, I get SIGSEGV
setWindowTitle(tr("My test app..."));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
plainTextEdit->setPlainText("Some text here");
}
It's the widget you're creating in your constructor that is "bogus". The widgets you created in the forms editor belong to the Ui::MainWindow, you must not re-create them in your main window.
Remove this from your MainWindow:
QPushButton *pushButton;
QPlainTextEdit *plainTextEdit;
Remove the widget creation from the constructor:
plainTextEdit = new QPlainTextEdit(this);
Change your on_pushButtonClicked member to:
ui->plainTextEdit->setPlainText("Some text here");

How to use qAction submenu in Qt

I want to implement simple commands like a qDebug() when I click on a sub menu in the mainwindow. I was referring to sample program given along with the Qt 5 IDE (...\Qt\Qt5.2.0\5.2.0\msvc2010\examples\widgets\mainwindows\menus), and using it, I managed to construct the code. I do not receive any compile time or run time errors.
I created the mainwindow.ui using the design mode. It has an object of the QAction class called actionInterval.
But when I click on it, nothing happens, I am not able to implement the command in void interval(). I guess I am not connecting properly. What am I missing here? Please advise.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
void createActions();
private slots:
void interval();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
createActions();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createActions()
{
ui->actionInterval = new QAction(tr("&Interval"), this);
ui->actionInterval->setStatusTip(tr("Set the interval for capturing delta & reference images"));
connect(ui->actionInterval, SIGNAL(triggered()), this, SLOT(interval()));
}
void MainWindow::interval()
{
qDebug()<<"inside interval qdialog";
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
void MainWindow::createActions()
{
ui->actionInterval->setStatusTip(tr("Set the interval for capturing delta & reference images"));
connect(ui->actionInterval, SIGNAL(triggered()), this, SLOT(interval()));
}
You shouldn't need that ui->actionInterval = new QAction(tr("&Interval"), this); line, the ui->setupUi() handles that for you, so it's potentially causing an incorrect reference so when you do click on it it's not firing correctly.

How to run thread from push button?

main.cpp:
#include "mainwindow.h"
#include "myobject.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QThread cThread;
MyObject cObject;
cObject.doSetup(cThread);
cObject.moveToThread(&cThread);
// cThread.start();
return a.exec();
}
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_pushButton_clicked()
{
// cThread.start();
}
I'm trying to start the thread on on_pushButton_clicked(). In order to do this, I would have to pass cThread to mainwindow through an argument, right? I just want someone to confirm to make sure I'm doing it right.
This seems like one way to do that. The other one would be to connect signal clicked() of button to thread's slot start(Priority) in your application which will be nicer and shorter.

Storing variables from dialog and using it in a qvtkwidget

I've created a dialog where I ask for some lengths of a number of slices, as xmax, ymax, and zmax as the number of slices. I intend to use those numbers in the mainwindow in the qvtkwidget. I'll simplify and make the example to only one variable so you can understand and help me.
Here's my dialog.cpp
#include <QtGui/QApplication>
#include <QDir>
#include <iostream>
using namespace std;
#include "dialog.h"
#include "ui_dialog.h"
// Create getters to transfer variables to main.cpp
double Dialog::getxpax()
{
return xpax;
}
// Start the mainwindow
void Dialog::startplanevolume()
{
// Getting some proprieties for the lenght of the volume
QString XMAX=ui->lineEdit->text();
xpax=XMAX.toDouble();
if (xpax==0)
{
ui->label_17->setText("Error: Can't start, invalid \nmeasures");
ui->label_17->setStyleSheet("QLabel { color : red; }");
}
else
{
this->accept();
}
}
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
// Control volume measures
// Making the lineedit objects only accept numbers
ui->lineEdit->setValidator(new QDoubleValidator(this));
// Start planevolume
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(startplanevolume()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(hide()));
}
The pushbutton is the ok button and the pushbutton_2 is the cancel button.
In my mainwindow I created a setter function to set the value of the xmax.
here's some code.
// Get stored data from dialog
void planevolume::setxpax(double xpax)
{
xp=xpax;
}
and when I use qDebug() the xp inside the settter shows me that the xp actually gets the xpax value.
here's my main.cpp
#include <QtGui/QApplication>
#include <iostream>
using namespace std;
#include "planevolume.h"
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Dialog *dialog= new Dialog;
if (dialog->exec())
{
planevolume mainwindow;
mainwindow.setxpax(dialog->getxpax());
mainwindow.show();
return app.exec();
}
return 0;
}
So the only problem is that is here, at mainwindow as planevolume.cpp when I need it, the value has not been set,
planevolume::planevolume(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::planevolume)
{
ui->setupUi(this);
// My vtk statements are here in the code, but they are
// executed before the setter gives the value to my new
// variable xp, so when I need the value it has not been set yet.
Any ideas guys?
If the planevolume constructor needs these data, they could be passed as parameter to the constructor itself (you can also make the dialog return all the variables inside a struct to have a single parameter to pass, instead of having an accessor for each one).
Another solution would be to call the vtk part after the event loop has started by putting it in a slot and schedule its execution with a QTimer::singleShot.
In that last case, your code should like this:
planevolume::planevolume(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::planevolume)
{
ui->setupUi(this);
QTimer::singleShot(0, this, SLOT(setupVtk()));
}
// declared as a slot in the class
void planevolume::setupVtk()
{
// Your VTK statements would be here
}

Resources