mi08037
26th March 2011, 16:29
Greetings,
I finally managed to get the MySQL driver to work and now I'm trying to present my sql query in a relational table with no success, i tried to figure out the example given by qt and program something similar.
What i managed to do is to print query results in command line but I cant display the results in a table on button click i think that i have segmentation fault but i cant figure out where because debugger shows assembler code.
Here is the code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSql>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void on_pushButton_2_clicked();
};
#endif // MAINWINDOW_H
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtSql>
int main(int argc, char *argv[])
{
int testUpload;
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql>
#include <string>
#include <iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_2_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("mysql");
db.setUserName("root");
db.setPassword("");
if (!db.open())
cout << "Fail!" << endl;
else
cout << "Success!" << endl;
QSqlQuery query("SELECT * FROM help_keyword");
QSqlRecord rec = query.record();
query.exec();
QSqlRelationalTableModel *model;
model->setTable("Kljucne reci");
for(int j = 0; j < rec.count();j++) {
string atr = rec.field(j).name().toStdString();
// Here is supposed to be field name but i dont know how to convert string or QString to const char * so i placed arbitary text just test it
model->setHeaderData(j, Qt::Horizontal, "lala");
}
model->select();
QTableView *tb = new QTableView;
tb->setModel(model);
//tb->setItemDelegate(new QSqlRelationalDelegate(tb));
tb->setWindowTitle("Kljucne reci!");
/*
* Command line output (works)
while(query.next()) {
cout << endl;
for(int i = 0; i < rec.count(); i++) {
QString str = query.value(i).toString();
cout << str.toStdString() << " ";
}
}
cout << endl;
*/
tb->show();
}
I finally managed to get the MySQL driver to work and now I'm trying to present my sql query in a relational table with no success, i tried to figure out the example given by qt and program something similar.
What i managed to do is to print query results in command line but I cant display the results in a table on button click i think that i have segmentation fault but i cant figure out where because debugger shows assembler code.
Here is the code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSql>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void on_pushButton_2_clicked();
};
#endif // MAINWINDOW_H
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtSql>
int main(int argc, char *argv[])
{
int testUpload;
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql>
#include <string>
#include <iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_2_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("mysql");
db.setUserName("root");
db.setPassword("");
if (!db.open())
cout << "Fail!" << endl;
else
cout << "Success!" << endl;
QSqlQuery query("SELECT * FROM help_keyword");
QSqlRecord rec = query.record();
query.exec();
QSqlRelationalTableModel *model;
model->setTable("Kljucne reci");
for(int j = 0; j < rec.count();j++) {
string atr = rec.field(j).name().toStdString();
// Here is supposed to be field name but i dont know how to convert string or QString to const char * so i placed arbitary text just test it
model->setHeaderData(j, Qt::Horizontal, "lala");
}
model->select();
QTableView *tb = new QTableView;
tb->setModel(model);
//tb->setItemDelegate(new QSqlRelationalDelegate(tb));
tb->setWindowTitle("Kljucne reci!");
/*
* Command line output (works)
while(query.next()) {
cout << endl;
for(int i = 0; i < rec.count(); i++) {
QString str = query.value(i).toString();
cout << str.toStdString() << " ";
}
}
cout << endl;
*/
tb->show();
}