PDA

View Full Version : How to pass information between wizardpages without widgets



roseicollis
23rd January 2015, 12:25
Hi!

I am making a GUI app with wizard structure and I have:


the main.cpp
the class MainWindow : public QMainWindow basic class
class BaseWizard : public QWizard
class BasePage : public QWizardPage
all the pages(wp1, wp2, wp3 …) like this: class WP1 : public BasePage (they inherit from BasePage)


Then in wp1 I have 2 big buttons which you can focus the with tab. If you press 'L' when button1 has focus, you go to wp2, and if the focus is on button2, you go to wp3.

Now I want to know if the user choose to go to wp2 or wp3, so I decided to put a var (int iA; ) in Basepage (cause its common to all pages) and change iA's value on the page I want to, so I have:

On basepage.h:

public:
void setA(int i);
int getA() {return iA;};
private:
int iA;

on wp2.cpp:


void WP2::initializePage()
{
BasePage::setA(0);
}

on wp3.cpp:


void WP3::initializePage()
{
BasePage::setA(1);
}


So I know that if getiA() returns 0 means the user chose the first option, and if it returns 1 the user chose the second.

The problem is that setA() works fine but then the var iA gets any number lately, and I don't make any other setA()... so when I am on w5 and I want to do a BasePage::getA() it returns 0 or 4 or 5 or... instead of (in case the user chose the second button) ...1 .

Any idea of why? Any idea of how can I pass the information throw wizardpages?

I know that you can registerfield() but that is only for wdgets like lineedits, so it doesn't work here.

Thanks.

anda_skoa
23rd January 2015, 12:32
Any idea of why?

Why would changing one object's iA variable change the value of another object's iA variable?
In C++ an instance of a class is independent of any other instance of that class.

Cheers,
_

roseicollis
23rd January 2015, 13:11
Then how can I pass the information throw wizardpages?

Lesiok
23rd January 2015, 13:30
How about dynamic properties (http://qt-project.org/doc/qt-4.8/properties.html#dynamic-properties) on BaseWizard ?

wysota
23rd January 2015, 14:07
There is also QWizardPage::registerField(), QWizard::setField() QWizard::field().

anda_skoa
23rd January 2015, 15:07
Then how can I pass the information throw wizardpages?

Well, since we are talking about C++:
- through an object that all pages have access to
- through using static members in the base class

Cheers,
_

roseicollis
23rd January 2015, 16:19
Ty wysota, as I said I already saw the registerField but what I didn't see are the fileds property and changedsignal,


void QWizardPage::registerField ( const QString & name, QWidget * widget, const char * property = 0, const char * changedSignal = 0 )

So I suppose that I have to do something like this to get the focus change:
registerField("First_Option", button1, focus , somesignalhere);
But I can’t see how does it works with or without the ‘supposed’ signal.

----------

anda_skoa:

- through an object that all pages have access to
- through using static members in the base class
That's why I created int iA in basepage, trying to have a var which is common to all, but I suppose that this is not the way??

wysota
23rd January 2015, 16:23
But I can’t see how does it works with or without the ‘supposed’ signal.
No idea what you mean. I have no idea why you'd want to set a field which keeps information whether a particular widget has focus or not.

roseicollis
23rd January 2015, 17:32
No idea what you mean. I have no idea why you'd want to set a field which keeps information whether a particular widget has focus or not.

I have the first wizard page: WP1
- with the fist button u go to WP2
- with the second button u go to WP3

this are 2 diferent ways of pages but both go to WPEnd.
Then in WPEnd I need to know if user arrived there throw WP2 way or throw WP3 way and that is what I am trying to do: a way to know that.

ChrisW67
23rd January 2015, 21:23
Each QWizardPage is a QWidget.
Give the page that has the buttons:
a getter and setter for the value you want to pass,
a declaration of the property
a signal to indicate it has changed (you may not even need this)

And then register the page widget and property as the field.

wysota
23rd January 2015, 23:14
I have the first wizard page: WP1
- with the fist button u go to WP2
- with the second button u go to WP3

this are 2 diferent ways of pages but both go to WPEnd.
Then in WPEnd I need to know if user arrived there throw WP2 way or throw WP3 way and that is what I am trying to do: a way to know that.

Hmm... usually in a wizard you would have some widget (a checkbox, command button or radio button) for choosing the variant and then you would click the "Next" button to go to the next page according to that variant. What you want to do is very unusual for a wizard (the user would expect to traverse pages via Next and Previous buttons).

Radek
24th January 2015, 06:08
I've never needed to write a wizard but, IMO, it's only a matter of data organization.
(1) All "shared" data (shared between wizard pages) need to be data items of the BaseWizard instance. For example, the iA variable belongs to the BaseWizard, not to the BasePage.
(2) The QWizardPage class has a (protected) QWizardPage::wizard() method for accessing the wizard which the page belongs to.
(3) Using the pointer, all pages have access to the "shared" data. They can store any status there and the final page can check what it wants.

anda_skoa
24th January 2015, 10:52
anda_skoa:

That's why I created int iA in basepage, trying to have a var which is common to all, but I suppose that this is not the way??

The variable is common to all pages, each page has it.
But each page is an independent object, so each page has its own value.

Just consider physical objects if in doubt: e.g. changing the content of my fridge does not change the content of your fridge, despite both having the same base class "cooled food storage kitchen appliance".

Cheers,
_

d_stranz
25th January 2015, 19:52
That's why I created int iA in basepage, trying to have a var which is common to all, but I suppose that this is not the way??

You are confused about base classes, derived classes, and instances of them. A base class is simply a place to put all of the variables and methods that each derived class inherits. Inheriting is not sharing. If you define a member variable called "iA" in a base class, that means that every derived class will have a variable called "iA" that it inherits from the base class. When you instantiate a derived class (i.e. call "new MyDerivedClass()"), C++ creates a new instance of the class with its own value for iA. Changing the value of iA in one instance has no effect at all on the value of iA in other instances of the class.

The only way to achieve what you want through a member variable is to make that member static. A static member variable's value is shared among all instances of a class, so changing iA's value in one instance changes the value seen by all other instances. If the static member is in the base class, then all derived classes inherit the variable and all instances of all derived classes share the same value.

But as others have said, the QWizard architecture already has features built in to enable you to determine how you have arrived at a certain wizard page, and how to change where you go next depending on what happens in the current page. You don't need to reinvent that wheel.

roseicollis
2nd February 2015, 11:49
Hi, thanks to all for your help. Its solved now.

Thank you so much d_stranz. You were totally right and I was mixing some terms. Now I have it clear so I could fix the problem.