Results 1 to 3 of 3

Thread: QSqlDatabase model can't display data in QTableView

  1. #1
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QSqlDatabase model can't display data in QTableView

    Hi,

    I'm trying to display model's data from sqlite db in QTableView.
    I'm doing the follow but there is nothing inside the tableview.
    PLEASE HELP!

    Qt Code:
    1. #include "clients.h"
    2. #include "ui_clients.h"
    3. #include <QSqlQueryModel>
    4. #include <QMessageBox>
    5. #include <QSqlQuery>
    6. #include <QSqlTableModel>
    7. #include <QSqlError>
    8. #include <QDebug>
    9.  
    10. Clients::Clients(QWidget *parent) :
    11. QDialog(parent),
    12. ui(new Ui::Clients)
    13. {
    14. ui->setupUi(this);
    15.  
    16. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    17. db.setDatabaseName("faktura.sqlite");
    18. db.open();
    19.  
    20.  
    21. model->setQuery("SELECT ClientId, ClientName, ClientAddress FROM clients");
    22. model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    23. model->setHeaderData(1, Qt::Horizontal, QObject::tr("company name"));
    24. model->setHeaderData(2, Qt::Horizontal, QObject::tr("address"));
    25.  
    26. ui->tableView_clients->setModel(model);
    27. ui->tableView_clients->setColumnWidth(0,100);
    28. ui->tableView_clients->setColumnWidth(1,100);
    29. ui->tableView_clients->setColumnWidth(2,100);
    30. ui->tableView_clients->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
    31. ui->tableView_clients->horizontalHeader()->setStretchLastSection(true);
    32. ui->tableView_clients->show();
    33. }
    34.  
    35.  
    36. Clients::~Clients()
    37. {
    38. delete ui;
    39. }
    40.  
    41.  
    42. void Clients::on_pushButton_add_pressed()
    43. {
    44. GroupsAddDialog *dialog=new GroupsAddDialog(this);
    45. dialog->show();
    46. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2011
    Location
    Germany
    Posts
    27
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlDatabase model can't display data in QTableView

    After line 22 (model->setQuery(...)) add th e following o see, if there is an error:
    Qt Code:
    1. if (model->lastError().isValid())
    2. qDebug() << model->lastError();
    To copy to clipboard, switch view to plain text mode 
    Insanity: doing the same thing over and over again and expecting different results.
    Albert Einstein

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlDatabase model can't display data in QTableView

    Also check that "faktura.sqlite" is in the current working directory of the program. This is not necessarily the same as the source directory or the directory containing the executable.

Similar Threads

  1. Data from model not inserted in QTableView
    By schmimona in forum Qt Programming
    Replies: 1
    Last Post: 10th August 2011, 05:16
  2. Replies: 1
    Last Post: 24th February 2011, 05:54
  3. Replies: 1
    Last Post: 21st August 2010, 09:34
  4. Replies: 2
    Last Post: 24th July 2009, 22:09
  5. Replies: 1
    Last Post: 16th January 2008, 11:48

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.