LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Multiple Forms in Qt Application (https://www.linuxquestions.org/questions/programming-9/multiple-forms-in-qt-application-144322/)

00it45 02-10-2004 01:14 AM

Multiple Forms in Qt Application
 
hi,
i am currently developing an application in qt that which involves multiple forms..
what i want to know how 2 traverse among those forms..

e.g..

i created 2 forms form1 & form2
now i show form2 from form1 by following

#include "form2.h"

form1::load_form2()
{
form2 f2;
this->hide();
f2->show();
}

this is done by inherating the form2.h in form1.h by Qt designer...
now i wnt 2 load and load and show form1 from the form2 ..

following the above method i need 2 inherige form1.h in form2.h
but i already have inherited form2.h in form1.h.. so it results in collision..

can i use QApplication widget 2 resolve this problem..
or any other sol............

please help me out.........
:(

synna 02-10-2004 05:27 AM

You can make some singleton like this

class form1{
static form1* instance(){return single;}
static form1* single = new form1;
};

//the same for form 2

then in form 2

form2::load_form1(){
form1::instance()->show();
}


Hope this help

00it45 02-11-2004 12:06 AM

Re: Multiple Forms in Qt app.
 
thanx, synna,

i wnt 2 know about the class hierarchy of the struct u gave me....

i.e inheritance

won't it be result into conflict..

if i include form2.h in form1.h and the form1.h in form2.h..

synna 02-11-2004 05:42 AM

Don't do any inheritance between form1 and form2

in form1.h

class form2;
class form1 : public Generated_By_Uic_Class{
//Your implementation
static form2 * instance();
};

in form2.h :

class form1;
class form2 : public Generated_By_Uic_Class_2{
//Your implementation
static form2 * instance();
};

That's all!

00it45 02-13-2004 12:21 AM

thanx
 
thanx, synna
u'r tip worked,
i have to make some changes..
and now i can communicate among several forms...
i added the .h files in the .cpp file of the form where i want 2 use the form of .h file and it worked...

now if u have any idea about how to define and use global variables in Qt please guide me..
as, a have declared one global var. and included the .h file of which in other files but is showing multiple declaration error in object files....

synna 02-13-2004 05:12 AM

I don't remember exactly but declaring global variable isn't a good practice.

In fact when you use the singleton pattern you use something like a global variable since instance() return an object that's defined only once and that you can called from where you want. (I recommend you reading about "singleton pattern"). You will understand my tip a little more.

You can define global variable in cpp file by declaring a static variable
static int myvariable=0;
then include "myfile.cpp"

try this

If I remember in .h you should use extern "C"{
variable declaration;
}

Hope this help

00it45 02-13-2004 05:50 AM

reference ab'ut singleton pattern
 
hi synna,
may i know the references of "singleton pattern" i want 2 get more info. about singleton pattern..

thanx,
again

synna 02-13-2004 06:41 AM

You'll find thousands on google. (search for "design pattern" and "singleton pattern").

00it45 02-14-2004 07:38 AM

so far my app. is woking nicely,
i am using a global pointer and thus can call the same form from anywhere in the app. (provided all inclusions are done properly).

but now as u suggested in case of global pointer...
i have 2 rethink 4 my app.

so i will try 2 to by singleton pattern...
hope it won't generate many prob..

thanx synna...

00it45 02-17-2004 01:41 AM

hi, synna

i tried u'r method of making singleton pattern.. as follows

in class frm1
i created function
class frm1{
// other implementations
static frm1* main1= new frm1; // this caused error first time

static frm1* instance()
{
return main1;
}

}

then i made all the inclusions appropriately...
in class frm2

frm2::load2()
{
frm1::instance()->show();
}

at compile
first it was showing error of can,t make initialization of static member in declaration so i x'fer the line "main1 = new frm1" to frm1.cpp

now that error gone but now it is showing error like below:

In file included from frm1.cpp:19:
frm1.ui.h:14: error: cannot declare member function `frm1* frm1::instance()' to have static linkage
make: *** [frm1.o] Error 1

any solution...


:mad:


All times are GMT -5. The time now is 04:34 PM.