Originally Posted by
goes2bob
I have main.cpp call gui.cpp which creates a stackedWidget with 4 widgets, 2 of these will (may) someday include QTableViews that are simple read-only tables from a database.
I'm a beginner too, so may be I am not right, but my main.cpp looks only like this:
// main.cpp
#include <QApplication>
int main( int argc, char* argv[])
{
Erpel myErpel;
myErpel.show();
return app.exec();
}
// main.cpp
#include <QApplication>
int main( int argc, char* argv[])
{
QApplication app(argc, argv);
Erpel myErpel;
myErpel.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Then I have a file called Erpel.cpp. This is the file where I put the "createConnection()" call.
// Erpel.cpp
#include "Erpel.h"
{
setupUi(this);
stackedWidget -> setCurrentIndex(0);
connect( actionHome,SIGNAL (triggered()), this, SLOT(slotHome()) );
connect( actionBackup,SIGNAL (triggered()), this, SLOT(fileBackup()) );
connect( actionRestore,SIGNAL (triggered()), this, SLOT(fileRestore()) );
// more code follows here ....
}
bool Erpel
::dbConnect(QString dbName
) {
QString iniFile
= curDir
+"/erpel.ini";
// get some parameters from erpel.ini
QString dbdriver
= settings.
value("database/dbdriver").
toString();
QString dbhost
= settings.
value("database/dbhost").
toString();
QString dbname
= settings.
value("database/dbname").
toString();
QString dbuser
= settings.
value("database/dbuser").
toString();
QString dbpasswd
= settings.
value("database/dbpasswd").
toString();
db.setHostName(dbhost);
db.setDatabaseName(dbName);
db.setUserName(dbuser);
db.setPassword(dbpasswd);
bool connect2Db = db.open(); // try to open the database ...
if (!connect2Db)
{
qApp->trUtf8("I was not able to connect to your database!"),
return false;
}
qDebug() << "dbconnect" << db.isValid();
return true;
}
// Erpel.cpp
#include "Erpel.h"
Erpel::Erpel(QWidget *parent) : QMainWindow( parent )
{
setupUi(this);
stackedWidget -> setCurrentIndex(0);
connect( actionHome,SIGNAL (triggered()), this, SLOT(slotHome()) );
connect( actionBackup,SIGNAL (triggered()), this, SLOT(fileBackup()) );
connect( actionRestore,SIGNAL (triggered()), this, SLOT(fileRestore()) );
// more code follows here ....
}
bool Erpel::dbConnect(QString dbName)
{
QString curDir = QCoreApplication::applicationDirPath();
QString iniFile = curDir+"/erpel.ini"; // get some parameters from erpel.ini
QSettings settings(iniFile, QSettings::IniFormat);
QString dbdriver = settings.value("database/dbdriver").toString();
QString dbhost = settings.value("database/dbhost").toString();
QString dbname = settings.value("database/dbname").toString();
QString dbuser = settings.value("database/dbuser").toString();
QString dbpasswd = settings.value("database/dbpasswd").toString();
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(dbhost);
db.setDatabaseName(dbName);
db.setUserName(dbuser);
db.setPassword(dbpasswd);
bool connect2Db = db.open(); // try to open the database ...
if (!connect2Db)
{
QMessageBox::critical(0, qApp->tr("ERROR!"),
qApp->trUtf8("I was not able to connect to your database!"),
QMessageBox::Cancel);
return false;
}
qDebug() << "dbconnect" << db.isValid();
return true;
}
To copy to clipboard, switch view to plain text mode
and
// Erpel.h // not complete, only as a sample ....
#ifndef ERPEL_H
#define ERPEL_H
#include <QtGui>
#include <QtCore/QVariant>
#include <QSettings>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
#include <QDebug>
#include "ui_erpel.h"
{
Q_OBJECT
public:
~Erpel();
public slots:
void slotClose();
private:
void adjustHeader();
private slots:
void editButton(int name);
protected:
signals:
};
#endif //ERPEL_H
// Erpel.h // not complete, only as a sample ....
#ifndef ERPEL_H
#define ERPEL_H
#include <QtGui>
#include <QtCore/QVariant>
#include <QSettings>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
#include <QDebug>
#include "ui_erpel.h"
class Erpel : public QMainWindow, public Ui::Erpel
{
Q_OBJECT
public:
Erpel (QWidget * parent = 0);
~Erpel();
QString dbdriver;
QString dbhost;
QString dbname;
QString dbuser;
QString dbpasswd;
bool dbConnect(QString dbName);
public slots:
void slotClose();
private:
void adjustHeader();
private slots:
void editButton(int name);
protected:
signals:
};
#endif //ERPEL_H
To copy to clipboard, switch view to plain text mode
I think I am thinking too much - cannot see thru the fog
.
I know that feeling very well, but may be the code above will help you a little bit to find your way even when it's foggy ....
Bookmarks