category tree recursive method

M

Milan Krejci

hello guys, where could i be wrong, please?

QTreeWidgetItem* sa::treeList(QTreeWidgetItem* item,int category) {
QString cid;
QTreeWidgetItem *new_item;

QSqlQuery query(db);
query.prepare("SELECT cid,cname FROM kategorie WHERE parent=? ORDER BY
cname"); query.addBindValue(category); query.exec();
while (query.next()) {
cid=query.value(0).toString();
new_item=new QTreeWidgetItem(item); //QTreeWidgetItem(parent);
new_item->setText(0,query.value(1).toString()); //setting text to cname
from the query
item=this->treeList(new_item,cid.toInt());
}
return item;
}
 
M

Milan Krejci

Tadeusz B. Kopec napsal(a):
hello guys, where could i be wrong, please?
[snip code]
You are wrong in posting here without reading the FAQ, especially
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
i am sorry. i expect the program to create a tree view of category tree.
it is qt 4.3 code, 2.6.20-16-generic #2 SMP x86_64 GNU/Linux.
and the whole thing goes like this. i believe/hope it is compilable by
itself as it is:


#ifndef MYQTAPP_H
#define MYQTAPP_H

#include "ui_sa.h"
#include <QtGui>
#include <QtSql>

class sa : public QWidget, private Ui::saDLG
{
Q_OBJECT

public:
sa(QWidget *parent = 0);
QMap <QTreeWidgetItem*,int> map_of_categories;

private: bool createConnection();
QSqlDatabase db;
QTreeWidgetItem* treeList(QTreeWidgetItem*,int);

public slots:
void getItem(QTreeWidgetItem*,int);
};


#endif

#include <QtGui>
#include <iostream>
sa::sa(QWidget *parent)
{
setupUi(this); // this sets up GUI
this->createConnection();

QTreeWidgetItem *item=new QTreeWidgetItem(treeWidget);
item->setText(0,tr("categories"));

item=this->treeList(item,7);

map_of_categories.insert(item,1);
treeWidget->addTopLevelItem(item);
connect( treeWidget, SIGNAL( itemClicked(QTreeWidgetItem*,int) ), this,
SLOT( getItem(QTreeWidgetItem*,int) ) );
}

QTreeWidgetItem* sa::treeList(QTreeWidgetItem* item,int category) {
QString cid,cname,parent;
QTreeWidgetItem *new_item;

QSqlQuery query(db);
query.prepare("SELECT cid,cname,parent FROM kategorie WHERE parent=?
ORDER BY cname"); query.addBindValue(category); query.exec();
if (!query.isActive()) { QMessageBox::warning(0, QObject::tr("Database
Error"),query.lastError().text()); }
while (query.next()) {
cid=query.value(0).toString(); cname=query.value(1).toString();
parent=query.value(2).toString();
//if (parent==QString::number(category))
{ textEdit->append("par:"+parent); textEdit->append("Cat
ID:"+QString::number(category));
item=new QTreeWidgetItem(item);
item->setText(0,cname); }
}
return item;
}

bool sa::createConnection()
{
//QTextCodec::setCodecForCStrings (
QTextCodec::codecForName("UTF-8"));QTextCodec::setCodecForLocale (
QTextCodec::codecForName("UTF-8"));
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost"); db.setDatabaseName("pd");
db.setUserName("root"); db.setPassword("31257");
if (!db.open()) { QMessageBox::critical(0, QObject::tr("Database
Error"),db.lastError().text());
return false; }
return true;
}
 
R

red floyd

Milan said:
Tadeusz B. Kopec napsal(a):
hello guys, where could i be wrong, please?
[snip code]
You are wrong in posting here without reading the FAQ, especially
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
i am sorry. i expect the program to create a tree view of category tree.

You still haven't told us what the problem is. So you expect it to
create a tree view. What's wrong. Is it not displaying at all? In that
case, you're probably OT, because we don't do Qt here. Is it displaying
a tree, but with the wrong data? Give us the info we need to help you.
 
M

Milan Krejci

well, it creates a tree like this
a
|-b
|-c
instead of a
|-b
|-c
|-d
you get me?


red floyd napsal(a):
Milan said:
Tadeusz B. Kopec napsal(a):
On Mon, 03 Dec 2007 17:48:08 +0100, Milan Krejci wrote:

hello guys, where could i be wrong, please?
[snip code]
You are wrong in posting here without reading the FAQ, especially
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
i am sorry. i expect the program to create a tree view of category tree.

You still haven't told us what the problem is. So you expect it to
create a tree view. What's wrong. Is it not displaying at all? In that
case, you're probably OT, because we don't do Qt here. Is it displaying
a tree, but with the wrong data? Give us the info we need to help you.


it is qt 4.3 code, 2.6.20-16-generic #2 SMP x86_64 GNU/Linux.
and the whole thing goes like this. i believe/hope it is compilable by
itself as it is:


#ifndef MYQTAPP_H
#define MYQTAPP_H

#include "ui_sa.h"
#include <QtGui>
#include <QtSql>

class sa : public QWidget, private Ui::saDLG
{
Q_OBJECT

public:
sa(QWidget *parent = 0);
QMap <QTreeWidgetItem*,int> map_of_categories;

private: bool createConnection();
QSqlDatabase db;
QTreeWidgetItem* treeList(QTreeWidgetItem*,int);

public slots:
void getItem(QTreeWidgetItem*,int);
};


#endif

#include <QtGui>
#include <iostream>
sa::sa(QWidget *parent)
{
setupUi(this); // this sets up GUI
this->createConnection();

QTreeWidgetItem *item=new QTreeWidgetItem(treeWidget);
item->setText(0,tr("categories"));

item=this->treeList(item,7);

map_of_categories.insert(item,1);
treeWidget->addTopLevelItem(item);
connect( treeWidget, SIGNAL( itemClicked(QTreeWidgetItem*,int) ),
this, SLOT( getItem(QTreeWidgetItem*,int) ) );
}

QTreeWidgetItem* sa::treeList(QTreeWidgetItem* item,int category) {
QString cid,cname,parent;
QTreeWidgetItem *new_item;

QSqlQuery query(db);
query.prepare("SELECT cid,cname,parent FROM kategorie WHERE
parent=? ORDER BY cname"); query.addBindValue(category); query.exec();
if (!query.isActive()) { QMessageBox::warning(0,
QObject::tr("Database Error"),query.lastError().text()); }
while (query.next()) {
cid=query.value(0).toString();
cname=query.value(1).toString(); parent=query.value(2).toString();
//if (parent==QString::number(category))
{ textEdit->append("par:"+parent); textEdit->append("Cat
ID:"+QString::number(category));
item=new QTreeWidgetItem(item);
item->setText(0,cname); }
}
return item;
}

bool sa::createConnection()
{
//QTextCodec::setCodecForCStrings (
QTextCodec::codecForName("UTF-8"));QTextCodec::setCodecForLocale (
QTextCodec::codecForName("UTF-8"));
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost"); db.setDatabaseName("pd");
db.setUserName("root"); db.setPassword("31257");
if (!db.open()) { QMessageBox::critical(0, QObject::tr("Database
Error"),db.lastError().text());
return false; }
return true;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,579
Latest member
CharaS3188

Latest Threads

Top