PDA

View Full Version : Displat query results in a table



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

Rhayader
26th March 2011, 17:42
*model is not initialized.
line 37 should be

QSqlRelationalTableModel *model=new QSqlRelationalTableModel(this);

mi08037
26th March 2011, 18:46
I figured out that using relationalmodel is not good choice since i'll have an arbitary query to display so i switched to other technique

I get the corretly sized empty table but no query result values are inserted

Here is the code:


#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 db");
QSqlRecord rec = query.record();
query.exec();

int k=0;
while(query.next()) {
k++;
}

ui->tableWidget->setRowCount(k);
ui->tableWidget->setColumnCount(rec.count());
int m=0;
while(query.next()) {
for(int i = 0; i < rec.count(); i++) {
QString str = query.value(i).toString();
QTableWidgetItem *item = new QTableWidgetItem();
item->setData(Qt::UserRole,str);
ui->tableWidget->setItem(m,i,item);
}
m++;
}
}

Rhayader
26th March 2011, 18:58
line 49. you should not set the data as UserRole

mi08037
27th March 2011, 03:23
Ok, If that is the only problem and if that method is correct what am i supposed to set it to?

Regards,
Vlad

Rhayader
27th March 2011, 15:32
Here you can read about the roles http://doc.qt.nokia.com/latest/qt.html#ItemDataRole-enum
The easiest way is to use item->setText(str) or pass str as argument in item's constructor. Since you are using QTableWidget you should familiarise yourself with QTableWidget and QTableWidgetItem