Wizard pages
-
I am creating a Wizard given below:
@//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();
}
//dynamic.h
#ifndef DYNAMIC_H
#define DYNAMIC_H#include<QWizard>
#include <QtGui>class dynamic : public QWizard
{
Q_OBJECTpublic:
explicit dynamic(QWidget *parent = 0);
~dynamic(){}};
class GraphInfoPage: public QWizardPage
{
Q_OBJECT
public:
GraphInfoPage();
//static int getnum(){return num;}
protected:
//static int num;
private:
friend void abc(int &);
QLabel *VerticesLabel;
QLabel *EdgesLabel;
QSpinBox *edgesSpinbox;
QGridLayout *layout;
QSpinBox *verticesSpinbox;
public slots:
//void handleInt(int i);
};
//int GraphInfoPage::num;
class VerticesInfoPage: public QWizardPage
{
Q_OBJECT
public:
friend void abc();
VerticesInfoPage();
private:
int i;
QSpinBox *Spinbox;
QLineEdit *LineEdit[100];
QLineEdit *LineEit;
//const char *str;
char ch[2];
QString str;
QGridLayout *lay;
protected:
void initializePage();
};
#endif // DYNAMIC_H//dynamic.cpp
#include "dynamic.h"
#include <QtGui>
dynamic::dynamic(QWidget *parent)
: QWizard(parent)
{
addPage(new GraphInfoPage);
addPage(new VerticesInfoPage);setWindowTitle(tr("Graph Wizard"));
}
GraphInfoPage::GraphInfoPage()
{
QSlider sl;
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)),&sl,SLOT(setValue(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()
{
setTitle(tr("<i>Enter vertices: </i>"));
lay = new QGridLayout;
setLayout(lay);
int k=0,j=0;
LineEit=new QLineEdit;
lay->addWidget(LineEit);
Spinbox=new QSpinBox;
setLayout(lay);
//for creating text boxes
for(int n=0;n<i;n++)
{
LineEdit[n]=new QLineEdit;
LineEdit[n]->setMaximumWidth(35);
LineEdit[n]->setMaxLength(3);
lay->addWidget(LineEdit[n],++j,k);
if(j==4){j=0;k++;}
}
}
void VerticesInfoPage::initializePage()
{
str=field("vertices").toString();
LineEit->setText(str);
i=str.toInt();
}@
in VerticesInfopage i want to create as many text boxes as entered by user in GraphinfoPage...For this to do I converted value of str to integer i in initializepage
but when I tried to create textboxes using following code
@ for(int n=0;n<i;n++)
{
LineEdit[n]=new QLineEdit;
LineEdit[n]->setMaximumWidth(35);
LineEdit[n]->setMaxLength(3);
lay->addWidget(LineEdit[n],++j,k);
if(j==4){j=0;k++;}
}@
the program exited saying:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
The program has unexpectedly finished.
plz tel wats wrong and what should i do to correct it... -
Try stepping through the code in debug mode to find the exact line of the bad alloc.
Some notes on your code:
@
QLineEdit *LineEdit[100];
QLineEdit LineEit;
@
That's madness ;). LineEit is just a typo of LineEdit and because you needed a new variable name you just kept using it and then created a new variable with the correct spelling? Don't do stuff like that, it provokes errors and misunderstandings.
Further, don't use plain C arrays or even pointer-arrays. It's 2012 and this is not high performance code, so please, use a QList<QLineEdit>. This way your memory management becomes easier (see for example qDeleteAll();) and you don't restrict yourself to a hard coded limit of e.g. 100 vertex edit fields.@
for(int n=0;n<i;n++)
@
99% of all codes use n and i in such situations exactly the other way around, you should, too, unless you plan to lock your code in a box in the cellar and never show it to anybody else. This is not a question of taste but of efficiency and interoperability (between programmers). -
wel this is not the answer to my question...I thank you for pointing out my mistakes...Since I am new to Qt I am not aware of all the classes .. I am just going through it... I would appreciate if u help me correcting the error... Is there any tutorial for debugging techniques in Qt?? As far as N and i variable are concerned I did that in hurry ... never mind I will keep that in mind next time..but for now please help me if u can..
-
I already told you the technique to find that error here. Step through the code in debug mode.
Here's a Qt tutorial about debugging with QtCreator:
http://doc.qt.nokia.com/qtcreator-2.3/creator-debug-mode.html
Don't be overwhelmed by it, it tells you much more than you need to know here. Just compile in debug mode (computer-symbol on the bottom left), place a breakpoint at the beginning of the section where you suspect the crash by clicking in the blank area on the left of the line number (a red dot will appear), start in debug mode (start-arrow with bug infront of it). The program execution will halt at the breakpoint. Now you can step through the code execution line by line with the "step over" and "step inside" arrows at the bottom. And at some point your program will crash. The last line you've executed has caused the crash.