[Qt-interest] adressing something to the mainWindow from a dialog
John McClurkin
jwm at nei.nih.gov
Tue Apr 7 13:44:45 CEST 2009
Giancarlo Amati wrote:
> Many thanks!
>
> ------------------------------------------------------------------------
> From: tiago at cnotinfor.pt
> Date: Tue, 7 Apr 2009 09:29:26 +0100
> To: qt-interest at trolltech.com
> Subject: Re: [Qt-interest] adressing something to the mainWindow from a
> dialog
>
> Hi,
>
> You can access the main application by using the global variable qApp,
> then you can use activeWindow static method to get the active window.
>
> You can also subclass QApplication, to have your own method to have a
> pointer to your main window, then you could access it.
>
> On Tue, Apr 7, 2009 at 9:13 AM, Giancarlo Amati
> <ilferraresebono at hotmail.it <mailto:ilferraresebono at hotmail.it>> wrote:
>
> I'm afraid u didn't understand the question...probably my fault.
> QT as well as MFC they have they're own scoping for the variable
> through the application due to different mechanism
> Moreover, having more than 10 years experience in C++ programming
> with a Computer Science degree and PHD I appreciate your effort in
> reminding me the basic rules of C++ (of course my question wasn't on
> that).
> Basically the question was if I have a class of mine derived from
> any of QT class I can always have the access to a pointer which
> points to the main windows/application as in MFC there is this
> mechanism while it doesn't exists expicitly in QT....
>
> Best Regards.
> Giancarlo Amati
>
> > From: alex at wai.com <mailto:alex at wai.com>
>
> > To: qt-interest at trolltech.com <mailto:qt-interest at trolltech.com>
> > Date: Mon, 6 Apr 2009 16:05:28 -0700
> > Subject: Re: [Qt-interest] adressing something to the mainWindow
> from a dialog
>
> >
> > Please take into the count that correctly asked question is at
> least half of a right answer. And I afraid I might not understand it
> right.
> >
> >
> > So asssume you have vaiable 'var' in your MainWindow class.
> >
> > class MainWindow
> > {
> > ...........
> > int var;
> > ...........
> > }
> >
> > Remember that variable 'var' makes sence only withing MainWindow
> object.
> > Even you have only one object, you should have allocate it
> somewhere, probably like this:
> >
> > MainWindow* mainWindow = new ........;
> >
> > So first thing you need is a pointer to your mainWindow object.
> > How you get it, depends on the code, here I would assume that you
> can access it, by storing the pointer in a global variable. (I don't
> recommend this way, it is just chosen for simplicity).
> >
> >
> > Once you have an access to a pointer to MainWindow object in your
> module (a dialog window):
> >
> > - if var variable is public, you can get and set it directly,
> like mainWindow->var = 3; int mainWindow->var = 4;
> > - if it is protected or private, you can either add public
> methods (functions) to MainWindow which will return or set value of
> val, etc.
> >
> >
> > Now I hope this helps.
> > But I would ask you to remember that QT interest forum is not a
> place for basic C++ questions.
> > The reason is that most of people read and post here in their
> free time.
> > And they don't like to dig through non-qt related questions all
> the time, because they were currently interested in specific
> product, even they were willing to help you.
> > There are special forums for C++ beginners. Try to find one. You
> might find much more help there too.
> >
> > Best regards,
> > Alex
> >
> >
> > ________________________________________
> > From: qt-interest-bounces at trolltech.com
> <mailto:qt-interest-bounces at trolltech.com>
> [mailto:qt-interest-bounces at trolltech.com
> <mailto:qt-interest-bounces at trolltech.com>] On Behalf Of Giancarlo Amati
> > Sent: Monday, April 06, 2009 9:17 AM
> > To: qt-interest at trolltech.com <mailto:qt-interest at trolltech.com>
> > Subject: [Qt-interest] adressing something to the mainWindow from
> a dialog
> >
> > Hello everybody,
> >
> > i have this problem, I have a dialog windows from where I want to
> adress a variable which beloges to may MainWindow class linked to
> the main application. Is there a way to do it?
> >
> > Kind regards.
> > Giancarlo
> > ________________________________________
>
If I understand your case correctly, you want to manipulate a variable
in a parent from a dialog that was launched from the parent, say by
clicking a button or from a menu action. Have you considered signals and
slots? Define a signal for you dialog class that exports its variable,
and a slot in the parent that accepts the signal. When the parent
creates the dialog, it can connect the dialog's signal it its slot. Then
the dialog can emit the signal whenever the relevant variable is
changed. This does not require global variables and does not require
that the dialog know about the parent.
More information about the Qt-interest-old
mailing list