Results 1 to 4 of 4

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 16:43. Reason: missing [code] tags

Similar Threads

  1. Replies: 7
    Last Post: 1st December 2017, 16:27
  2. Replies: 1
    Last Post: 18th October 2011, 12:02
  3. present data in tabular form reading from a file
    By sachinmcajnu in forum Qt Programming
    Replies: 1
    Last Post: 2nd April 2011, 15:53
  4. Replies: 9
    Last Post: 19th November 2009, 10:18
  5. Replies: 1
    Last Post: 19th August 2007, 20: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.