PDA

View Full Version : Dialog is not closing



manmohan
27th November 2008, 18:32
Hi dudes,

I am a newbie to qt and I have a small problem...

I created 2 dialogs with 2 push buttons, and 2 labels to indicate the dialogs, I have generated a simple code using which I am able to open the first dialog screen and when I click on the first dialog's push button the second screen is poping up but I am unable to close the first dialog completely.......

The basic need is click on the first dialogs push button it should pop up the second dialog and close the first dialog completely, when I click on the second dialogs push button it should close the second dialog completely and the push up the first dialog..... vice versa... so...

Seems to be a crazy idea but can we do it...

I have checked out the FAQ's to open a new dialog... I have use the simple code....

copied code from FAQ's :cool:

void someClass::someMethod()
{
firstDialog::close();
secondDialog *dialog = new secondDialog(this);
dialog->show();
} :D

This was the simple code Which I have used... if I use the code I am able to reach near to the assumption but when I try to close the dialog using the upper "X" mark then I abserved that there were to many windows to close... this shows that the windows which I have opened using the new Dialog(this) are not closing ...

Is there any way to close the dialogs....

Thanks for the help

spirit
28th November 2008, 07:21
could be better if you provide more code.

manmohan
28th November 2008, 17:00
Thank's for looking at my problem by the way this is the complete code


main.cpp
==========

#include <QtGui/QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}

########################################

dialog.cpp
============

#include <QtGui>
#include "dialog.h"
#include "x1dialog.h"

Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
setupUi(this);
connect(pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
}

void Dialog::on_pushButton_clicked()
{
Dialog::close();
x1Dialog dialog(this);
dialog.exec();

}

dialog.h
===========
#ifndef DIALOG_H
#define DIALOG_H

#include <QtGui/QDialog>
#include "ui_dialog.h"

class Dialog : public QDialog, public Ui::Dialog
{
Q_OBJECT

public:
Dialog(QWidget *parent = 0);

public slots:
void on_pushButton_clicked();
};

#endif

dialog.ui
============
<ui version="4.0" >
<class>Dialog</class>
<widget class="QDialog" name="Dialog" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>Dialog</string>
</property>
<widget class="QPushButton" name="pushButton" >
<property name="geometry" >
<rect>
<x>60</x>
<y>100</y>
<width>75</width>
<height>28</height>
</rect>
</property>
<property name="text" >
<string>PushButton</string>
</property>
</widget>
<widget class="QLabel" name="label" >
<property name="geometry" >
<rect>
<x>170</x>
<y>70</y>
<width>161</width>
<height>131</height>
</rect>
</property>
<property name="text" >
<string>This is the 1st dialog</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>

################################################## #############################

x1dialog.cpp
===============
#include <QtGui>
#include "dialog.h"
#include "x1dialog.h"

x1Dialog::x1Dialog(QWidget *parent)
: QDialog(parent)
{
setupUi(this);
connect(pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
}

void x1Dialog::on_pushButton_clicked()
{
x1Dialog::close();
Dialog dialog(this);
dialog.exec();

}

x1dialog.h
=============
#ifndef X1DIALOG_H
#define X1DIALOG_H

#include <QtGui/QDialog>
#include "ui_x1dialog.h"

class x1Dialog : public QDialog, public Ui::x1Dialog
{
Q_OBJECT

public:
x1Dialog(QWidget *parent = 0);

public slots:
void on_pushButton_clicked();
};

#endif

x1dialog.ui
==============
<ui version="4.0" >
<class>x1Dialog</class>
<widget class="QDialog" name="x1Dialog" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>x1Dialog</string>
</property>
<widget class="QPushButton" name="pushButton" >
<property name="geometry" >
<rect>
<x>60</x>
<y>100</y>
<width>75</width>
<height>28</height>
</rect>
</property>
<property name="text" >
<string>PushButton</string>
</property>
</widget>
<widget class="QLabel" name="label" >
<property name="geometry" >
<rect>
<x>170</x>
<y>70</y>
<width>161</width>
<height>131</height>
</rect>
</property>
<property name="text" >
<string>This is the 2nd dialog</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>

How ever I am attaching a copy of the conntent..........................



compiled on linux ubuntu

spirit
29th November 2008, 11:14
so, the problem is: on_pushButton_clicked() is called twice, because if you use such kind of slot naming on_pushButton_clicked then you don't need to connect it manually, because it will be made automatically. so, in your case two connections were created.
so, you need to comment these connections


connect(pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
connect(pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));

or rename slots.
see this for more details QMetaObject::connectSlotsByName (http://doc.trolltech.com/4.4/qmetaobject.html#connectSlotsByName).
PS. please, use tags CODE. :)

manmohan
1st December 2008, 10:49
Thank you very much, I have found this very much help full now the program works well...

manmohan
1st December 2008, 17:04
May I know a best guide to learn the opengl ???????