LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-10-2004, 01:14 AM   #1
00it45
LQ Newbie
 
Registered: Feb 2004
Location: INDIA
Distribution: FEDORA
Posts: 19

Rep: Reputation: 0
Unhappy 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.........
 
Old 02-10-2004, 05:27 AM   #2
synna
Member
 
Registered: Jan 2004
Posts: 40

Rep: Reputation: 15
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
 
Old 02-11-2004, 12:06 AM   #3
00it45
LQ Newbie
 
Registered: Feb 2004
Location: INDIA
Distribution: FEDORA
Posts: 19

Original Poster
Rep: Reputation: 0
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..
 
Old 02-11-2004, 05:42 AM   #4
synna
Member
 
Registered: Jan 2004
Posts: 40

Rep: Reputation: 15
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!
 
Old 02-13-2004, 12:21 AM   #5
00it45
LQ Newbie
 
Registered: Feb 2004
Location: INDIA
Distribution: FEDORA
Posts: 19

Original Poster
Rep: Reputation: 0
Unhappy 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....
 
Old 02-13-2004, 05:12 AM   #6
synna
Member
 
Registered: Jan 2004
Posts: 40

Rep: Reputation: 15
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
 
Old 02-13-2004, 05:50 AM   #7
00it45
LQ Newbie
 
Registered: Feb 2004
Location: INDIA
Distribution: FEDORA
Posts: 19

Original Poster
Rep: Reputation: 0
Smile 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
 
Old 02-13-2004, 06:41 AM   #8
synna
Member
 
Registered: Jan 2004
Posts: 40

Rep: Reputation: 15
You'll find thousands on google. (search for "design pattern" and "singleton pattern").
 
Old 02-14-2004, 07:38 AM   #9
00it45
LQ Newbie
 
Registered: Feb 2004
Location: INDIA
Distribution: FEDORA
Posts: 19

Original Poster
Rep: Reputation: 0
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...
 
Old 02-17-2004, 01:41 AM   #10
00it45
LQ Newbie
 
Registered: Feb 2004
Location: INDIA
Distribution: FEDORA
Posts: 19

Original Poster
Rep: Reputation: 0
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...


 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing form values between multiple forms!! AskMe Programming 5 09-07-2005 07:44 PM
Forms with Qt Raphael_T Programming 10 05-12-2004 02:42 PM
Gtk-Warnings, and multiple application crashes nullcreation Arch 1 10-19-2003 12:23 AM
qt forms help alrawab Programming 1 10-15-2003 03:24 PM
Handling multiple forms with same name hidden fields coolman0stress Programming 5 09-04-2003 01:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration