PDA

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



swati777999
22nd November 2021, 09:31
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.


#include "dbmanager.h"

DbManager::DbManager(const QString &path)
{ Q_UNUSED(path);
m_db = QSqlDatabase::addDatabase("QSQLITE");
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;
QSqlQuery query;
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!

d_stranz
22nd November 2021, 16:02
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 (https://doc.qt.io/qt-5/examples-sql.html). 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):



MainWindow::MainWindow( QWidget * pParent )
: QMainWindow( pParent )
{
QLabel *labelExp = new QLabel(this);
labelExp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
labelExp->setText("first line\nsecond line");
labelExp->setAlignment(Qt::AlignBottom | Qt::AlignRight);

QTableView *view = new QTableView( this );

QWidget * pCentralWidget = new QWidget( this );
QVBoxLayout * pLayout = new QVBoxLayout;

pLayout->addWidget( labelExp );
pLayout->addWidget( view );
pCentralWidget->setLayout( pLayout );

setCentralWidget( pCentralWidget );

// mpModel is a member variable of MainWindow
mpModel1 = new QSqlQueryModel;
mpModel1->setHeaderData(0, Qt::Horizontal, QObject::tr("Time"));
mpModel1->setHeaderData(1, Qt::Horizontal, QObject::tr("Temperature"));

view->setModel( mpModel1 );
}

swati777999
23rd November 2021, 04:42
Why don't you study some of the Qt SQL examples (https://doc.qt.io/qt-5/examples-sql.html)? 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:


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 (https://doc.qt.io/qt-5/examples-sql.html). 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-tablemodel-tablemodel-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!

ChristianEhrlicher
23rd November 2021, 15:50
What's the createView function ?

It's defined directly above the main function.




It's a little demotivating when the examples don't run smoothly!

The example compiles fine here...