PDA

View Full Version : using wizard



arjita
24th July 2012, 15:05
i am creating a wizard in which in first page, i ll be asking a number( say num in a textbox) from user and in next page i ll be creating as many textboxes as entered by user in previous page (that is in num)..
initializePage() function of nextpage is:
void NextPage::initializePage() //let NextPage be a class
{
no_of_textboxes=field("num").toInt(); //is not working however
noOfTextboxes=field("num").toString();//is working
}
where no_of_textboxes(int type) and noOfTextboxes(QString type) are declared in class NextPage
nd num is registered field of class PreviousPage.
I tried
no_of_textboxes=noOfTextboxes.toInt(); even this didn't work...
Plz help ...

ChrisW67
25th July 2012, 02:42
"It doesn't work" is rarely an adequate description of a problem. What does it do? What were you expecting? What is the QVariant type and value of field("num")? What type of input widget is used to gather "num" on the first page?

amleto
25th July 2012, 11:09
Post complete, and compilabe example (see sig).

Also use code tags.

What is the (string) value of noOfTextboxes?

arjita
25th July 2012, 18:09
ok if my post is ambigoius i am posting the original code ....



class Dynamic : public QWizard
{
Q_OBJECT
public:
explicit Dynamic(QWidget *parent = 0);
~Dynamic();
void accept();
};
class GraphInfoPage: public QWizardPage
{
Q_OBJECT
public:
GraphInfoPage(QWidget *parent = 0);

private:
QLabel *VerticesLabel;
QLabel *EdgesLabel;
QLineEdit *VerticesLineEdit;
QLineEdit *EdgesLineEdit;
};
class VerticesInfoPage: public QWizardPage
{
Q_OBJECT
public:
VerticesInfoPage(QWidget *parent = 0);
private:
QLineEdit *LineEdit[];
QLineEdit *LineEit;
int v;
QGridLayout *layout;
protected:
void initializePage();

};
//dynamic.cpp
Dynamic:: Dynamic(QWidget *parent) :
QWizard (parent)
{
addPage(new GraphInfoPage);
addPage(new VerticesInfoPage);
setWindowTitle(tr("Graph Wizard"));
}
GraphInfoPage::GraphInfoPage(QWidget *parent): QWizardPage(parent)
{
setTitle(tr("<i>Enter Graph Details</i>"));
VerticesLabel = new QLabel(tr("<b>Vertices:</b> "));
QGridLayout *layout = new QGridLayout;
VerticesLineEdit = new QLineEdit;
VerticesLabel->setBuddy(VerticesLineEdit);
VerticesLineEdit->setMaxLength(2);
VerticesLineEdit->setMaximumWidth(35);

setLayout(layout);
registerField("vertices*", VerticesLineEdit);
layout->addWidget(VerticesLabel, 0,0);
layout->addWidget(VerticesLineEdit,0,1);
}
VerticesInfoPage::VerticesInfoPage(QWidget *parent):QWizardPage(parent)
{
setTitle(tr("<i>Enter vertices: </i>"));
layout = new QGridLayout;
setLayout(layout);
int k=0,j=0;
LineEit=new QLineEdit;
LineEit->setMaximumWidth(35);
LineEit->setMaxLength(3);
LineEit->setReadOnly(true);

layout->addWidget(LineEit,0,0);
/* for(int i=1;i<v;i++)
{
LineEdit[i]=new QLineEdit;
LineEdit[i]->setMaximumWidth(35);
LineEdit[i]->setMaxLength(3);
layout->addWidget(LineEdit[i],++j,k);
if(j==4){j=0;k++;}
}*/

}
void VerticesInfoPage::initializePage()
{
QString h=field("vertices").toString();
LineEit->setText(h);
}
//main.cpp



#include <QtGui/QApplication>
#include "dynamic.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dynamic w;
w.show();

return a.exec();
}

if now I want to convert sting h to integer v...
using
bool *ok;
v=h.toInt(ok,10);
this is not working in the sense nothing is getting stored in v... to check i used following code
char ch[2];
ch[0]=v;
ch[1]='\0';
and displayed in textbox LineEit but nothing is displayed... I want 'v' to hold the integer value entered by user in first page so that I can create as many text boxes as user want in next page that is VerticesInfoPage.
Hope I explained my problem ...plz help...

ChrisW67
26th July 2012, 01:44
Oh dear, where to begin.

bool *ok;
v=h.toInt(ok,10);

The first line declares a pointer to a bool but does not allocate space for a bool or initialise the pointer so that the second line is using an uninitialised pointer. If the second line survives the real possibility of crashing then v will contain something: an integer has no "nothing" value. I suggest you take a good look at the example usage of QString::toInt()


Your method of 'checking' is just plain broken. What do you expect to see if v == 0? What about v == 1? What is character 0 or 1?


BTW: If you want the user to enter a number it would be wise to only allow the user to enter a number using either a QSpinBox or by putting a QIntValidator on the QLineEdit.

arjita
28th July 2012, 10:45
k.. i changed the above code and used spinbox instead..now can u plz tel me how to take the value of spinBox as integer in next page...?

amleto
28th July 2012, 14:31
first, have you seen thisQSpinBox?

arjita
28th July 2012, 16:04
yes i have already seen this class but there is only signal valueChanged ( int i ) where integer stores value of the spinbox but to which QObject should I connect this signal, i am not getting this...plz help :(

amleto
28th July 2012, 17:14
connect it to whatever object would like to know that information

arjita
28th July 2012, 17:24
but i want to store that in an integer .. is it possible??

amleto
28th July 2012, 17:28
it gives you the value in an integer. so what is the problem?

arjita
28th July 2012, 17:36
see since valueChanged ( int i ) is a signal i have to connect it to some slot but I want the value of (int) i to be stored in some variable int num...and int num is not a QObject, I can't connect to num.. then how can I take the value of integer i...

amleto
28th July 2012, 18:10
you want to store it in 'int num' - but where is 'int num'? It must be in some class, so attach the signal to that class



class A : QObject
{
Q_OBJECT
public:
void fire() {emit sig(5);}

signals:
void sig(int);
};

class B : QObject
{
Q_OBJECT
public slots:
void handleint(int i) {num = i;}

private:
int num;
};

// some testing
void sometestclass:sometestfunc()
{
A a;
B b;

QObject::connect(&a, SIGNAL(sig(int)), &b, SLOT(handleint(int));

a.fire(); // 5 is sent to b through the signal/slot
}

arjita
28th July 2012, 19:16
I did what u said but no value is getting stored in num... here i am giving the defination of my classes GraphInfoPage and VerticesInfoPage
and num is private member of VerticesInfoPage...to check the value of num..i used spinBox in VertexInfoPage.. but no value of num is being displayed it is displaying 0..


class GraphInfoPage: public QWizardPage
{
Q_OBJECT
public:
GraphInfoPage();

private:

int num;
QLabel *VerticesLabel;
QLabel *EdgesLabel;
QSpinBox *edgesSpinbox;
QSpinBox *verticesSpinbox;
QGridLayout *layout;


};
class VerticesInfoPage: public QWizardPage
{
Q_OBJECT
public:
VerticesInfoPage(QWidget *parent = 0);
private:
QLineEdit *LineEdit[100];
QLineEdit *LineEit;
int num;
const char *str;
char ch[2];
QGridLayout *layout;
protected:
void initializePage();
public slots:
void handleInt(int i){num=i;}
};
//dynamic.cpp
GraphInfoPage::GraphInfoPage()
{
VerticesInfoPage vertex;
setTitle(tr("<i><font color=blue>Enter Graph Details</font></i>"));
VerticesLabel = new QLabel(tr("<b><font color=green>Vertices:</font></b> "));
layout = new QGridLayout;
EdgesLabel = new QLabel(tr("<b><font color=green>Edges:</font></b> "));
verticesSpinbox=new QSpinBox();
verticesSpinbox->setRange(0,100);
connect(verticesSpinbox,SIGNAL(valueChanged(int)),&vertex,SLOT(handleInt(int)));
edgesSpinbox=new QSpinBox();
edgesSpinbox->setRange(0,100);
registerField("vertices*",verticesSpinbox);
registerField("edges*",edgesSpinbox);
layout->addWidget(VerticesLabel,0,0);
layout->addWidget(verticesSpinbox,0,1);
layout->addWidget(EdgesLabel,2,0);
layout->addWidget(edgesSpinbox,2,1);
setLayout(layout);
}
VerticesInfoPage::VerticesInfoPage(QWidget *parent):QWizardPage(parent)
{
setTitle(tr("<i>Enter vertices: </i>"));
layout = new QGridLayout;
setLayout(layout);
int k=0,j=0;
LineEit=new QLineEdit;
LineEit->setReadOnly(true);
QSpinBox *Spinbox=new QSpinBox();
Spinbox->setValue(num);
layout->addWidget(Spinbox);
//for creating text boxes
for(int i=0;i<num;i++)
{
LineEdit[i]=new QLineEdit;
LineEdit[i]->setMaximumWidth(35);
LineEdit[i]->setMaxLength(3);
layout->addWidget(LineEdit[i],++j,k);
if(j==4){j=0;k++;}
}

}

amleto
28th July 2012, 19:48
erm, in your code you just use local variable then connect to it. What do you think happens to local variable when it exits scope?



GraphInfoPage::GraphInfoPage()
{
VerticesInfoPage vertex; <<<<<<<<<<<
...


that ^^ is pointless.

It is also not the same page that you put in your wizard...

Anyway, using wizard fields is much better qwizard#registering-and-using-fields