PDA

View Full Version : How to show a QDialog ( Designed ) ?



Bong.Da.City
17th August 2010, 16:53
I had created a new design form but i do not know how to show it
Take a look to the example i have created ...
Because i don't know why it doesn't let me to attach a rar file i have uploaded the example to megaupload.. Download link: Here (http://www.megaupload.com/?d=3IN2LIDB)

http://i36.tinypic.com/343ng2c.jpg

This documentation for signal and slots (http://doc.trolltech.com/4.6/signalsandslots.html) cannot help me so i would appreciate if you can edit my project..

Zlatomir
17th August 2010, 17:05
Try the examples, the simple ones first and then advance to more complicated ones, to understand the signal/slot mechanism, to understand how to write your own signal and slots and how to connect them.

A hint for your project (this is just one way to achieve this, depend on what functionality you want in your application)
You can create a slot in your main window (the one with the Button) and in this slot you can create an instance of the dialog (create it on the heap and pass 'this' pointer as parent, so that the main window will be parent of your dialog) and then show the created dialog.
Of course in the main window class constructor you will connect the button's clicked signal with the slot you created (careful on how you access the button Hint: need to use ui->Your_Button_Name)

Bong.Da.City
17th August 2010, 17:23
You know the documentation for signal and slots doesn't answer me directly to my question... I want when i press for example a button, a new Qdialog to pop up.
I can show a Qdialog with this..

QDialog *gamatosdialog = new QDialog;
gamatosdialog->show();

But i do not know how to design this QDialog... Show i thought it is a good idea to make a new design form .. So when you press a button for example this design form to be a dialog and pop up... I still cannot undersdand how to do this..

P.s Maybe somehow you connect the design form with a new Qdialog? I do not know.. :(:(

Urthas
17th August 2010, 17:43
You know the documentation for signal and slots doesn't answer me directly to my question

Are you sure about that? :)


connect(yourButton, SIGNAL(clicked()), yourForm, SLOT(showNewDialog());

Zlatomir
17th August 2010, 17:43
You know the documentation for signal and slots doesn't answer me directly to my question...

Indeed it doesn't answer directly to your question, but you need to understand how signal/slot and connect "mechanisms" work, then this won't be so "complicated"


QDialog *gamatosdialog = new QDialog;
gamatosdialog->show();

Almost correct (this is the way to create and show a dialog)

But (as i said before) you need to:
1) create a slot (witch is just a member function only that has "public slots:" access specifier) in the main window class
2) this slot will contain the code witch create and show (or exec) the dialog, with the only modification that you will pass 'this' pointer as a parent to the dialog
3) in the main window constructor you will connect the ui->NameOfButton signal clicked with the slot that you created (the one that create and show the dialog)

Bong.Da.City
17th August 2010, 18:54
void example::on_pushButton_clicked()
{
QDialog *gamatos = new QDialog;
connect(ui->pushButton, SIGNAL(clicked()), hi, SLOT(gamatos->show()));
}

This doenot work!!! :(

Urthas
17th August 2010, 18:57
Of course it doesn't. :) You never make a connection in the slot that it is supposed to call.

edit:



void example::on_pushButton_clicked()
{
QDialog *gamatos = new QDialog;
connect(ui->pushButton, SIGNAL(clicked()), hi, SLOT(gamatos->show()));
}


should be something along the lines of



yourClass::yourConstructor(...) : yourParent() {
...
connect(ui->pushButton, SIGNAL(clicked()), hi, SLOT(on_pushButton_clicked());
...
}

void example::on_pushButton_clicked()
{
QDialog *gamatos = new QDialog;
...
gamatos->exec();
}


You *really* need to read and re-read the signals and slots documentation very carefully until you understand it. A slot isn't a statement, it's a function name, and the connection must be established outside of the slot otherwise the slot *cannot* be called!

Zlatomir
17th August 2010, 19:04
You don't connect in the slot...

void example::on_pushButton_clicked()
{
QDialog *gamatos = new QDialog(this); //you need to pass 'this' as parent so that you don't get a memory leak
gamatos->show(); //you show the dialog
}

Now the connect "stuff" if you create slots by the autoconnect (in the end of that article) (http://doc.qt.nokia.com/4.0/designer-using-a-component.html) rules you don't need to manually connect (the autoconnect does the connection for you)

Else (and i really recommend this method) you do the connection in the class constructor, something like:


example::example // : parameter passing...
{//...
connect(ui->NameOfButton, SIGNAL (clicked()), this, SLOT(on_pushButton_clicked()));
//... rest of the constructor code
}

Bong.Da.City
17th August 2010, 19:09
See this... It still doesn't work ..


#include "example.h"
#include "ui_example.h"
#include <QDialog>

example::example(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::example)
{
ui->setupUi(this);
QDialog *gamatos = new QDialog(this);
connect(ui->pushButton, SIGNAL(clicked()), hi.ui, SLOT(gamatos()));

}

example::~example()
{
delete ui;
}

void example::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

void example::on_pushButton_clicked()
{
connect(ui->pushButton, SIGNAL (clicked()), this, SLOT(on_pushButton_clicked()));
}

Urthas
17th August 2010, 19:12
And where is the gamatos() method, exactly? Stop thrashing. Read your error messages! Look at the code that has been handed to you...

Zlatomir
17th August 2010, 19:12
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));

That 'this' is a pointer to the object itself (the main window in your case) and this has the slot that will create and show the dialog (on_pushButton_clicked() in your case )

LE: the slot should be:


void example::on_pushButton_clicked()
{
QDialog *gamatos = new QDialog(this); //you need to pass 'this' as parent so that you don't get a memory leak
gamatos->show(); //you show the dialog
}

hakermania
17th August 2010, 19:22
I don't know if you understand the problem. I understood the Bong's problem. His has 2 ui files. One ui is its main file. The other ui is specially designed for the QDialog. How to connect the other.ui file with the push Button???

Zlatomir
17th August 2010, 19:30
I understood his problem, and the solution is in front of you:


#include "example.h"
#include "ui_example.h"
#include "what_ever_dialog_class_with_ui.h" //include the header file

example::example(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::example)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked())); //connect... the signal with slot ...

}

example::~example()
{
delete ui;
}

void example::on_pushButton_clicked()
{
//use the other class to instantiate the QDialog or the QDialog derived class (with or without ui)
what_ever_dialog_class_with_ui *gamatos = new what_ever_dialog_class_with_ui(this); //you need to pass 'this' as parent so that you don't get a memory leak
gamatos->show(); //you show the dialog
}

void example::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

Bong.Da.City
17th August 2010, 19:42
I cannot understand what should i change on what_ever_dialog_class_with_ui ? look what i have done..


#include "example.h"
#include "ui_example.h"
#include "hi.h" //include the header file

example::example(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::example)

{
ui->setupUi(this);
QDialog *gamatos = new QDialog(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked())); //connect... the signal with slot ...
}

example::~example()
{
delete ui;
}

void example::on_pushButton_clicked()

{
//use the other class to instantiate the QDialog or the QDialog derived class (with or without ui)
hi.h *gamatos = new QDialog(this); //you need to pass 'this' as parent so that you don't get a memory leak
gamatos->show(); //you show the dialog
}

void example::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

Zlatomir
17th August 2010, 19:46
#include "example.h"
#include "ui_example.h"
#include "hi.h" //include the header file

example::example(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::example)
{
ui->setupUi(this);

// note that the declaration of pointer can be done only once

connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked())); //connect... the signal with slot ...

}

example::~example()
{
delete ui;
}

void example::on_pushButton_clicked()
{
//use the other class to instantiate the QDialog or the QDialog derived class (with or without ui)

hi *gamatos = new hi(this); //this is valid if the class name is "hi"... replace that with your class name //you need to pass 'this' as parent so that you don't get a memory leak

gamatos->show(); //you show the dialog
}

void example::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}


NOTE: the code changed a bit... i corrected in the previous post, but you already copied... you can't declare the pointer twice (you don't declare it in the constructor, only in the slot)

Bong.Da.City
17th August 2010, 19:48
Ok i founded it.. .But know two Qdialogs are been shown :P


#include "example.h"
#include "ui_example.h"
#include "hi.h" //include the header file

example::example(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::example)

{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked())); //connect... the signal with slot ...
}

example::~example()
{
delete ui;
}

void example::on_pushButton_clicked()

{
//use the other class to instantiate the QDialog or the QDialog derived class (with or without ui)
hi *gamatos = new hi(this); //you need to pass 'this' as parent so that you don't get a memory leak
gamatos->show(); //you show the dialog
}

void example::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

Zlatomir
17th August 2010, 19:51
See the constructor... and the edits of my previous posts... you initialize the second dialog twice (create two dialogs... with two pointers that have the same name...)

Zlatomir
17th August 2010, 19:56
Or, maybe the name of the slot match the autoconnect rules (i gave you a link in a previous post)

You can rename the slot: something like


void example::openDialog() //and connect with this slot name
{
//use the other class to instantiate the QDialog or the QDialog derived class (with or without ui)
hi *gamatos = new hi(this); //you need to pass 'this' as parent so that you don't get a memory leak
gamatos->show(); //you show the dialog
}


Or don't connect manually and let autoconnect do the connection

Bong.Da.City
17th August 2010, 20:01
Ok it works... Many thanks.. I need it.. :)