PDA

View Full Version : Qt show modal dialog (.ui) on menu item click



tomorrow
15th November 2013, 20:06
I have created several menu items in Qt but I do not know how to connect them to to the qtdialog which I created
for the menu items :
mainwindow.h


........

void get_started_info();

private:
Ui::MainWindow *ui;
//! Create the menu classes
void createActions();
void createMenus();
//! Menu Items
QMenu *home;
QMenu *help;
QAction *quit;
QAction *start;

};

#endif // MAINWINDOW_H


mainwindow.cpp


void MainWindow::createActions()
{
QPixmap startpix("../src/img/start.png");

start = new QAction(startpix,"&Get Started",this);
start->setStatusTip(tr("Learn how to use this program"));
connect(start,SIGNAL(triggered()),qApp,SLOT(get_st arted_info()));

}

void MainWindow::createMenus()
{
home = menuBar()->addMenu(tr("&Home")); //! Adding a home item
help = menuBar()->addMenu(tr("H&elp")); //! Adding a help item
help->addAction(start); //! Adding About under the help

}



in the mainwindow.cpp I also have this class which should show the dialog


void MainWindow::get_started_info()
{
get_started one(this); //create a object directly
one.exec(); //modal dialog
}



I also have get_started.h / get_started.cpp / get_strated.ui

I think there is an issue with this lines :


start = new QAction(startpix,"&Get Started",this);
start->setStatusTip(tr("Learn how to use this program"));
connect(start,SIGNAL(triggered()),qApp,SLOT(get_st arted_info()));


is it possible to show the dialog when the "Get Started" is clicked from the menu ?
thank you

toufic.dbouk
15th November 2013, 20:48
is it possible to show the dialog when the "Get Started" is clicked from the menu ?
Yes it is possible. In the the triggered Slot of the QAction you are using show the dialog you want.
For example:
void MainWindow::on_start_triggered()
{
one = new get_started(this); // make sure to have it as class member or use the exec() function if you create the instance in the slot without being a member.
one->setModal(true); // if you need it.
one->show();
}


connect(start,SIGNAL(triggered()),qApp,SLOT(get_st arted_info()));
why are you using qApp as a parent here ? your slot is in the current class so you should use this instead.

Good Luck.

tomorrow
15th November 2013, 21:20
thank you for your response

why are you using qApp as a parent here ? your slot is in the current class so you should use this instead.
sorry I was following a tutorial and thats how they did it, I changed it to "this"


Yes it is possible. In the the triggered Slot of the QAction you are using show the dialog you want.
I tried the code and had this :


void MainWindow::get_started_info()
{
one = new get_started(this);
one->setModal(true); // if you need it.
one->show();

//get_started one(this); //create a object directly
//one.exec(); //modal dialog
}


and used this to connect :


start = new QAction(startpix,"&Get Started",this);
start->setStatusTip(tr("Learn how to use this program"));
connect(start,SIGNAL(triggered()),this,SLOT(get_st arted_info()));


but it says:
- "one" was not declared in this scope
- expected type-specified before 'get_started'
- expected ';' before get_started

here are the files for get_started

get_started.h :


#ifndef GET_STARTED_H
#define GET_STARTED_H

#include <QDialog>
#include "ui_get_started.h"

class get_started : public QDialog
{
Q_OBJECT
public:
explicit get_started(QWidget *parent = 0);

private:
Ui:: get_started *ui;
};


#endif // GET_STARTED_H


get_started.cpp


#include "get_started.h"
#include "ui_get_started.h"

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

ui->setupUi(this);
}


I have been trying to solve this issue for several hours now, really appreciate if you help me solve it

toufic.dbouk
15th November 2013, 21:31
Add a qDebug () in your get_started_info() slot to check if it is getting called when the triggered signal is fired. Try to create a QMessageBox to see if it will show.

QMessageBox::information(this,"title","message");// dont forget to include QMessageBox
You can go to the UI file in the designer and go to the action editor and right click your action , go to slot and press on triggered ( it will then automatically create a slot for you in your mainwindow ) and inside it create your dialog.
Did you create a class memember from your dialog ?

get_started *one; //in your mainwindow under private

Good Luck.

tomorrow
15th November 2013, 21:51
void MainWindow::get_started_info()
{
QMessageBox::information(this,"About","message");// dont forget to include QMessageBox
}

this works, when I click on Get Started, this message box appears


get_started *one; //in your mainwindow under private
like this :


private:
Ui::MainWindow *ui;
//! Create the menu classes
void createActions();
void createMenus();
//! Menu Items
QMenu *home;
QMenu *help;
QAction *quit;
QAction *start;
QAction *about;
get_started *one;


but I get an error 'get_started' does not name a type

toufic.dbouk
15th November 2013, 22:00
but I get an error 'get_started' does not name a type
Did you include get_started.h ?
And you have
#include "ui_get_started.h"
I am not sure if you need this to be included in both header and cpp file, maybe just in your .cpp file.

Good Luck.

anda_skoa
16th November 2013, 08:53
The original code was almost correct, the only mistake was usign qApp as the receiver object instead of this.

Creating the dialog on the stack and excuting it as a modal dialog using exec() where correct.

Cheers,
_

toufic.dbouk
16th November 2013, 09:58
Creating the dialog on the stack and excuting it as a modal dialog using exec() where correct.
Yes it was correct and we mentioned that he can keep his code since its right instead of creating it on heap. Creating it on the stack will limit the scope where you can use the object for further usage.