You are not logged in.

1

Friday, December 2nd 2011, 9:56am

[solved]one instance of QDialog

I'm trying to make only one instance of a modelss QDialog when a button is clicked. Here is what I have so far:

Source code

1
2
3
4
5
6
void MainWindow::on_pushButton_clicked()
{	
    	Form2 *form2;
    	form2=new Form2(this);
    	form2->show();
}


I tried using the code examples here: http://doc.qt.nokia.com/latest/qdialog.html but, when I tried if(!form2) I get a segmentation fault.
However, I can get it to work using a global variable though.

Any help would be greatly appreciated, thank you.

This post has been edited 1 times, last edit by "vis.15" (Dec 2nd 2011, 9:47pm) with the following reason: solved


Junior

Professional

  • "Junior" is male

Posts: 1,623

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Friday, December 2nd 2011, 1:38pm

Create the form in the constructor and then just use show in the clicked() function.

3

Friday, December 2nd 2011, 6:41pm

yes, if you want to have the same widget, you should store the pointer as a member variable.

Your segfault sounds like you did something like this:

Source code

1
2
3
Form2* frm;
if (!frm)
...


Of course, this is incorrect c++. frm is not initialised - it could point anywhere in memory, so dereferencing may cause problems,
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

4

Friday, December 2nd 2011, 7:58pm

Thank you Junior, but there is still a problem, if you try to create a form in the constructor you will get an infinite loop. this is what I tried thus far:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//form2.h
class Form2 : public QDialog {
bool showingform;
	Q_OBJECT
public:
	Form2(QWidget *parent = 0);
	~Form2();
	bool Showing();

	Form2 *form2;

protected:
	void changeEvent(QEvent *e);

private:
	Ui::Form2 *ui;
};


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//form2.cpp
#include "form2.h"
#include "ui_form2.h"

Form2::Form2(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::Form2)
{
	ui->setupUi(this);
	setAttribute(Qt::WA_DeleteOnClose);

	if (showingform==false)
	{
    	showingform=true;
    	form2=new Form2;
	}
}

Form2::~Form2()
{
	delete ui;
	showingform=false;
}
bool Form2::Showing(void)
{

	form2->show();
	return showingform;
}


Source code

1
2
3
4
5
6
//main.cpp
void MainWindow::on_pushButton_clicked()
{  
Form2 form2;
    	form2.Showing();
}

5

Friday, December 2nd 2011, 9:46pm

I got it!

Here is what I did:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <form2.h>

namespace Ui {
	class MainWindow;
}

class MainWindow : public QMainWindow {
	Q_OBJECT
public:
	MainWindow(QWidget *parent = 0);
	~MainWindow();
	Form2 *form2;  //declare form2 globally in the MainWindow class. 

protected:
	void changeEvent(QEvent *e);

private:
	Ui::MainWindow *ui;

private slots:
	void on_pushButton_clicked();
};

#endif // MAINWINDOW_H


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainWindow)
{
	ui->setupUi(this);

	form2=new Form2; //create new form only once.

}
void MainWindow::on_pushButton_clicked()
{ 
 	form2->show(); //show the form;
}


Thanks guys! :D

6

Friday, December 2nd 2011, 11:17pm

form2 in mainwiondow should be private, not public.

you also now have a memory leak as you do not delete form2 anywhere - this should be done in mainwindow destructor.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

7

Saturday, December 3rd 2011, 1:15am

I thought I had a memory leak, thank you Anleto. So, I should just do this then?

Source code

1
2
3
4
5
MainWindow::~MainWindow()
{
	delete ui;
	delete form2;
}