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;
}