Results 1 to 3 of 3

Thread: C++ Mysql QTableView - problem to put MySQL's table's data into the QTableView

  1. #1
    Join Date
    Nov 2015
    Posts
    1
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default C++ Mysql QTableView - problem to put MySQL's table's data into the QTableView

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++ Mysql QTableView - problem to put MySQL's table's data into the QTableView

    What if you pass the database connection to the query's constructor?

    And you probably don't want to leak the query object.

    You could also try setting the query and the database on the model using QSqlQueryModel::setQuery, the overload that takes a QString and a QSqlDatabase.

    Cheers,
    _

  3. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: C++ Mysql QTableView - problem to put MySQL's table's data into the QTableView

    Your code doesn't do any error checking. You will find/fix bugs much easier if you don't silently ignore the return values from methods. You check that your database is open, but not the result of the prepare and exec methods for example
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Visualising data from mysql table
    By vrltwe in forum Newbie
    Replies: 3
    Last Post: 30th December 2013, 18:14
  2. QTableView and MySQL
    By fnmblot in forum Newbie
    Replies: 3
    Last Post: 30th August 2013, 17:15
  3. Replies: 9
    Last Post: 14th February 2013, 13:14
  4. Strange problem with mysql and QTableView.
    By Datakim in forum Newbie
    Replies: 2
    Last Post: 26th July 2011, 11:30
  5. MYSQL 5 Table qt model as small Mysql admin
    By patrik08 in forum Qt-based Software
    Replies: 0
    Last Post: 1st May 2007, 10:43

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.