#include "config.h"
#include <QtGui>
#include <QtCore>
#include "global.h"
{
setModal(true);
ui.setupUi(this);
connect(ui.apply,SIGNAL(clicked()),this,SLOT(apply()));
connect(ui.noapply,SIGNAL(clicked()),this,SLOT(noapply()));
settings
= new QSettings(ORGANISATIONNAME,APPLICATIONNAME,
this);
// Read configured values, if they exist
ui.hostname->setText(settings->value("hostname","").toString());
ui.port->setText(settings->value("port","").toString());
ui.database->setText(settings->value("database","").toString());
ui.username->setText(settings->value("username","").toString());
ui.password->setText(settings->value("password","").toString());
db->QSqlDatabase::addDatabase("QPSQL");
qDebug()<<db;
qDebug()<<db->isValid();
qDebug()<<"constructor done";
}
bool Config::createDatabaseTable()
{
bool ok=false;
if (! db->isOpen())
{
db->setHostName(settings->value("hostname","").toString());
QString tmp
= settings
->value
("port").
toString();
if (tmp != "");
db->setPort(tmp.toInt());
db->setDatabaseName(settings->value("database","").toString());
db->setUserName(settings->value("username","").toString());
db->setPassword(settings->value("password","").toString());
ok = db->open();
}
else
{
ok=true;
}
if (ok)
{
QSqlQuery result
= db
->exec
("SELECT * FROM DomainTable");
if (result.isValid())
{
// table already exist.
return true;
}
else
{
// goahead and try to create the database table
return false;
}
}
else
{
QMessageBox::critical( this,
"Failed to connect to database",
"Edit connection settings again and apply");
return false;
}
}
void Config::noapply()
{
this->done(0);
}
void Config::apply()
{
settings->setValue("hostname",ui.hostname->text());
settings->setValue("database",ui.database->text());
settings->setValue("port",ui.port->text());
settings->setValue("username",ui.username->text());
settings->setValue("password",ui.password->text());
settings->sync();
qDebug()<<"Appply";
qDebug()<<"valid "<<db->isValid();
//qDebug()<<"open "<<db->isOpen();
qDebug()<<"continue";
bool ok=false;
if (! db->isOpen())
{
db->setHostName(settings->value("hostname","").toString());
QString tmp
= settings
->value
("port").
toString();
if (tmp != "");
db->setPort(tmp.toInt());
db->setDatabaseName(settings->value("database","").toString());
db->setUserName(settings->value("username","").toString());
db->setPassword(settings->value("password","").toString());
ok = db->open();
}
else
ok=true;
if (ok)
{
ok = createDatabaseTable();
if (ok)
this->done(1);
else
QMessageBox::critical( this,
"Failed to create the database table",
"Check database log");
}
else
{
QMessageBox::critical( this,
"Failed to connect to database",
"Edit connection settings");
}
// else continue to work...
}
#include "config.h"
#include <QtGui>
#include <QtCore>
#include "global.h"
Config::Config(QWidget *parent)
: QDialog(parent, 0)
{
setModal(true);
ui.setupUi(this);
connect(ui.apply,SIGNAL(clicked()),this,SLOT(apply()));
connect(ui.noapply,SIGNAL(clicked()),this,SLOT(noapply()));
settings = new QSettings(ORGANISATIONNAME,APPLICATIONNAME,this);
// Read configured values, if they exist
ui.hostname->setText(settings->value("hostname","").toString());
ui.port->setText(settings->value("port","").toString());
ui.database->setText(settings->value("database","").toString());
ui.username->setText(settings->value("username","").toString());
ui.password->setText(settings->value("password","").toString());
db->QSqlDatabase::addDatabase("QPSQL");
qDebug()<<db;
qDebug()<<db->isValid();
qDebug()<<"constructor done";
}
bool Config::createDatabaseTable()
{
bool ok=false;
if (! db->isOpen())
{
db->setHostName(settings->value("hostname","").toString());
QString tmp = settings->value("port").toString();
if (tmp != "");
db->setPort(tmp.toInt());
db->setDatabaseName(settings->value("database","").toString());
db->setUserName(settings->value("username","").toString());
db->setPassword(settings->value("password","").toString());
ok = db->open();
}
else
{
ok=true;
}
if (ok)
{
QSqlQuery result = db->exec("SELECT * FROM DomainTable");
if (result.isValid())
{
// table already exist.
return true;
}
else
{
// goahead and try to create the database table
return false;
}
}
else
{
QMessageBox::critical( this, "Failed to connect to database", "Edit connection settings again and apply");
return false;
}
}
void Config::noapply()
{
this->done(0);
}
void Config::apply()
{
settings->setValue("hostname",ui.hostname->text());
settings->setValue("database",ui.database->text());
settings->setValue("port",ui.port->text());
settings->setValue("username",ui.username->text());
settings->setValue("password",ui.password->text());
settings->sync();
qDebug()<<"Appply";
qDebug()<<"valid "<<db->isValid();
//qDebug()<<"open "<<db->isOpen();
qDebug()<<"continue";
bool ok=false;
if (! db->isOpen())
{
db->setHostName(settings->value("hostname","").toString());
QString tmp = settings->value("port").toString();
if (tmp != "");
db->setPort(tmp.toInt());
db->setDatabaseName(settings->value("database","").toString());
db->setUserName(settings->value("username","").toString());
db->setPassword(settings->value("password","").toString());
ok = db->open();
}
else
ok=true;
if (ok)
{
ok = createDatabaseTable();
if (ok)
this->done(1);
else
QMessageBox::critical( this, "Failed to create the database table", "Check database log");
}
else
{
QMessageBox::critical( this, "Failed to connect to database", "Edit connection settings");
}
// else continue to work...
}
To copy to clipboard, switch view to plain text mode
<ui version="4.0" >
<class>Config</class>
<widget class="QDialog" name="Config" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>561</width>
<height>306</height>
</rect>
</property>
<property name="sizePolicy" >
<sizepolicy vsizetype="Fixed" hsizetype="Fixed" >
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle" >
<string>Database settings</string>
</property>
<widget class="QPushButton" name="apply" >
<property name="geometry" >
<rect>
<x>70</x>
<y>230</y>
<width>81</width>
<height>28</height>
</rect>
</property>
<property name="text" >
<string>Apply</string>
</property>
</widget>
<widget class="QPushButton" name="noapply" >
<property name="geometry" >
<rect>
<x>250</x>
<y>230</y>
<width>188</width>
<height>28</height>
</rect>
</property>
<property name="text" >
<string>Do NOT apply values above</string>
</property>
</widget>
<widget class="QLineEdit" name="username" >
<property name="geometry" >
<rect>
<x>100</x>
<y>130</y>
<width>451</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label" >
<property name="geometry" >
<rect>
<x>20</x>
<y>40</y>
<width>81</width>
<height>18</height>
</rect>
</property>
<property name="text" >
<string>Hostname</string>
</property>
</widget>
<widget class="QLabel" name="label_2" >
<property name="geometry" >
<rect>
<x>20</x>
<y>70</y>
<width>62</width>
<height>18</height>
</rect>
</property>
<property name="text" >
<string>Port</string>
</property>
</widget>
<widget class="QLabel" name="label_3" >
<property name="geometry" >
<rect>
<x>20</x>
<y>100</y>
<width>62</width>
<height>18</height>
</rect>
</property>
<property name="text" >
<string>Database</string>
</property>
</widget>
<widget class="QLabel" name="label_4" >
<property name="geometry" >
<rect>
<x>20</x>
<y>130</y>
<width>81</width>
<height>18</height>
</rect>
</property>
<property name="text" >
<string>Username</string>
</property>
</widget>
<widget class="QLineEdit" name="hostname" >
<property name="geometry" >
<rect>
<x>100</x>
<y>40</y>
<width>451</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="port" >
<property name="geometry" >
<rect>
<x>100</x>
<y>70</y>
<width>113</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="database" >
<property name="geometry" >
<rect>
<x>100</x>
<y>100</y>
<width>451</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="password" >
<property name="geometry" >
<rect>
<x>100</x>
<y>160</y>
<width>451</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_5" >
<property name="geometry" >
<rect>
<x>20</x>
<y>160</y>
<width>62</width>
<height>18</height>
</rect>
</property>
<property name="text" >
<string>Password</string>
</property>
</widget>
<widget class="QLabel" name="label_6" >
<property name="geometry" >
<rect>
<x>40</x>
<y>200</y>
<width>501</width>
<height>18</height>
</rect>
</property>
<property name="text" >
<string>Notice that hitting apply will access the database and create database tables!</string>
</property>
</widget>
</widget>
<tabstops>
<tabstop>hostname</tabstop>
<tabstop>port</tabstop>
<tabstop>database</tabstop>
<tabstop>username</tabstop>
<tabstop>password</tabstop>
<tabstop>apply</tabstop>
<tabstop>noapply</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>
<ui version="4.0" >
<class>Config</class>
<widget class="QDialog" name="Config" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>561</width>
<height>306</height>
</rect>
</property>
<property name="sizePolicy" >
<sizepolicy vsizetype="Fixed" hsizetype="Fixed" >
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle" >
<string>Database settings</string>
</property>
<widget class="QPushButton" name="apply" >
<property name="geometry" >
<rect>
<x>70</x>
<y>230</y>
<width>81</width>
<height>28</height>
</rect>
</property>
<property name="text" >
<string>Apply</string>
</property>
</widget>
<widget class="QPushButton" name="noapply" >
<property name="geometry" >
<rect>
<x>250</x>
<y>230</y>
<width>188</width>
<height>28</height>
</rect>
</property>
<property name="text" >
<string>Do NOT apply values above</string>
</property>
</widget>
<widget class="QLineEdit" name="username" >
<property name="geometry" >
<rect>
<x>100</x>
<y>130</y>
<width>451</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label" >
<property name="geometry" >
<rect>
<x>20</x>
<y>40</y>
<width>81</width>
<height>18</height>
</rect>
</property>
<property name="text" >
<string>Hostname</string>
</property>
</widget>
<widget class="QLabel" name="label_2" >
<property name="geometry" >
<rect>
<x>20</x>
<y>70</y>
<width>62</width>
<height>18</height>
</rect>
</property>
<property name="text" >
<string>Port</string>
</property>
</widget>
<widget class="QLabel" name="label_3" >
<property name="geometry" >
<rect>
<x>20</x>
<y>100</y>
<width>62</width>
<height>18</height>
</rect>
</property>
<property name="text" >
<string>Database</string>
</property>
</widget>
<widget class="QLabel" name="label_4" >
<property name="geometry" >
<rect>
<x>20</x>
<y>130</y>
<width>81</width>
<height>18</height>
</rect>
</property>
<property name="text" >
<string>Username</string>
</property>
</widget>
<widget class="QLineEdit" name="hostname" >
<property name="geometry" >
<rect>
<x>100</x>
<y>40</y>
<width>451</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="port" >
<property name="geometry" >
<rect>
<x>100</x>
<y>70</y>
<width>113</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="database" >
<property name="geometry" >
<rect>
<x>100</x>
<y>100</y>
<width>451</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="password" >
<property name="geometry" >
<rect>
<x>100</x>
<y>160</y>
<width>451</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_5" >
<property name="geometry" >
<rect>
<x>20</x>
<y>160</y>
<width>62</width>
<height>18</height>
</rect>
</property>
<property name="text" >
<string>Password</string>
</property>
</widget>
<widget class="QLabel" name="label_6" >
<property name="geometry" >
<rect>
<x>40</x>
<y>200</y>
<width>501</width>
<height>18</height>
</rect>
</property>
<property name="text" >
<string>Notice that hitting apply will access the database and create database tables!</string>
</property>
</widget>
</widget>
<tabstops>
<tabstop>hostname</tabstop>
<tabstop>port</tabstop>
<tabstop>database</tabstop>
<tabstop>username</tabstop>
<tabstop>password</tabstop>
<tabstop>apply</tabstop>
<tabstop>noapply</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>
To copy to clipboard, switch view to plain text mode
Bookmarks