PDA

View Full Version : Open a window with a QAction in QToolBar



gen_mass
18th July 2010, 09:59
Hi,

I have a mainwindow with a toolbar. When the user clicks to a tool named DialogBox, I want my QWidget ''dialogue'' to open, so that the user can fill the questions ask in it. So, I tried to link the click signal of that tool to a slot (open_dialog()) where the coding of QWidget 'dialogue'' is,including a dialogue.show().
But that doesn't work...how can I do this ?


mainwindow::mainwindow(QWidget*p)
{

QWidget *central= new QWidget(0);
........
...........
.........
central->setLayout(layout);

setCentralWidget(central);


// Tool bar creation
QToolBar *toolBarFichier = addToolBar("Fichier");

QAction *actionQuitter = toolBarFichier->addAction("&Quit");
toolBarFichier->addAction(actionQuitter);
actionQuitter->setShortcut(QKeySequence("Ctrl+Q"));
actionQuitter->setIcon(QIcon("quit.png"));
connect(actionQuitter, SIGNAL(triggered()), qApp, SLOT(quit()));

//Informations about patient
QAction *actionDialogue = toolBarFichier->addAction("&Dialog box");
toolBarFichier->addAction(actionDialogue);
actionDialogue->setShortcut(QKeySequence("Ctrl+Q"));
actionDialogue->setIcon(QIcon("file.png"));
QObject::connect(actionDialogue, SIGNAL(triggered()), qApp, SLOT(open_dialog()));


}

void mainwindow::open_dialog()
{
QWidget dialogue;

QPalette pal = dialogue.palette();
pal.setColor(dialogue.backgroundRole(),Qt::white);
dialogue.setPalette(pal);

QLabel *intro = new QLabel("Informations relatives à la patiente");
QFont *font1;

QLabel *nameLabel = new QLabel("Nom:");
QLineEdit *nameLineEdit = new QLineEdit;

QLabel *prenomLabel = new QLabel("Prénom:");
QLineEdit *prenomLineEdit = new QLineEdit;

QLabel *ageLabel = new QLabel("Âge:");
QLineEdit *ageLineEdit = new QLineEdit;

QLabel *poidsLabel = new QLabel("Poids (kg):");
QLineEdit *poidsLineEdit = new QLineEdit;

QLabel *nbr_accLabel = new QLabel("Nombre d'accouchements vécus :");
QLineEdit *nbr_accLineEdit = new QLineEdit;


QPushButton *bouton1 = new QPushButton("Enregistrer ");
QPushButton *bouton2 = new QPushButton("Terminer");

QGridLayout *layout = new QGridLayout;
layout->addWidget(intro,0, 0);
layout->addWidget(nameLabel, 2, 0);
layout->addWidget(nameLineEdit, 2, 2,1,3);
layout->addWidget(prenomLabel, 3, 0);
layout->addWidget(prenomLineEdit, 3,2, 1,3);
layout->addWidget(ageLabel, 4, 0);
layout->addWidget(ageLineEdit, 4, 2);
layout->addWidget(poidsLabel, 5, 0);
layout->addWidget(poidsLineEdit, 5, 2);
layout->addWidget(nbr_accLabel, 6, 0);
layout->addWidget(nbr_accLineEdit, 6, 2);
layout->addWidget(bouton1, 8, 3);
layout->addWidget(bouton2, 8, 4);

dialogue.setLayout(layout);


dialogue.show();

}

Thanks for your helps !

Lykurg
18th July 2010, 10:02
It is a slot of your mainwindow and not of the application! Use
QObject::connect(actionDialogue, SIGNAL(triggered()), this, SLOT(open_dialog())); Also make sure you have declared open_dialog as a slot.

Zlatomir
18th July 2010, 10:08
Aren't you get some errors that the slot doesn't exist?

I think is because of this line:


//QObject::connect(actionDialogue, SIGNAL(triggered()), qApp, SLOT(open_dialog()));
//should be:
QObject::connect(actionDialogue, SIGNAL(triggered()), this, SLOT(open_dialog()));
//as i see from your code the open_dialog slot is member of MainWindow

And also:


void mainwindow::open_dialog()
{
//QWidget dialogue; //you might want to allocate dynamically and pass this pointer as a parent
//something like this:
QWidget *dialogue = new QWidget(this);
//or even use the QDialog class: QDialog *dialogue = new QDialog(this);
//...

gen_mass
18th July 2010, 10:51
oops right!!
thanks !!

I still have a little problem ....on my QDialog widget (I used the QDialog class instead of just QWidget, as proposed by Zlatomir)
I have a button to close it with the connection of the quit slot, but when I use it, my mainwindow closes at the same time, and I only want the QDialog widget to close...


void mainwindow::ouvrir_dialogue()
{
QDialog *dialogue = new QDialog(this);
dialogue->setModal(true);
QPalette pal = dialogue->palette();
pal.setColor(dialogue->backgroundRole(),Qt::white);
dialogue->setPalette(pal);

.......................
.........................

QPushButton *bouton1 = new QPushButton("Enregistrer ");
QPushButton *bouton2 = new QPushButton("Terminer");

...........................

dialogue->show();

QObject::connect(bouton1, SIGNAL(clicked()), this, SLOT(save()));
QObject::connect(bouton2, SIGNAL(clicked()), qApp, SLOT(quit()));

}

Zlatomir
18th July 2010, 11:28
Try like this:


QObject::connect(bouton2, SIGNAL(clicked()), dialogue, SLOT(quit()));


LE: You need to connect the Sender (Object), sender Signal, receiver (object) here don't just use the application object, and receiver Slot()