PDA

View Full Version : Problems Qt Widgets applications and MySql



Emit
31st October 2014, 12:28
This is just a part of the program. I don't understand why doesn't it throws the error

'mojabaza' was not declared in this scope




MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

mojabaza=QSqlDatabase::addDatabase("QSQLITE");
mojabaza.setDatabaseName(pot);
QFileInfo preveri (pot);
if (preveri.isFile())
{if (mojabaza.open()) ui->status->setText("[+]Povezava z bazo je uspela!");
}
else
ui->status->setText("[!]Povezava z bazo NI uspela!");
this->model1=new QSqlQueryModel();

}

MainWindow::~MainWindow()
{
delete ui;
mojabaza.close(); ///?? should throw the error here
}

anda_skoa
31st October 2014, 16:28
Why do you expect this to trigger an error in the destructor when the constructor does not?
It is either a class member, so accessible from all methods of the class, or it is not, in which case it is undeclared everywhere.

Cheers,
_

Emit
31st October 2014, 18:19
I figured out why it doesn't throw the error. Because it is defined in .h file like you said it is a class member.

Added after 1 24 minutes:

I have another question. Does function bellow bellow returns the same value?
If I leave code on line 3 in my program the program doesn't work if I leave code on line 4 it works ??


QString s1 = "some SQL command";

1) qry.exec(s1);
2) qry.exec("some SQL command");

ChrisW67
1st November 2014, 04:14
Line 3 and line 4 do the same thing, and will either work or not in the same way.

Emit
1st November 2014, 21:18
I don't think it is the same:

QString s1 = "xyz";



qDebug() << s1;
qDebug() << "xyz";


Output


"xyz"
xyz

ChrisW67
2nd November 2014, 03:31
The way a QString streams itself to QDebug object is different to the way a const char* is handled. This has nothing to with your question about QSqlQuery::exec().

Emit
2nd November 2014, 11:04
I thought there is a difference if you pass a const char* or a QString to QSqlQuery::exec().

anda_skoa
2nd November 2014, 12:58
I thought there is a difference if you pass a const char* or a QString to QSqlQuery::exec().

QSqlQuery::exec() takes a QString argument. You always pass a QString.
Either an explicitly constructed one or an implicitly converted one.

Cheers,
_

ChrisW67
2nd November 2014, 21:22
To be clear, when you compile this:


QSqlQuery qry;
bool ok = qry.exec("select count(*) from cpp_tutorials");
the compiler looks for a function matching the arguments provided. There is no QSqlQuery::exec(const char*) so the compiler looks for an exec() accepting a single argument that can be constructed from a const char*. QSqlQuery::exec(const QString&) is the only option so the compiler looks for, and uses, the QString conversion constructor to convert a const char* to QString. What actually gets compiled is equivalent to:


QSqlQuery qry;
bool ok = qry.exec(QString("select count(*) from cpp_tutorials"));
This is C++ behaviour and not specific to Qt.

Emit
3rd November 2014, 20:52
I want to add MySQL commands to the text browser in a way shown bellow. Is there a way to add time and date as well as well?


ui->textBrowser_MySQL_log->append("SELECT * FROM learn_table ");

ChrisW67
3rd November 2014, 21:02
You can add any text you like.

Emit
3rd November 2014, 21:21
I found a way already.
In case somebody will need it.


ui->textBrowser_MySQL_log->append(QDateTime::currentDateTime().toString() + ": "+ "SELECT * FROM learn_table ");

Emit
4th November 2014, 10:17
I found that it won't display
": " this part of the string. What is the reason for it?

Added after 14 minutes:



ui->textBrowser_MySQL_log->append(QDateTime::currentDateTime().toString() + ": "+ "SELECT * FROM learn_table ");
qDebug() << ui->textBrowser_MySQL_log->toPlainText();

Why doesn't the second line of the code prints text form the text browser?

Emit
5th November 2014, 09:43
I have Qt Widget application and inside I have a function:

void MainWindow::on_pushButton_pull_DB_clicked()

inside of that function I initialize a pointer called model1. Why can't I delete the pointer at the end of that function with delete model1? If I use the command my program won't work.



void MainWindow::on_pushButton_pull_DB_clicked()
{
model1 = new QSqlQueryModel();

if (db.isOpen()){

model1->setQuery("SELECT * FROM learn_table");
ui->textBrowser_MySQL_log->append(QDateTime::currentDateTime().toString() + ": "+ "SELECT * FROM learn_table ");

if (model1->lastError().isValid()){
qDebug() << "Error_100: " << model1->lastError(); // isValid()?
}
}
else {
qDebug() << "Db is not opened, Error_101";
}

ui->tableView->setModel(model1);

// delete model1; // If I uncomment this line the program will not work

}

anda_skoa
5th November 2014, 11:44
look at the line before.

Cheers,
_

Emit
5th November 2014, 21:05
where should i putt the delete statement than?

ChrisW67
6th November 2014, 10:47
You need to consider the lifetime of the model object. Create it when it needs to start existing and delete it when it needs to be disposed of. You cannot display the data from a model if you have deleted it.

Emit
6th November 2014, 23:39
I placed it in the destructor but the program crashes.

Emit
7th November 2014, 09:53
I wanted to write a simple program that connects to MySQL database and shows the name of the tables in a database.
Somehow I wasn't successful. Anybody knows why?


#include <QCoreApplication>
#include <QtSql>
#include <Qdebug>
#include <iostream>
#include <QString>
#include <QStringList>

using namespace std;

int main()
{
QString string1;


QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("cpp_delavnica");
db.setUserName("root");
db.setPassword("foo.bar");

// QStringList lst = db.tables();

// foreach(QString itm, lst){

// qDebug() << itm;
// }



return 0;
}

Added after 16 minutes:

i figured out what is wrong. Here is the working code if somebody else will need it.



#include <QCoreApplication>
#include <QtSql>
#include <Qdebug>
#include <iostream>
#include <QString>
#include <QStringList>

using namespace std;

int main()
{
QString string1;


QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("cpp_delavnica");
db.setUserName("root");
db.setPassword("foo.bar");

db.open();

QStringList lst = db.tables();

foreach(QString itm, lst){

qDebug() << itm;
}



return 0;
}

anda_skoa
7th November 2014, 09:56
I placed it in the destructor but the program crashes.

Did you unset the model on the view before deleting it or at least deleted the view before deleting the model?

Cheers,
_

Emit
7th November 2014, 10:09
Is that it?

delete ui;

anda_skoa
7th November 2014, 12:08
No, that just deletes the structure holding the pointers to all UI elements.

Why do you want to explicitly delete the model at all?
Just pass your window as a parent and let it delete the model just like it deletes its UI children.

Cheers,
_

Emit
7th November 2014, 15:15
To avoid garbage in the Heap.

Added after 1 23 minutes:

Is it possible to do this with a program and how would I do it?

You choose a Table(from MySQL database) with for example 4 fields and press a button.
When you press the button 4 labels (out of 7 labels) are named with the name fields of the chosen table.

For example table has fields: name, age, salary, email

When you choose the table with those fields 4 out of 7 labels text become age, name ...

anda_skoa
7th November 2014, 15:18
To avoid garbage in the Heap.

I was not suggesting to not delete the object, was I?



You choose a Table(from MySQL database) with for example 4 fields and press a button.
When you press the button 4 labels (out of 7 labels) are named with the name fields of the chosen table.

For example table has fields: name, age, salary, email

When you choose the table with those fields 4 out of 7 labels text become age, name ...

QDataWidgetMapper


Cheers,
_