Hallo,

Could I please ask you to have a look at the below pieces of code? I have got a problem to output the content of my mysql's table into the QTableView which (QTableView) I composed using the QtCreator Designer. I mean, the thing is, that after compiling and running the applications there is the QTableView visible with the headers and counted rows but with no content. Could you please point me out where and what I am doing incorrect? I use Qt Creator 3.5.1 (opensource) , Ubuntu 15.10. Thank you.

practice_clients.h

Qt Code:
  1. #ifndef PRACTICE_CLIENTS_H
  2. #define PRACTICE_CLIENTS_H
  3.  
  4. #include <QtSql/QSql>
  5. #include <QCoreApplication>
  6. #include <QtSql/QSqlDriverPlugin>
  7. #include <QDialog>
  8. #include <QDebug>
  9. #include <QFileInfo>
  10.  
  11. #include <QtSql/qsqldatabase.h>
  12. #include <QtSql/QSqlQuery>
  13. #include <QDebug>
  14. #include <QSqlQueryModel>
  15. #include <QSqlTableModel>
  16. #include <QMessageBox>
  17. #include <QSqlError>
  18.  
  19.  
  20.  
  21. #include <QMainWindow>
  22.  
  23. namespace Ui {
  24. class Practice_Clients;
  25. }
  26.  
  27. class Practice_Clients : public QMainWindow
  28. {
  29. Q_OBJECT
  30.  
  31. public:
  32. explicit Practice_Clients(QWidget *parent = 0);
  33. ~Practice_Clients();
  34.  
  35.  
  36.  
  37. private:
  38. Ui::Practice_Clients *ui;
  39.  
  40. QSqlDatabase practice_clients_db;
  41. //QSqlQuery* query;
  42. //QSqlQueryModel *modal;
  43. //QSqlTableModel table_model;
  44. };
  45.  
  46. #endif // PRACTICE_CLIENTS_H
To copy to clipboard, switch view to plain text mode 


and practice_clients.cpp

Qt Code:
  1. #include "practice_clients.h"
  2. #include "ui_practice_clients.h"
  3.  
  4. Practice_Clients::Practice_Clients(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::Practice_Clients)
  7. {
  8. ui->setupUi(this);
  9.  
  10. QSqlDatabase practice_clients_db = QSqlDatabase::addDatabase("QMYSQL");
  11. practice_clients_db.setHostName("127.0.0.1");
  12. practice_clients_db.setDatabaseName("qt_mysql");
  13. practice_clients_db.setUserName("");
  14. practice_clients_db.setPassword("");
  15. practice_clients_db.open();
  16.  
  17. if (!practice_clients_db.open())
  18. {
  19. qDebug() << "Error";
  20. }
  21.  
  22. qDebug() << practice_clients_db.open();
  23.  
  24.  
  25. QSqlQuery* query = new QSqlQuery();
  26. query->prepare("SELECT * FROM gkUsers");
  27. query->exec();
  28.  
  29. modal->setQuery(*query);
  30.  
  31.  
  32. ui->tableView->setModel(modal);
  33. //practice_clients_db.close();
  34.  
  35. //qDebug() << model->rowCount();
  36.  
  37. }
  38.  
  39.  
  40. Practice_Clients::~Practice_Clients()
  41. {
  42. delete ui;
  43. }
To copy to clipboard, switch view to plain text mode