Results 1 to 6 of 6

Thread: Displat query results in a table

  1. #1

    Default Displat query results in a table

    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:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtSql>
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21.  
    22. private slots:
    23. void on_pushButton_2_clicked();
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. #include <QtSql>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. int testUpload;
    8. QApplication a(argc, argv);
    9. MainWindow w;
    10. w.show();
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtSql>
    4. #include <string>
    5. #include <iostream>
    6.  
    7. using namespace std;
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. }
    20.  
    21. void MainWindow::on_pushButton_2_clicked()
    22. {
    23. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    24. db.setHostName("localhost");
    25. db.setDatabaseName("mysql");
    26. db.setUserName("root");
    27. db.setPassword("");
    28. if (!db.open())
    29. cout << "Fail!" << endl;
    30. else
    31. cout << "Success!" << endl;
    32.  
    33. QSqlQuery query("SELECT * FROM help_keyword");
    34. QSqlRecord rec = query.record();
    35. query.exec();
    36.  
    37. model->setTable("Kljucne reci");
    38. for(int j = 0; j < rec.count();j++) {
    39. string atr = rec.field(j).name().toStdString();
    40. // 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
    41. model->setHeaderData(j, Qt::Horizontal, "lala");
    42. }
    43. model->select();
    44. tb->setModel(model);
    45. //tb->setItemDelegate(new QSqlRelationalDelegate(tb));
    46. tb->setWindowTitle("Kljucne reci!");
    47. /*
    48.   * Command line output (works)
    49.   while(query.next()) {
    50.   cout << endl;
    51.   for(int i = 0; i < rec.count(); i++) {
    52.   QString str = query.value(i).toString();
    53.   cout << str.toStdString() << " ";
    54.   }
    55.   }
    56.   cout << endl;
    57.   */
    58. tb->show();
    59. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displat query results in a table

    *model is not initialized.
    line 37 should be
    Qt Code:
    To copy to clipboard, switch view to plain text mode 

  3. #3

    Default Re: Displat query results in a table

    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:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtSql>
    4. #include <string>
    5. #include <iostream>
    6.  
    7. using namespace std;
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. }
    20.  
    21. void MainWindow::on_pushButton_2_clicked()
    22. {
    23. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    24. db.setHostName("localhost");
    25. db.setDatabaseName("mysql");
    26. db.setUserName("root");
    27. db.setPassword("");
    28. if (!db.open())
    29. cout << "Fail!" << endl;
    30. else
    31. cout << "Success!" << endl;
    32.  
    33. QSqlQuery query("SELECT * FROM db");
    34. QSqlRecord rec = query.record();
    35. query.exec();
    36.  
    37. int k=0;
    38. while(query.next()) {
    39. k++;
    40. }
    41.  
    42. ui->tableWidget->setRowCount(k);
    43. ui->tableWidget->setColumnCount(rec.count());
    44. int m=0;
    45. while(query.next()) {
    46. for(int i = 0; i < rec.count(); i++) {
    47. QString str = query.value(i).toString();
    48. item->setData(Qt::UserRole,str);
    49. ui->tableWidget->setItem(m,i,item);
    50. }
    51. m++;
    52. }
    53. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displat query results in a table

    line 49. you should not set the data as UserRole

  5. #5

    Default Re: Displat query results in a table

    Ok, If that is the only problem and if that method is correct what am i supposed to set it to?

    Regards,
    Vlad

  6. #6
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Displat query results in a table

    Here you can read about the roles http://doc.qt.nokia.com/latest/qt.ht...mDataRole-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

Similar Threads

  1. How to delete results of QSqlQuery from table ?
    By homerun4711 in forum Newbie
    Replies: 2
    Last Post: 14th February 2011, 22:50
  2. QXmlQuery query results
    By JovianGhost in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2010, 03:02
  3. SQL slow query - table update
    By lasher in forum Newbie
    Replies: 4
    Last Post: 21st October 2009, 23:12
  4. QTreeView with results from an SQL query.
    By ederbs in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2007, 02:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.