I have written a code for creation of a dialog that will display the contents of a tree stored in memory. The tree is created by reading data from a file.
Code:
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
#include <sstream>
#include <QtGui>
using namespace std;
class treenode;
char *stringtoarray(string);
struct leveldata
{
string name;
double efficiency;
long noofflops;
};
class treenode
{
public:
leveldata *data;
unsigned int level;
treenode *parent;
vector <treenode *> children;
void createnode(int h, treenode *r)
{
level=h;
parent=r;
}
void insertchild(treenode *c)
{
children.push_back (c);
}
void initdata(string e, double f, long g)
{
data->name=e;
data->efficiency=f;
data->noofflops=g;
}
treenode* traversal(void)
{
static vector <treenode*>::size_type p=0;
if(p<children.size())
{
return(children[p]);
++p;
}
else
{
p=0;
return NULL;
}
}
};
treenode *root=NULL;
treenode *curr=NULL;
class Mainwindow;
/*class pushbutton : public QPushButton, public treenode //This class enables the ues of right click mouse button
{
Q_OBJECT
public:
PushButton(QWidget *parent = 0);
protected:
void mouseReleaseEvent(QMouseEvent *e);
signals:
void rightClicked();
};
pushbutton::pushbutton(QWidget *parent) : QPushButton(parent) {}
void pushbutton::mouseReleaseEvent(QMouseEvent *e)
{
if (e->button() == Qt::RightButton)
{
emit rightClicked();
}
QPushButton::mouseReleaseEvent(e);
}
void pushbutton::rightClicked()
{*/
class Mainwindow : public QDialog, public treenode
{
Q_OBJECT
public:
Mainwindow();
void createbutton(treenode * node);
void adjustroot(QPushButton *button);
protected:
void closeevent(QCloseEvent *event);
public slots:
void displayinfo(treenode *node);
void displaychildren(treenode *node);
private:
QLabel *namelabel;
QLabel *efflabel;
QLabel *flopslabel;
QTextEdit *namefield;
QTextEdit *efffield;
QTextEdit *flopsfield;
QHBoxLayout *topleftlayout;
QHBoxLayout *bottomleftlayout;
QVBoxLayout *leftlayout;
QVBoxLayout *rightlayout;
QHBoxLayout *mainlayout;
QScrollArea *scrollbox;
QWidget *scrollwidget;
QHBoxLayout *scrollboxlayout;
QRect Rect;
float factor;
};
Mainwindow::Mainwindow(void)
{
topleftlayout=new QHBoxLayout;
bottomleftlayout=new QHBoxLayout;
leftlayout=new QVBoxLayout;
rightlayout=new QVBoxLayout;
mainlayout=new QHBoxLayout;
namefield=new QTextEdit;
efffield=new QTextEdit;
flopsfield=new QTextEdit;
treenode *tempnode;
tempnode=root->traversal();
scrollbox=new QScrollArea;
scrollbox->setWidgetResizable(true);
bottomleftlayout->addWidget(scrollbox);
scrollbox->setFrameShape(QFrame::NoFrame);
scrollwidget=new QWidget();
scrollboxlayout=new QHBoxLayout(scrollwidget);
scrollbox->setWidget(scrollwidget);
scrollbox->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollboxlayout->setAlignment(Qt::AlignLeft);
scrollboxlayout->setSizeConstraint(QLayout::SetMinimumSize);
while(tempnode!=NULL)
{
createbutton(tempnode);
tempnode=root->traversal();
}
Rect=QApplication::desktop()->screenGeometry();
factor=(float)(Rect.width()/Rect.height());
setMaximumSize(Rect.width()*0.7/factor, Rect.height()*0.4/factor);
leftlayout->addLayout(topleftlayout);
leftlayout->addLayout(bottomleftlayout);
mainlayout->addLayout(leftlayout);
mainlayout->addLayout(rightlayout);
}
void Mainwindow::createbutton(treenode *node)
{
QPushButton *newbutton =new QPushButton;
newbutton->setDefault(false);
newbutton->setEnabled(true);
if(node==root)
{
topleftlayout->addWidget(newbutton);
}
else
{
scrollboxlayout->addWidget(newbutton);
}
QObject::connect(newbutton, SIGNAL(pressed()), this, SLOT(displaychildren(node)));
QObject::connect(newbutton, SIGNAL(pressed()), this, SLOT(displayinfo(node)));
QObject::connect(newbutton, SIGNAL(pressed()), this, SLOT(adjustroot(newbutton)));
QObject::connect(newbutton, SIGNAL(released()), this, SLOT(displaychildren(node->parent)));
QObject::connect(newbutton, SIGNAL(released()), this, SLOT(displayinfo(node->parent)));
QObject::connect(newbutton, SIGNAL(released()), this, SLOT(adjustroot(node->parent)));
}
void Mainwindow::displaychildren(treenode *node)
{
treenode *tempnod;
tempnod=node->traversal();
scrollbox->setWidgetResizable(true);
bottomleftlayout->addWidget(scrollbox);
scrollbox->setFrameShape(QFrame::NoFrame);
scrollbox->setWidget(scrollwidget);
scrollbox->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollboxlayout->setAlignment(Qt::AlignLeft);
scrollboxlayout->setSizeConstraint(QLayout::SetMinimumSize);
while(tempnod!=NULL)
{
createbutton(tempnod);
tempnod=root->traversal();
}
Rect=QApplication::desktop()->screenGeometry();
factor=(float)(Rect.width()/Rect.height());
setMaximumSize(Rect.width()*0.7/factor, Rect.height()*0.4/factor);
leftlayout->addLayout(topleftlayout);
leftlayout->addLayout(bottomleftlayout);
mainlayout->addLayout(leftlayout);
mainlayout->addLayout(rightlayout);
}
void Mainwindow::displayinfo(treenode *node)
{
namefield->insertPlainText(stringtoarray(node->data->name));
namefield->setDocumentTitle("Name:");
rightlayout->addWidget(namefield);
stringstream ss(stringstream::in|stringstream::out);
ss<<node->data->efficiency;
string s=ss.str();
QString q1(s.c_str());
efffield->insertPlainText(q1);
efffield->setDocumentTitle("Efficiency:");
rightlayout->addWidget(efffield);
s.clear();
ss.str("");
ss<<node->data->noofflops;
s=ss.str();
QString q2(s.c_str());
flopsfield->insertPlainText(q2);
flopsfield->setDocumentTitle("Flops:");
rightlayout->addWidget(flopsfield);
}
void Mainwindow::adjustroot(QPushButton *button)
{
topleftlayout->addWidget(button);
}
/*void treenode::createnode(leveldata *a, int h, treenode *r)
{
data=a;
level=h;
}
void treenode::insertchild(treenode *c)
{
children.push_back (c);
}
void treenode::initdata(char *e, double f, long g,)
{
name=e;
efficiency=f;
noofflops=g;
}*/
int main(int argc, char *argv[])
{
unsigned int spaces=0;
string temp;
string temp2;
string levname;
double eff;
long flops;
int flag=1;
ifstream myfile("myfile.txt");
while(flag)
{
//Separating the data from the line read to initialise the data members of the object
unsigned int pos=spaces;
for(; temp[pos]!=' '; ++pos)
{
temp2.push_back(temp[pos]);
}
++pos;
levname=temp2;
temp2.clear();
for(; temp[pos]!=' '; ++pos)
{
temp2.push_back(temp[pos]);
}
++pos;
eff=atof(stringtoarray(temp2));
temp2.clear();
for(; pos<temp.length(); ++pos)
{
temp2.push_back(temp[pos]);
}
++pos;
flops=atol(stringtoarray(temp2));
temp2.clear();
//Creating a new node
treenode *newnode=new treenode;
newnode->initdata(levname, eff, flops);
newnode->createnode(spaces, curr);
if(!newnode->level)
{
root=newnode;
}
//Associating the child to the parent
curr->insertchild(newnode);
//Updating the curr pointer to point to the parent for next node to be created
if (getline(myfile, temp))
{
flag=1;
}
else
{
flag=0;
break;
}
spaces=0;
while(temp[spaces]==' ')
{
++spaces;
}
if(spaces>newnode->level)
{
curr=newnode;
}
else if(spaces<newnode->level)
{
treenode *temporary;
temporary=newnode;
while(!(temporary->level<spaces))
{
temporary=temporary->parent;
}
curr=temporary;
}
}
QApplication app(argc, argv);
Mainwindow *mywindow=new Mainwindow;
mywindow->show();
return app.exec();
}
char *stringtoarray(string s)
{
char *a=new char[s.size()+1];
memcpy(a, s.c_str(), s.size());
a[s.size()]='\0';
return a;
}
#include "entrytree2.moc"
Here is the file dat I'm reading :
Code:
a 48 23000
b 89 4500
c 40 2200
d 94 2300
e 39 12200
f 47 6250
g 98 3250
h 23 3000
i 88 5950
j 98 2590
k 80 2400
l 29 6300
m 70 4300
n 12 2000
I am getting a segmentation fault on running the code. Debugging using gdb gives the following error
Code:
#0 0x0804caa1 in main ()
I'm unable to understand what this means. I tried running the program in gdb and using where but dat didn't help. Can someone please help me out.