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.
Code:
#include "dbmanager.h"
DbManager
::DbManager(const QString &path
) { Q_UNUSED(path);
m_db.setDatabaseName("C:/Users/ss/Desktop/TestData.db");
if (!m_db.open())
{
qDebug() << "Error: connection with database failed";
}
else
{
qDebug() << "Database: connection ok";
}
}
bool DbManager
::addEntry(const QString &name
) {
bool success = false;
query.prepare("INSERT INTO TestData VALUES (:time)");
query.bindValue(":time, time);
if(query.exec())
{
success = true;
}
else
{
qDebug() << "addtemperature error:"
<< query.lastError();
}
return success;
qDebug() << query.isValid();
QSqlQueryModel model;
model.setQuery("SELECT * FROM TestData");
for (int i = 0; i < model.rowCount(); ++i) {
int id = model.record(i).value("id").toInt();
QString name = model.record(i).value("time").toString();
qDebug() << id << time;
}
QLabel *labelExp = new QLabel(this);
labelExp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
labelExp->setText("first line\nsecond line");
labelExp->setAlignment(Qt::AlignBottom | Qt::AlignRight);
//Named binding
QSqlQuery query1;
query1.prepare("INSERT INTO TestData (time,temperature) VALUES (:time, "
":temperature)");
query1.bindValue(":time","2020-11-31 10:19:38");
query1.bindValue(":temperature", 27.4);
query1.exec();
//Positional Binding
QSqlQuery query2;
query2.prepare("INSERT INTO TestData (time,temperature) VALUES (?, ?, )");
query2.addBindValue("2010-06-31 12:08:35");
query2.addBindValue(30.6);
query2.exec();
QSqlQueryModel *model1 = new QSqlQueryModel;
model1->setQuery("SELECT time, temperature FROM TestData");
model1->setHeaderData(0, Qt::Horizontal, tr("Time"));
model1->setHeaderData(1, Qt::Horizontal, tr("Temperature"));
QTableView *view = new QTableView;
view->setModel(model1);
view->show();
model1->setHeaderData(0, Qt::Horizontal, QObject::tr("Time"));
model1->setHeaderData(1, Qt::Horizontal, QObject::tr("Temperature"));
}
I am not getting any output on my screen. Please help me to get the output.
Thanks!
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):
Code:
MainWindow
::MainWindow( QWidget * pParent
){
labelExp->setText("first line\nsecond line");
labelExp->setAlignment(Qt::AlignBottom | Qt::AlignRight);
pLayout->addWidget( labelExp );
pLayout->addWidget( view );
pCentralWidget->setLayout( pLayout );
setCentralWidget( pCentralWidget );
// mpModel is a member variable of MainWindow
mpModel1
->setHeaderData
(0, Qt
::Horizontal,
QObject::tr("Time"));
mpModel1
->setHeaderData
(1, Qt
::Horizontal,
QObject::tr("Temperature"));
view->setModel( mpModel1 );
}
Tutorial Examples for TableModel view ,didn't run
Quote:
Originally Posted by
d_stranz
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
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!
Re: Tutorial Examples for TableModel view ,didn't run
Quote:
Originally Posted by
swati777999
What's the createView function ?
It's defined directly above the main function.
Quote:
Originally Posted by
swati777999
It's a little demotivating when the examples don't run smoothly!
The example compiles fine here...