I’ve already posted a question about this problem (http://qt-project.org/forums/viewthread/28174/) but unfortunately I have not got any answers to my questions.
I’ve decided to break my initial application to small pieces and build a very small QT application, from scratch, to make sure the problem exists and to have an example that I could share and see if someone helps me to understand what I am doing wrong or if it is some QT feature / limitation / bug or whatever.
The “problem”:
1) I’ve got one window showing records from a database table.
2) If somewhere in the application (testOkClicked), I have to update the database without using ‘transactions’ everything works ok.
3) If somewhere in the application (testFailClicked), I have to update the database using ‘transactions’ the records showing in the window (1) (*) disappeared and become blank records.
*) If I’ve got more windows (mdi) showing several tables, all windows, all tables records go BLANK simultaneously.
I am using : QT 5.0.2 + MinGW 4.7 + 32 bits + ODBC driver + Windows 7 (32) + SQL Server 2012 Express
Can someone help me to understand why using transaction()/commit() makes all records (all forms) BLANK?
Is it because I am using windows? ODBC? MinGW? QT!?
thanks in advance for any help.
Source Code:
main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString dbServer = "W7B32QT5\\SQLEXPRESS";
QString dbDatabase = "MYDB";
QString dbUser = "sa";
QString dbPass = "masterkey";
//QString connectionStr = QString::fromLocal8Bit("DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;UID=%3;PWD=%4;Trusted_Connection=Yes").arg(dbServer,dbDatabase,dbUser,dbPass);
QString connectionStr = QString::fromLocal8Bit("DRIVER={SQL Server Native Client 11.0};SERVER=%1;DATABASE=%2;UID=%3;PWD=%4;Trusted_Connection=Yes").arg(dbServer,dbDatabase,dbUser,dbPass);
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName(connectionStr);
if (! db.open()){
qDebug() << db.lastError();
}
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QDialog>
class QTableView;
class QSqlQueryModel;
class QVBoxLayout;
class QHBoxLayout;
class MainWindow : public QDialog
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
signals:
public slots:
void testOkClicked();
void testFailClicked();
private slots:
private:
QPushButton *testOkButton;
QPushButton *testFailButton;
QPushButton *closeButton;
QTableView *tblView;
QSqlQueryModel *sqlModel;
QVBoxLayout *mainLayout;
QHBoxLayout *buttonLayout;
void createGUI();
void setupSQL();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QtWidgets>
#include <QtSql>
#include <QSqlQueryModel>
MainWindow::MainWindow(QWidget *parent) :
QDialog(parent)
{
createGUI();
setupSQL();
}
void MainWindow::createGUI()
{
tblView = new QTableView;
testOkButton = new QPushButton(tr("TEST OK"));
testFailButton = new QPushButton(tr("TEST FAIL : Blank Records"));
closeButton = new QPushButton(tr("Close"));
connect(testOkButton, SIGNAL(clicked()), this, SLOT(testOkClicked()));
connect(testFailButton, SIGNAL(clicked()), this, SLOT(testFailClicked()));
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(testOkButton);
buttonLayout->addWidget(testFailButton);
buttonLayout->addStretch();
buttonLayout->addWidget(closeButton);
mainLayout = new QVBoxLayout;
mainLayout->addWidget(tblView);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
setWindowTitle(tr("TEST2 Dialog"));
}
void MainWindow::setupSQL()
{
sqlModel = new QSqlQueryModel;
sqlModel->setQuery("SELECT artigoid, descricao FROM artigos");
tblView->resizeColumnsToContents();
tblView->setAlternatingRowColors(true);
tblView->setSelectionMode(QAbstractItemView::SingleSelection);
tblView->setSelectionBehavior(QAbstractItemView::SelectRows);
tblView->setModel(sqlModel);
tblView->selectRow(0);
tblView->setFocus();
}
void MainWindow::testOkClicked()
{
QSqlQuery query;
query.prepare("DELETE FROM artigos WHERE artigoid = :artigoid");
query.bindValue(":artigoid", "abc");
query.exec();
}
void MainWindow::testFailClicked()
{
QSqlDatabase::database().transaction();
QSqlQuery query;
query.prepare("DELETE FROM artigos WHERE artigoid = :artigoid");
query.bindValue(":artigoid", "abc");
query.exec();
QSqlDatabase::database().commit();
}
↧