PDA

View Full Version : How to stop multiple appearance of a widget



Sarma
9th March 2006, 06:26
hi all,
I have an application in which when I press a button I should get a find dialog box. Thing is if I press the button again, I shouldnot get one more dialog box. The previous one should appear.
My present code is like this:


CPT::CPT(QWidget *parent):QWidget(parent)
{
//some other lines of code
QPushButton *but=new QPushButton(this);
QObject::connect(but,SIGNAL(clicked()),this,SLOT(f indbox()));
//remaining program
}
void CPT::findbox()
{
QWidget *findstr=new QWidget;
//other widgets added to it
findstr->show();
}

So kindly help me in this problem.

sunil.thaha
9th March 2006, 06:36
Window::showFindDialog(){
static FindDialog* pFindDlg = 0;
if( pFindDlg == 0 ) {
pFindDlg = new FindDialog( this, "") ;
}
pFindDlg->show();
pFindDlg->raise();
pFindDlg->setActiveWindow();

}

Sarma
9th March 2006, 06:46
hi sunil,
can you please show me the changes that I have to make in my program. And u have written FindDialog in your code. Is there any such default dialog box provided by Qt.

regards,
Sarma.

sunil.thaha
9th March 2006, 07:50
I just called the Dialog you wanted to show as FindDialog That is all !

Sarma
9th March 2006, 08:14
sorry sunil,
That is giving good number of errors. It says that the second line (of your code) is wrong and because of this, it says that pFindDlg is not defined. can you suggest me other method

sunil.thaha
9th March 2006, 08:27
Try the Attachment

Sarma
9th March 2006, 08:40
hi sunil,
well, thanks for your help. But no where I have found the code that you have written previously. In which file should I see that . And can you please tell me how to generate the .ui files and use them in our ordinary program.

Thanks & regards,
Sarma.

wysota
9th March 2006, 08:49
See the FAQ (http://www.qtcentre.org/forum/faq.php?faq=qt_general_category#faq_qt_designer_2f orms).

Sarma
9th March 2006, 09:44
sir,
Iam getting errors as I said previously :( . Kindly show me the changes that I have to make in my code(I have send it in my first post).

Thanks & regards,
Sarma.

wysota
9th March 2006, 10:03
Look at the FAQ entry I mentioned. The code is there -- literally. Especially note the "static" keyword.

Sarma
9th March 2006, 10:12
sir,
I have modified my code like this:


CPT::CPT(QWidget *parent):QWidget(parent)
{
//some other lines of code
QPushButton *but=new QPushButton(this);
QObject::connect(but,SIGNAL(clicked()),this,SLOT(f indbox()));
//remaining program
}
void CPT::findbox()
{
QWidget *fwin=new QWidget;
//other widgets added to it
//fwin->show();
static fwin *form2 = new fwin(this);
form2->show();
form2->activateWindow(); // or form2->setActiveWindow() in Qt3
form2->raise();
}


And I got the following errors:


"cptdebug.cpp", line 1501: Error: fwin, of type int, was previously declared QWidget*.
"cptdebug.cpp", line 1501: Error: Multiple declaration for fwin.
"cptdebug.cpp", line 1501: Error: "," expected instead of "*".
"cptdebug.cpp", line 1502: Error: form2 is not defined.
"cptdebug.cpp", line 1503: Error: form2 is not defined.
"cptdebug.cpp", line 1504: Error: form2 is not defined.

Kindly tell me why? And give me the possible solution.

Thanks and regards,
Sarma.

wysota
9th March 2006, 10:26
The compiler already told you why...


void CPT::findbox(){
static fwin *form2 = new fwin(this);
form2->show();
form2->activateWindow();
form2->raise();
}

Sarma
9th March 2006, 10:40
Sir,
sorry for troubling you much. I couldnot get the proper solution.Please show the changes that I have to make in my code.

regards,
Sarma.

wysota
9th March 2006, 10:58
Well, I gave you the code in the previous post. What else do you want?

Sarma
13th March 2006, 06:45
hello Sir,
I have posted this thread long back but I couldnot get proper solution.


See the FAQ. (http://www.qtcentre.org/forum/faq.php?faq=qt_general_category#faq_qt_designer_2f orms)

In that FAQ, it was written that "Assuming you have two form classes: Form1 and Form2 and that Form1 has a pushbutton "button" child and Form2 is derived from QDialog" .

But in my application, the find box which I want to show is not derived from QDialog. It is a widget to which many child widgets are added.
Here is the code that I have written:




CPT::CPT(QWidget *parent):QWidget (parent)
{
QPushButton *findbut=new QPushButton(this);
QObject::connect(findbut,SIGNAL(clicked()),this,SL OT(findstr()));
}

void CPT::findstr()
{
QWidget *fwin=new QWidget;
fwin->setCaption("CP Trace Debug Tool : FIND");
fwin->setFixedSize(320,125);
fwin->setFont(QFont("SansSerif",9));
fwin->setBackgroundColor(c1);

QVBoxLayout *fvbox=new QVBoxLayout(fwin);
fvbox->setMargin(2);
fvbox->setSpacing(6);

QHBoxLayout *fhbox1=new QHBoxLayout(fvbox);
fhbox1->setMargin(2);
fhbox1->setSpacing(8);

QLabel *fl1=new QLabel("Find:",fwin);
fl1->setBackgroundColor(c1);

fedit1=new QLineEdit(fwin);

fch=new QCheckBox("Regular Expression",fwin);
fch->setBackgroundColor(c1);

fhbox1->addWidget(fl1);
fhbox1->addWidget(fedit1);
fhbox1->addWidget(fch);

QHBoxLayout *fhbox2=new QHBoxLayout(fvbox);
fhbox2->setMargin(2);
fhbox2->setSpacing(6);

QPushButton *fb1=new QPushButton("Forward",fwin);
fb1->setPaletteBackgroundColor(c1);

QPushButton *fb2=new QPushButton("Backward",fwin);
fb2->setPaletteBackgroundColor(c1);

QPushButton *fb3=new QPushButton("Close",fwin);
fb3->setPaletteBackgroundColor(c1);

QObject::connect(fb1,SIGNAL(clicked()),this,SLOT(r esult_for()));
QObject::connect(fb2,SIGNAL(clicked()),this,SLOT(r esult_bck()));
QObject::connect(fb3,SIGNAL(clicked()),fwin,SLOT(c lose()));

fhbox2->addWidget(fb1);
fhbox2->addWidget(fb2);
fhbox2->addWidget(fb3);

fwin->show();
}


Now when I press the button again and again, I get multiple windows of this findbox which I shouldn't. Kindly give me the possible solution.

Thanks & Regards,
Sarma.

jpn
13th March 2006, 09:27
You are always creating a new find dialog each and every time the button is pressed.
Move the find widget as a member variable, instantiate it only once, and connect the find buttons clicked() signal to the find widget's show() slot.



CPT::CPT(QWidget *parent):QWidget (parent)
{
createFind();
QPushButton *findbut=new QPushButton(this);
QObject::connect(findbut,SIGNAL(clicked()),fwin,SL OT(show()));
}

void CPT::createFind()
{
// add "QWidget* fwin" as a member variable
fwin=new QWidget;
fwin->setCaption("CP Trace Debug Tool : FIND");
fwin->setFixedSize(320,125);
fwin->setFont(QFont("SansSerif",9));
fwin->setBackgroundColor(c1);

QVBoxLayout *fvbox=new QVBoxLayout(fwin);
fvbox->setMargin(2);
fvbox->setSpacing(6);

QHBoxLayout *fhbox1=new QHBoxLayout(fvbox);
fhbox1->setMargin(2);
fhbox1->setSpacing(8);

QLabel *fl1=new QLabel("Find:",fwin);
fl1->setBackgroundColor(c1);

fedit1=new QLineEdit(fwin);

fch=new QCheckBox("Regular Expression",fwin);
fch->setBackgroundColor(c1);

fhbox1->addWidget(fl1);
fhbox1->addWidget(fedit1);
fhbox1->addWidget(fch);

QHBoxLayout *fhbox2=new QHBoxLayout(fvbox);
fhbox2->setMargin(2);
fhbox2->setSpacing(6);

QPushButton *fb1=new QPushButton("Forward",fwin);
fb1->setPaletteBackgroundColor(c1);

QPushButton *fb2=new QPushButton("Backward",fwin);
fb2->setPaletteBackgroundColor(c1);

QPushButton *fb3=new QPushButton("Close",fwin);
fb3->setPaletteBackgroundColor(c1);

QObject::connect(fb1,SIGNAL(clicked()),this,SLOT(r esult_for()));
QObject::connect(fb2,SIGNAL(clicked()),this,SLOT(r esult_bck()));
QObject::connect(fb3,SIGNAL(clicked()),fwin,SLOT(c lose()));

fhbox2->addWidget(fb1);
fhbox2->addWidget(fb2);
fhbox2->addWidget(fb3);

// removed this
// fwin->show();
}

Sarma
13th March 2006, 09:54
hi jpn,
well thanks for replying.But that didnot solve my problem. Actually, I didnot put the entire code before you( sorry for that ). Thing is that I need to set certain values before createFind() is called. So, I have the code like( with the changes suggested by you ):


CPT::CPT(QWidget *parent):QWidget(parent)
{
QPushButton *findbox=new QPushButton();
QObject::connect(findbox,SIGNAL(clicked()),this,SL OT(defaults()));
}

void CPT::defaults()
{
//setting values for some variables (of integer type)
createFind();
fwin->show();
}

void CPT::createFind()
{
//QWidget *fwin is made as a member variable
fwin=new QWidget;
fwin->setCaption("CP Trace Debug Tool : FIND");
fwin->setFixedSize(320,125);
fwin->setFont(QFont("SansSerif",9));
fwin->setBackgroundColor(c1);

QVBoxLayout *fvbox=new QVBoxLayout(fwin);
fvbox->setMargin(2);
fvbox->setSpacing(6);

QHBoxLayout *fhbox1=new QHBoxLayout(fvbox);
fhbox1->setMargin(2);
fhbox1->setSpacing(8);

QLabel *fl1=new QLabel("Find:",fwin);
fl1->setBackgroundColor(c1);

fedit1=new QLineEdit(fwin);

fch=new QCheckBox("Regular Expression",fwin);
fch->setBackgroundColor(c1);

fhbox1->addWidget(fl1);
fhbox1->addWidget(fedit1);
fhbox1->addWidget(fch);

QHBoxLayout *fhbox2=new QHBoxLayout(fvbox);
fhbox2->setMargin(2);
fhbox2->setSpacing(6);

QPushButton *fb1=new QPushButton("Forward",fwin);
fb1->setPaletteBackgroundColor(c1);

QPushButton *fb2=new QPushButton("Backward",fwin);
fb2->setPaletteBackgroundColor(c1);

QPushButton *fb3=new QPushButton("Close",fwin);
fb3->setPaletteBackgroundColor(c1);

QObject::connect(fb1,SIGNAL(clicked()),this,SLOT(r esult_for()));
QObject::connect(fb2,SIGNAL(clicked()),this,SLOT(r esult_bck()));
QObject::connect(fb3,SIGNAL(clicked()),fwin,SLOT(c lose()));

fhbox2->addWidget(fb1);
fhbox2->addWidget(fb2);
fhbox2->addWidget(fb3);

// fwin->show();
}


Now kindly show me changes that I can make in this program so that at any time only one box should be made visible.

Thanks & Regards,
Sarma.

jpn
13th March 2006, 10:04
....and you are still creating the dialog again and again.
You do not want to create a new instance of the dialog every time the button is pressed.



CPT::CPT(QWidget *parent):QWidget(parent)
{
createFind(); // create the dialog ONLY ONCE
QPushButton *findbox=new QPushButton();
QObject::connect(findbox,SIGNAL(clicked()),this,SL OT(find()));
}

void CPT::find()
{
// set your defaults here
fwin->show();
fwin->activateWindow();
fwin->raise();
}

void CPT::createFind()
{
fwin = new QWidget;
// ...
}

Sarma
13th March 2006, 10:47
sir,
Thank you very much. It is working well. Now I got the point. I was creating an instance of the window everytime I click the button. But here I got a doubt. We are creating the instance of the findbox in the application even before clicking it. Now for any reason if we donot click the button , doesn't that cause memory wastage? Kindly rectify my doubt.

Thanks once again for this useful post,
Sarma.:)

fullmetalcoder
13th March 2006, 10:55
sir,
Thank you very much. It is working well. Now I got the point. I was creating an instance of the window everytime I click the button. But here I got a doubt. We are creating the instance of the findbox in the application even before clicking it. Now for any reason if we donot click the button , doesn't that cause memory wastage? Kindly rectify my doubt.

Thanks once again for this useful post,
Sarma.:)

No memory leaks caused provided you use destructor wisely to free all used memory...

jpn
13th March 2006, 13:04
We are creating the instance of the findbox in the application even before clicking it. Now for any reason if we donot click the button , doesn't that cause memory wastage?
Since you don't supply any parent for the widget, it doesn't get destroyed automatically. So you will have to delete it in the descructor.
A widget without a parent is always an independent window.. But why don't you just use a modeless dialog? Then you could pass your window as a parent, the dialog would show up centered to your window, and it would get automatically deleted..

wysota
13th March 2006, 15:05
hello Sir,
I have posted this thread long back but I couldnot get proper solution.

In that FAQ, it was written that "Assuming you have two form classes: Form1 and Form2 and that Form1 has a pushbutton "button" child and Form2 is derived from QDialog" .

But in my application, the find box which I want to show is not derived from QDialog. It is a widget to which many child widgets are added.

It's all the same for QWidget, you just have to pass 0 as the parent (meaning you'll get a memory leak if you don't free that memory yourself later). Why don't you use a QDialog subclass anyway?