Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Problems Qt Widgets applications and MySql

  1. #1
    Join Date
    Oct 2014
    Posts
    21
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Problems Qt Widgets applications and MySql

    This is just a part of the program. I don't understand why doesn't it throws the error
    Qt Code:
    1. 'mojabaza' was not declared in this scope
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. mojabaza=QSqlDatabase::addDatabase("QSQLITE");
    8. mojabaza.setDatabaseName(pot);
    9. QFileInfo preveri (pot);
    10. if (preveri.isFile())
    11. {if (mojabaza.open()) ui->status->setText("[+]Povezava z bazo je uspela!");
    12. }
    13. else
    14. ui->status->setText("[!]Povezava z bazo NI uspela!");
    15. this->model1=new QSqlQueryModel();
    16.  
    17. }
    18.  
    19. MainWindow::~MainWindow()
    20. {
    21. delete ui;
    22. mojabaza.close(); ///?? should throw the error here
    23. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problems Qt Widgets applications and MySql

    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,
    _

  3. #3
    Join Date
    Oct 2014
    Posts
    21
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems Qt Widgets applications and MySql

    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 ??

    Qt Code:
    1. QString s1 = "some SQL command";
    2.  
    3. 1) qry.exec(s1);
    4. 2) qry.exec("some SQL command");
    To copy to clipboard, switch view to plain text mode 
    Last edited by Emit; 31st October 2014 at 18:04.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problems Qt Widgets applications and MySql

    Line 3 and line 4 do the same thing, and will either work or not in the same way.

  5. #5
    Join Date
    Oct 2014
    Posts
    21
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems Qt Widgets applications and MySql

    I don't think it is the same:

    QString s1 = "xyz";

    Qt Code:
    1. qDebug() << s1;
    2. qDebug() << "xyz";
    To copy to clipboard, switch view to plain text mode 

    Output
    Qt Code:
    1. "xyz"
    2. xyz
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problems Qt Widgets applications and MySql

    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().

  7. #7
    Join Date
    Oct 2014
    Posts
    21
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems Qt Widgets applications and MySql

    I thought there is a difference if you pass a const char* or a QString to QSqlQuery::exec().

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problems Qt Widgets applications and MySql

    Quote Originally Posted by Emit View Post
    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,
    _

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problems Qt Widgets applications and MySql

    To be clear, when you compile this:
    Qt Code:
    1. bool ok = qry.exec("select count(*) from cpp_tutorials");
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. bool ok = qry.exec(QString("select count(*) from cpp_tutorials"));
    To copy to clipboard, switch view to plain text mode 
    This is C++ behaviour and not specific to Qt.

  10. #10
    Join Date
    Oct 2014
    Posts
    21
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems Qt Widgets applications and MySql

    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?

    Qt Code:
    1. ui->textBrowser_MySQL_log->append("SELECT * FROM learn_table ");
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problems Qt Widgets applications and MySql

    You can add any text you like.

  12. #12
    Join Date
    Oct 2014
    Posts
    21
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems Qt Widgets applications and MySql

    I found a way already.
    In case somebody will need it.

    Qt Code:
    1. ui->textBrowser_MySQL_log->append(QDateTime::currentDateTime().toString() + ": "+ "SELECT * FROM learn_table ");
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Oct 2014
    Posts
    21
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems Qt Widgets applications and MySql

    I found that it won't display
    Qt Code:
    1. ": "
    To copy to clipboard, switch view to plain text mode 
    this part of the string. What is the reason for it?


    Added after 14 minutes:


    Qt Code:
    1. ui->textBrowser_MySQL_log->append(QDateTime::currentDateTime().toString() + ": "+ "SELECT * FROM learn_table ");
    2. qDebug() << ui->textBrowser_MySQL_log->toPlainText();
    To copy to clipboard, switch view to plain text mode 

    Why doesn't the second line of the code prints text form the text browser?
    Last edited by Emit; 4th November 2014 at 09:17.

  14. #14
    Join Date
    Oct 2014
    Posts
    21
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems Qt Widgets applications and MySql

    I have Qt Widget application and inside I have a function:
    Qt Code:
    1. void MainWindow::on_pushButton_pull_DB_clicked()
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. void MainWindow::on_pushButton_pull_DB_clicked()
    2. {
    3. model1 = new QSqlQueryModel();
    4.  
    5. if (db.isOpen()){
    6.  
    7. model1->setQuery("SELECT * FROM learn_table");
    8. ui->textBrowser_MySQL_log->append(QDateTime::currentDateTime().toString() + ": "+ "SELECT * FROM learn_table ");
    9.  
    10. if (model1->lastError().isValid()){
    11. qDebug() << "Error_100: " << model1->lastError(); // isValid()?
    12. }
    13. }
    14. else {
    15. qDebug() << "Db is not opened, Error_101";
    16. }
    17.  
    18. ui->tableView->setModel(model1);
    19.  
    20. // delete model1; // If I uncomment this line the program will not work
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problems Qt Widgets applications and MySql

    look at the line before.

    Cheers,
    _

  16. #16
    Join Date
    Oct 2014
    Posts
    21
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems Qt Widgets applications and MySql

    where should i putt the delete statement than?

  17. #17
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problems Qt Widgets applications and MySql

    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.

  18. #18
    Join Date
    Oct 2014
    Posts
    21
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems Qt Widgets applications and MySql

    I placed it in the destructor but the program crashes.

  19. #19
    Join Date
    Oct 2014
    Posts
    21
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems Qt Widgets applications and MySql

    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?

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QtSql>
    3. #include <Qdebug>
    4. #include <iostream>
    5. #include <QString>
    6. #include <QStringList>
    7.  
    8. using namespace std;
    9.  
    10. int main()
    11. {
    12. QString string1;
    13.  
    14.  
    15. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    16. db.setHostName("localhost");
    17. db.setDatabaseName("cpp_delavnica");
    18. db.setUserName("root");
    19. db.setPassword("foo.bar");
    20.  
    21. // QStringList lst = db.tables();
    22.  
    23. // foreach(QString itm, lst){
    24.  
    25. // qDebug() << itm;
    26. // }
    27.  
    28.  
    29.  
    30. return 0;
    31. }
    To copy to clipboard, switch view to plain text mode 


    Added after 16 minutes:


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

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QtSql>
    3. #include <Qdebug>
    4. #include <iostream>
    5. #include <QString>
    6. #include <QStringList>
    7.  
    8. using namespace std;
    9.  
    10. int main()
    11. {
    12. QString string1;
    13.  
    14.  
    15. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    16. db.setHostName("localhost");
    17. db.setDatabaseName("cpp_delavnica");
    18. db.setUserName("root");
    19. db.setPassword("foo.bar");
    20.  
    21. db.open();
    22.  
    23. QStringList lst = db.tables();
    24.  
    25. foreach(QString itm, lst){
    26.  
    27. qDebug() << itm;
    28. }
    29.  
    30.  
    31.  
    32. return 0;
    33. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Emit; 7th November 2014 at 09:08.

  20. #20
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problems Qt Widgets applications and MySql

    Quote Originally Posted by Emit View Post
    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,
    _

Similar Threads

  1. Problems with QODBC and MySQL
    By crazymonkey in forum Qt Programming
    Replies: 0
    Last Post: 25th April 2011, 12:34
  2. Problems running applications .exe
    By croussou in forum Installation and Deployment
    Replies: 8
    Last Post: 25th March 2011, 18:07
  3. Problems with MySQL on Ubuntu
    By neoclaw in forum Qt Programming
    Replies: 3
    Last Post: 21st June 2010, 02:26
  4. Qwt - Qt Widgets for Technical Applications
    By gandalf in forum Newbie
    Replies: 6
    Last Post: 5th May 2010, 15:09
  5. Problems with Qt 4.3.4 and MySql
    By Headhunter_X in forum Installation and Deployment
    Replies: 9
    Last Post: 19th March 2008, 06:17

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.