PDA

View Full Version : How to transfer data entered in a QWizard to other classes?



dcopeto
26th March 2009, 15:03
Hi!

I'm using a QWizard to get some user configurations at the beggining of my program but i can't seem to find a way to fill in my database class with the data the user enters in the wizard pages! How can this be done?


thanks a lot for the help,
david

Lykurg
26th March 2009, 16:53
Hi,

have a look at the docs, there you can find your questions answered: Class Wizard Example (http://doc.trolltech.com/4.5/dialogs-classwizard.html). Especially "void ClassWizard::accept()".

Or in your code is somewhere
MyWizard wiz;
wiz.exec()
wiz.getMyFancyData(); // where you have to implement these function in your wizard class...

Lykurg

dcopeto
26th March 2009, 17:12
Thanks for the reply :)


Yeah in the meantime i found that too and my problem changed:
From what i understood, one recovers the data using something like


QString sessionName = field("session.name").toString();

which is ok. My problem is that i want to recover data from a QTableWidget!:confused: Any ideas on how to do this?


thanks for your help,
david

Lykurg
26th March 2009, 18:09
The "field" way is only one possibility. Since the QWizard is a normal class you can simply add a private member which points to the table. Use this pointer in the "save" function to get the data.

dcopeto
26th March 2009, 18:42
Maybe i didn't give you enough details because i'm having trouble finding out to do that...:crying:


This is what i have in my startwizard.h

class startWizard : public QWizard
{
Q_OBJECT

public:
enum{Page_Intro, Page_New, Page_Load, Page_Configs, Page_LoadDetails, Page_NewDetails};
startWizard(QWidget *parent = 0);

void accept();

};

(...)

class configsPage : public QWizardPage
{
Q_OBJECT

public:
configsPage(QWidget *parent = 0);
int nextId();

private:
(...)
QTableWidget *tableDisplay;

(...)
};


In the ConfigsPage of the wizard i create the table for the user to see what he's adding/configuring and then i want to be able to recover the data he just entered to my database object in the main application.

In your previous reply you meant placing a QTableWidget pointer in the startWizard class, pointing to the table that gets the stuff in ConfigsPage class? even that i'm having trouble with...

I still don't see how can i go on from there? I mean to have access to the database I must make the wizard have access to the database somehow...:confused:


thanks a lot for the help,
david

wysota
26th March 2009, 18:46
Move the table widget pointer to your QWizard subclass and then access the pointer when you want to retrieve the data.

Lykurg
26th March 2009, 19:50
class startWizard : public QWizard
{
Q_OBJECT
public:
enum{Page_Intro, Page_New, Page_Load, Page_Configs, Page_LoadDetails, Page_NewDetails};
startWizard(QWidget *parent = 0);
void accept();
private:
configsPage *m_configsPage;
};

startWizard::startWizard(QWidget *parent) : QWizard(parent) {
//...
m_configsPage = new configsPage(this);
addPage(m_configsPage);
//...
}

void startWizard::accept() {
//...
m_configsPage->table() //<- here's your pointer...
//...
}
(...)

class configsPage : public QWizardPage
{
Q_OBJECT

public:
configsPage(QWidget *parent = 0);
int nextId();
const QTableWidget* table() const {return tableDisplay;}

private:
(...)
QTableWidget *tableDisplay;

(...)
};

dcopeto
27th March 2009, 01:21
THANK YOU!:D

That took care of my table access and now I have also managed to 'reach' the dataBase from the wizard by passing a pointer to the database to it!

thanks again,
david

wysota
27th March 2009, 08:49
You don't have to transfer pointers to the database (assuming you are using QSqlDatabase). You can always retrieve the connection by calling it by name. See docs of QSqlDatabase.

dcopeto
27th March 2009, 10:39
Hey! thanks for the suggestion! I'm not using sqlDatabase, I'm using my own storing class, because it's not a lot of stuff, anyway can you tell me when does it 'payoff' to use a sql database instead of a custom class? I'm not really into it so it's probable that I'm writing garbage right this moment... :o


thanks,
david

BillGates
4th May 2010, 18:07
hi

maybe i didnt understand very well the method that Lykurg wrote in code, but, will other Pages of the wizard be able to access m_configsPage->table() ?

Is it possible to pass objects instead of int/Qstrings using ::field() and registerField(..) ?

wysota
4th May 2010, 20:16
You can pass anything that will fit into QVariant.

BillGates
4th May 2010, 22:04
Thanks for reply wysota !

Im sorry to ask, but is there any example somewhere ? I am a noob.
I searched on the net and even here but all I could find were widgets with properties that were int or string.

As I understand it it must be something like :

registerField("name", myWidget, "myProperty", SIGNAL(selectionChanged()));

so i conclude that i must declare my own widget and my own propery. Is this correct ?
My purpose is to make variables like a QList accessible to any/all wizardPage subclass.

wysota
4th May 2010, 22:13
A QList of what? That's a template class, you know... And while you are at it, what is the reason for doing that? You can always expose the list using your wizard class's public interface.

BillGates
4th May 2010, 22:38
yes, i know its a template, pardon me, a QList<QString> . I didnt think it mattered much.
I dont understand what you mean by "your wizard class public interface" ? Maybe I am missing something?

wysota
5th May 2010, 01:59
yes, i know its a template, pardon me, a QList<QString> . I didnt think it mattered much.
Well, it does, because QStringList is already registered to be used with QVariant.


I dont understand what you mean by "your wizard class public interface" ? Maybe I am missing something?
I mean two methods in your wizard class's "public" section - one for setting the value and the other for getting it. Look at the code Lykurg has written, "table()" is the getter method and "setTable(QTableWidget*)" would be a setter method.

Still you didn't answer what you want to do with this all so maybe there are tons of better way to do what you want.

BillGates
5th May 2010, 13:46
I want for the QTableWidget *tableDisplay from the first QWizardPAGE to be accesible to the second, the third and so on Pages. So that i can use them for default values, exacty what the field() does.
The only problem is that I am not competent enough; I dont know how to pass my own variables through registerField().
If I have a QStringList created in Page1, i would like to access it in Page2 and Page3..


in void myPage2::initializePage() {

myObject list = field("some_field_registered_from_page1") <-- something like this...

}

wysota
5th May 2010, 18:45
I want for the QTableWidget *tableDisplay from the first QWizardPAGE to be accesible to the second, the third and so on Pages. So that i can use them for default values, exacty what the field() does.
It's not what the field() does. To me it seams you need a model in your wizard. Then you can set this model to a QTableView (or QListView) in the first wizard page and also access the model from other pages. field() should represent a single value, not all possible values.

BillGates
6th May 2010, 12:42
Umh... so how would i access the model from the QWizard class from inside the Pages ? Pass it to their constructor ?

wysota
6th May 2010, 14:19
You can do it any way you see fit. C++ allows for many solutions to this problem. For instance each page has a pointer to the wizard object it is part of.

BillGates
6th May 2010, 18:02
"For instance each page has a pointer to the wizard object it is part of. "

you mean wizard() ?



class myWizard : public QWizard
{
Q_OBJECT

public:
whatever* m_member;
}


in the myWizard constructor i do :
addPage( new myPage1( ) );





myPage1::mypage1(QWidget *parent) : QWizardPage(parent) {

wizard()->m_member ? (is this supposed to work?) (apparently it doesnt) (thats basically what i want :D )

}



Can I reimplement ::next ? http://doc.trolltech.com/4.5/qwizard.html#next [slot] (doesnt say virtual, but I'm no c++ expert)

I do realise your time is more precious than mine, so I *am* thankful.

wysota
6th May 2010, 20:13
"For instance each page has a pointer to the wizard object it is part of. "

you mean wizard() ?
Yes, you can cast it to a proper type and use your methods.


MyWizard *wiz = qobject_cast<MyWizard*>(wizard()); // or static_cast if MyWizard doesn't have Q_OBJECT macro
wiz->something();


Can I reimplement ::next ? http://doc.trolltech.com/4.5/qwizard.html#next [slot] (doesnt say virtual, but I'm no c++ expert)
Reimplement nextId().

BillGates
6th May 2010, 21:07
Thanks so much for the code snippet, I didnt find this anywhere, in no example! Perhaps they considered normal people would know this..(qobject_cast). I had no clue that that must be done :< Thank you

About ::next(), I understand you are saying to Not reimplement ::next, but reimplement ::nextId() instead. I want to do some stuff when the user clicks "next" so ::next() seems more intuitive.. Basically, you are saying to "do some stuff" not in ::next but in ::nextId() ?



i was thinking

void myWizard::next() {

..do some stuff...

QWizard::next();
}
is this wrong in any way ?



PS: apparently if I do in myWizard constructor :
setButton(QWizard::NextButton, mybuton);
connect ( mybuton, SIGNAL(clicked()), this, SLOT(my_slot()));

my_slot() is executed AFTER the next Page's initializePage() ?!?! (is this logical?)

wysota
6th May 2010, 22:03
Thanks so much for the code snippet, I didnt find this anywhere, in no example! Perhaps they considered normal people would know this..(qobject_cast). I had no clue that that must be done :< Thank you
It's there in many places, I assure you.


About ::next(), I understand you are saying to Not reimplement ::next, but reimplement ::nextId() instead. I want to do some stuff when the user clicks "next" so ::next() seems more intuitive..
No, in that case you need to reimplement QWizardPage::initializePage() on the next page you want to show.




PS: apparently if I do in myWizard constructor :
setButton(QWizard::NextButton, mybuton);
connect ( mybuton, SIGNAL(clicked()), this, SLOT(my_slot()));

my_slot() is executed AFTER the next Page's initializePage() ?!?! (is this logical?)
Yes, it's logical.


Read this article, it might help you understand how wizards work:
http://doc.trolltech.com/qq/qq22-qwizard.html

BillGates
7th May 2010, 02:48
ok, i dont know if this is normal or not.... but apparently after casting i cannot access data members of wiz-> or even methods of wiz that themselves access members of the wizard object. I get a SIGSEGV upon running the program... :( (tried with static_cast too, although i have the q_object macro)

wysota
7th May 2010, 07:43
Check if wizard() doesn't return 0 or that qobject_cast doesn't return 0.

BillGates
7th May 2010, 11:45
in myPage1 constructor:

qDebug()<< this->wizard() ; // outputs QObject(0x0)

myWizard *wiz = qobject_cast<myWizard*>( wizard() ); //also tried myWizard *wiz = qobject_cast<myWizard*>((myWizard*) wizard() );

if(wiz) { wiz->my_slot("testing") ; //does not get executed }




i set the Page in the myWizard constructor using : addPage( new myPage1( ) );

What am i screwing up?

wysota
7th May 2010, 11:57
Before you add the page to the wizard (which happens after the page constructor is executed) the page doesn't belong to the wizard so wizard() returns 0.

BillGates
7th May 2010, 12:29
I am so ashamed of myself. I am a complete idiot. I will give up programming and go be a janitor... Qt is great though....