[Qt-interest] QMYSQL problem on ubuntu

Diego Schulz dschulz at gmail.com
Fri Jun 5 18:16:58 CEST 2009


On Fri, Jun 5, 2009 at 11:53 AM, Riccardo
Roasio<riccardo.roasio at gmail.com> wrote:
> Yes!
> Here is my code!
> Hope this can help...thanks so much!
> Riccardo
>
> cat MainWindow.cpp
> #include "MainWindow.h"
>
> MainWindow::MainWindow(QWidget *parent): QWidget(parent)
>  {
>        menuFrame=new QFrame(this,0);
>        menuFrame->setFrameStyle(QFrame::Box);
>        menuFrame->setGeometry( 2, 2, 196,696);
>
>        mainFrame=new QFrame(this,0);
>        mainFrame->setFrameStyle(QFrame::Box);
>        mainFrame->setGeometry( 200, 2, 796,696);
>
>        menuLayout=new QVBoxLayout(menuFrame);
>
>        calcButton=new QPushButton("&Calc",menuFrame);
>        editButton=new QPushButton("&Edit",menuFrame);
>        graphButton=new QPushButton("&Graph",menuFrame);
>        reportButton=new QPushButton("&Repo",menuFrame);
>        exitButton=new QPushButton("&Exit",menuFrame);
>
>
>        menuLayout->addWidget(calcButton);
>        menuLayout->addWidget(editButton);
>        menuLayout->addWidget(graphButton);
>        menuLayout->addWidget(reportButton);
>        menuLayout->addWidget(exitButton);
>
>        connect(calcButton, SIGNAL(clicked()), this, SLOT(ShowCalculatorScreen()));
>        connect(exitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
>
> }
>
> bool MainWindow::ConnectToMysql()
> {
>        bool result;
>        db=new QSqlDatabase();
>        db->addDatabase("QMYSQL");
>        db->setHostName("localhost");
>        db->setDatabaseName("osty");
>        db->setUserName("root");
>        db->setPassword("root");
>        result = db->open();
>        if(result)
>        {
>                 logOnFile("Connection with db opened");
>        }
>        else
>        {
>                logOnFile("Failed connecting with db.");
>                logOnFile(db->lastError().text());
>                logOnFile(db->drivers().join(","));
>        }
>
>        return result;
> }
>
> void MainWindow::ShowCalculatorScreen()
> {
>        logOnFile("Drawing calculator screen...");
>
>        calcMenuTab=new QTabBar(mainFrame);
>        calcMenuTab->setGeometry( 2, 2, 792,30);
>
>        QSqlQuery query("SELECT * FROM receipts limit 20");
>        int fieldNo = query.record().indexOf("id");
>
>        while (query.next()) {
>         QString receipt = query.value(fieldNo).toString();
>         calcMenuTab->addTab (receipt );
>
>        }
>
>        calcMenuTab->show();
>
> }
>
> void MainWindow::logOnFile(QString logString)
> {
>     QFile logFile("osty.log");
>     if (!logFile.open(QIODevice::Append))
>         return;
>
>     QTextStream out(&logFile);
>     out << logString << "\n";
>     logFile.close();
> }
>
> cat MainWindow.h
> #include <QWidget>
> #include <QApplication>
> #include <QVBoxLayout>
> #include <QFrame>
> #include <QPushButton>
> #include <QTabBar>
> #include <QFile>
> #include <QString>
> #include <QTextStream>
> #include <QtSql>
> #include <QString>
>
> class MainWindow : public QWidget
>  {
>        Q_OBJECT
>
>        QVBoxLayout *menuLayout;
>        QFrame *menuFrame;
>        QFrame *mainFrame;
>
>        QPushButton *calcButton;
>        QPushButton *editButton;
>        QPushButton *graphButton;
>        QPushButton *reportButton;
>        QPushButton *exitButton;
>
>        QSqlDatabase *db;
>        QTabBar *calcMenuTab;
>
>        public:
>                MainWindow(QWidget *parent = 0);
>                bool ConnectToMysql();
>
>        public slots:
>                void ShowCalculatorScreen();
>
>        private:
>                void logOnFile(QString logString);
>  };
>
> cat main.cpp
> #include <QApplication>
> #include <QPixmap>
> #include <QSplashScreen>
> #include "MainWindow.h"
>
>
> int main(int argc, char *argv[])
> {
>     QApplication app(argc, argv);
>
>     QPixmap pixmap("images/splash.png");
>     QSplashScreen splash(pixmap);
>     splash.show();
>     splash.showMessage("Loaded interface...");
>     MainWindow *window=new MainWindow();
>     splash.showMessage("Connecting to database...");
>     window->ConnectToMysql();
>     window->setGeometry( 0, 0, 1000, 700);
>     window->show();
>     //sleep(2);
>     splash.finish(window);
>     return app.exec();
> }
>
> cat Osty.pro
> ######################################################################
> # Automatically generated by qmake (2.01a) Fri Jun 5 14:43:49 2009
> ######################################################################
>
> TEMPLATE = app
> TARGET =
> DEPENDPATH += .
> INCLUDEPATH += .
>
> # Input
> HEADERS += MainWindow.h
> SOURCES += main.cpp MainWindow.cpp
> QT += sql
>


Your problem can be solved with some minor changes:

1) In MainWindow.h, Define db variable of type  QSqlDatabase *  to QSqlDatabase,

           QSqlDatabase db;

       instead of

           QSqlDatabase * db;


2)  Adapt the method MainWindow::ConnectToMysql()


bool MainWindow::ConnectToMysql()
{
       bool result;
       db = QSqlDatabase::addDatabase("QMYSQL");
       db.setHostName("localhost");
       db.setDatabaseName("osty");
       db.setUserName("root");
       db.setPassword("root");
       result = db.open();
       if(result)
       {
                logOnFile("Connection with db opened");
       }
       else
       {
               logOnFile("Failed connecting with db.");
               logOnFile(db.lastError().text());
               logOnFile(db.drivers().join(","));
       }

       return result;
}



But there is room for more improvements, your program will work, but
you will see some warnings in stderr.
warnings look like

QSqlDatabasePrivate::removeDatabase: connection
'qt_sql_default_connection' is still in use, all queries will cease to
work.



regards,

diego




More information about the Qt-interest-old mailing list