Results 1 to 5 of 5

Thread: QSqlQueryModel and QTableView question

  1. #1
    Join Date
    Sep 2006
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question QSqlQueryModel and QTableView question

    Hello.
    I am a newbie.

    here is part of my code:

    trans.h
    Qt Code:
    1. #ifndef TRANS_H
    2. #define TRANS_H
    3.  
    4. #include <QtSql>
    5. #include "ui_fleTrans.h"
    6.  
    7. class trans : public QWidget, private Ui::frmTrans
    8. {
    9. Q_OBJECT
    10. public:
    11. trans(QWidget *parent = 0);
    12.  
    13. public slots:
    14. ...
    15. void showTable(QSqlQueryModel *model);
    16. void initializeModel(QSqlQueryModel *model);
    17. ...
    18.  
    19. private:
    20. ...
    21. };
    22. #endif
    To copy to clipboard, switch view to plain text mode 

    trans.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include "trans.h"
    4. #include "custsqlmodel.h" //basically the same as customsqlmodel in sql/querymodel
    5. //example
    6.  
    7. trans::trans(QWidget *parent)
    8. {
    9. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    10. db.setHostName("localhost");
    11. db.setDatabaseName("databasename");
    12. db.setUserName("username");
    13. db.setPassword("password");
    14. if (!db.open())
    15. {
    16. QMessageBox::critical(0, "Database Error", db.lastError().text());
    17. }
    18.  
    19. setupUi(this);
    20.  
    21. model = new QSqlQueryModel;
    22.  
    23. CustSqlModel custSqlModel;
    24. initializeModel(&custSqlModel);
    25. showTable(&custSqlModel);
    26. }
    27.  
    28. void trans::initializeModel(QSqlQueryModel *model)
    29. {
    30. model->setQuery("select * from table");
    31. }
    32.  
    33. void trans::showTableLedger(QSqlQueryModel *model)
    34. {
    35. QTableView *view = new QTableView;
    36. view->setModel(model);
    37. view->show();
    38. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "trans.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. trans *dialog = new trans;
    8. dialog->show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    nothing wrong with my qsqlquerysublcass since it compiles without error and shows the data in the table using the sql/querymodel example.

    The above code simply shows a window frame with scroll bars, no grid, no column header, no data - just white space.

    Please help.

    Thank you in advance.

    wil
    Last edited by jacek; 20th September 2006 at 14:40. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQueryModel and QTableView question

    You have to call select() on the model for it to fetch data from the database.

  3. #3
    Join Date
    Sep 2006
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQueryModel and QTableView question

    Quote Originally Posted by wysota View Post
    You have to call select() on the model for it to fetch data from the database.
    Thank you for your reply but i must apologize since I am new to qt, i do not know how to implement a select() on the model.

    here is my model:

    custSqlModel.h
    Qt Code:
    1. #ifndef CUSTSQLMODEL_H
    2. #define CUSTSQLMODEL_H
    3. #include <QSqlQueryModel>
    4.  
    5. class CustSqlModel : public QSqlQueryModel
    6. {
    7. Q_OBJECT
    8. public:
    9. CustSqlModel(QObject *parent = 0);
    10. QVariant data(const QModelIndex &index, int role) const;
    11. };
    12. #endif /*LEDGERMODEL_H*/
    To copy to clipboard, switch view to plain text mode 

    custSqlModel.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "ledgermodel.h"
    3.  
    4. CustSqlModel::CustSqlModel(QObject *parent)
    5. : QSqlQueryModel(parent)
    6. {
    7. }
    8.  
    9. QVariant CustSqlModel::data(const QModelIndex &index, int role) const
    10. {
    11. QVariant value = QSqlQueryModel::data(index, role);
    12. if (value.isValid() && role == Qt::DisplayRole)
    13. {
    14. if (index.column() == 0)
    15. return value.toString();
    16. else if (index.column() == 1)
    17. return QString("%L1").arg(value.toDouble(), 0, 'f', 2);
    18. }
    19. else if (role == Qt::TextAlignmentRole && index.column() == 2)
    20. {
    21. return int(Qt::AlignRight | Qt::AlignVCenter);
    22. }
    23.  
    24. return value;
    25. }
    To copy to clipboard, switch view to plain text mode 

    there seems to be no select() member in QSqlQueryModel.

    Please help with code snippet or at least a hint on what to do.

    Thank you for your patience.

    wil
    Last edited by jacek; 20th September 2006 at 14:41. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQueryModel and QTableView question

    You don't implement it. I didn't notice you were using QSqlQueryModel, I somehow assumed it was QSqlTableModel.

    I see you have a problem in your code. You create the model (custSqlModel) on stack and it gets deleted when the constructor returns. On the other hand you have another model there (model), which looks like is not used at all...

    Try this:
    Qt Code:
    1. trans::trans(QWidget *parent){
    2. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    3. db.setHostName("localhost");
    4. db.setDatabaseName("databasename");
    5. db.setUserName("username");
    6. db.setPassword("password");
    7. if (!db.open())
    8. {
    9. QMessageBox::critical(0, "Database Error", db.lastError().text());
    10. }
    11. model = new CustSqlModel(this);
    12. model->setQuery("select * from table");
    13. view = new QTableView(this);
    14. view->setModel(model);
    15. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to wysota for this useful post:

    wbt_ph (20th September 2006)

  6. #5
    Join Date
    Sep 2006
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQueryModel and QTableView question

    You're the man!!!

    Code works now!!!

    Thank you so much!!!

Similar Threads

  1. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  2. [Qt 4.1] Strange behaviour with QTableView
    By fane in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 06:17

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.