Results 1 to 4 of 4

Thread: Displaying the result of SQL queries in tabular form in window without using .ui file

  1. #1
    Join Date
    Jul 2021
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Red face Displaying the result of SQL queries in tabular form in window without using .ui file

    Hi All,

    I want to display the result of my SQL queries [using SQLite driver] in Qt Mainwindow [through .setcentralWidget]. I don't have any .ui file in my project as I have to do it without any .ui file.

    Qt Code:
    1. #include "dbmanager.h"
    2.  
    3. DbManager::DbManager(const QString &path)
    4. { Q_UNUSED(path);
    5. m_db = QSqlDatabase::addDatabase("QSQLITE");
    6. m_db.setDatabaseName("C:/Users/ss/Desktop/TestData.db");
    7.  
    8. if (!m_db.open())
    9. {
    10. qDebug() << "Error: connection with database failed";
    11. }
    12. else
    13. {
    14. qDebug() << "Database: connection ok";
    15. }
    16. }
    17. bool DbManager::addEntry(const QString &name)
    18. {
    19. bool success = false;
    20. QSqlQuery query;
    21. query.prepare("INSERT INTO TestData VALUES (:time)");
    22. query.bindValue(":time, time);
    23. if(query.exec())
    24. {
    25. success = true;
    26. }
    27. else
    28. {
    29. qDebug() << "addtemperature error:"
    30. << query.lastError();
    31. }
    32.  
    33. return success;
    34. qDebug() << query.isValid();
    35.  
    36. QSqlQueryModel model;
    37. model.setQuery("SELECT * FROM TestData");
    38.  
    39. for (int i = 0; i < model.rowCount(); ++i) {
    40. int id = model.record(i).value("id").toInt();
    41. QString name = model.record(i).value("time").toString();
    42. qDebug() << id << time;
    43. }
    44.  
    45. QLabel *labelExp = new QLabel(this);
    46. labelExp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    47. labelExp->setText("first line\nsecond line");
    48. labelExp->setAlignment(Qt::AlignBottom | Qt::AlignRight);
    49.  
    50.  
    51. //Named binding
    52. QSqlQuery query1;
    53. query1.prepare("INSERT INTO TestData (time,temperature) VALUES (:time, "
    54. ":temperature)");
    55. query1.bindValue(":time","2020-11-31 10:19:38");
    56. query1.bindValue(":temperature", 27.4);
    57. query1.exec();
    58.  
    59. //Positional Binding
    60. QSqlQuery query2;
    61. query2.prepare("INSERT INTO TestData (time,temperature) VALUES (?, ?, )");
    62. query2.addBindValue("2010-06-31 12:08:35");
    63. query2.addBindValue(30.6);
    64. query2.exec();
    65.  
    66. QSqlQueryModel *model1 = new QSqlQueryModel;
    67. model1->setQuery("SELECT time, temperature FROM TestData");
    68. model1->setHeaderData(0, Qt::Horizontal, tr("Time"));
    69. model1->setHeaderData(1, Qt::Horizontal, tr("Temperature"));
    70.  
    71. QTableView *view = new QTableView;
    72. view->setModel(model1);
    73. view->show();
    74.  
    75. model1->setHeaderData(0, Qt::Horizontal, QObject::tr("Time"));
    76. model1->setHeaderData(1, Qt::Horizontal, QObject::tr("Temperature"));
    77. }
    To copy to clipboard, switch view to plain text mode 

    I am not getting any output on my screen. Please help me to get the output.

    Thanks!
    Last edited by d_stranz; 22nd November 2021 at 15:43. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,121
    Thanks
    291
    Thanked 847 Times in 834 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Displaying the result of SQL queries in tabular form in window without using .ui

    You need to make your QTableView and QLabel part of your MainWindow. Simply creating them as standalone widgets won't work.

    The rest of your code in your addEntry() method is completely messed up. I won't even try to fix it - it looks like you have no good idea what to do and are trying anything you can to get something to work. Why don't you study some of the Qt SQL examples. There are a lot of them, from basic to more advanced.

    Meanwhile, here is one way to create your MainWindow (untested code, might have bugs):

    Qt Code:
    1. MainWindow::MainWindow( QWidget * pParent )
    2. : QMainWindow( pParent )
    3. {
    4. QLabel *labelExp = new QLabel(this);
    5. labelExp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    6. labelExp->setText("first line\nsecond line");
    7. labelExp->setAlignment(Qt::AlignBottom | Qt::AlignRight);
    8.  
    9. QTableView *view = new QTableView( this );
    10.  
    11. QWidget * pCentralWidget = new QWidget( this );
    12. QVBoxLayout * pLayout = new QVBoxLayout;
    13.  
    14. pLayout->addWidget( labelExp );
    15. pLayout->addWidget( view );
    16. pCentralWidget->setLayout( pLayout );
    17.  
    18. setCentralWidget( pCentralWidget );
    19.  
    20. // mpModel is a member variable of MainWindow
    21. mpModel1 = new QSqlQueryModel;
    22. mpModel1->setHeaderData(0, Qt::Horizontal, QObject::tr("Time"));
    23. mpModel1->setHeaderData(1, Qt::Horizontal, QObject::tr("Temperature"));
    24.  
    25. view->setModel( mpModel1 );
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 23rd November 2021 at 16:47.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    swati777999 (23rd November 2021)

  4. #3
    Join Date
    Jul 2021
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Unhappy Tutorial Examples for TableModel view ,didn't run

    Quote Originally Posted by d_stranz View Post
    Why don't you study some of the Qt SQL examples? There are a lot of them, from basic to more advanced.

    }
    I have tried to follow some of these examples but I don't know why it does not run in my system[even after adding the required headerfiles]. Any suggestion for me to create small basic applications of my own?


    Added after 1 7 minutes:


    Quote Originally Posted by d_stranz View Post
    You need to make your QTableView and QLabel part of your MainWindow. Simply creating them as standalone widgets won't work.

    Why don't you study some of the Qt SQL examples. There are a lot of them, from basic to more advanced.

    }

    There's a sample code for TableModel Example https://doc.qt.io/qt-5.12/qtsql-tabl...model-cpp.html What's the createView function ? That's where I am getting the error. If I declare it in the .h file, it does not solve the issue either.

    It's a little demotivating when the examples don't run smoothly!
    Last edited by swati777999; 23rd November 2021 at 04:42.

  5. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Tutorial Examples for TableModel view ,didn't run

    Quote Originally Posted by swati777999 View Post
    What's the createView function ?
    It's defined directly above the main function.


    Quote Originally Posted by swati777999 View Post
    It's a little demotivating when the examples don't run smoothly!
    The example compiles fine here...

Similar Threads

  1. Replies: 7
    Last Post: 1st December 2017, 15:27
  2. Replies: 1
    Last Post: 18th October 2011, 11:02
  3. present data in tabular form reading from a file
    By sachinmcajnu in forum Qt Programming
    Replies: 1
    Last Post: 2nd April 2011, 14:53
  4. Replies: 9
    Last Post: 19th November 2009, 09:18
  5. Replies: 1
    Last Post: 19th August 2007, 19:50

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.