PDA

View Full Version : [QT]Push button-no action



nqn
30th May 2010, 17:48
I can compile my program but when i press push button doesn't happen and i don't know why?


#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();

}


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "osm_sqlite.h"

namespace Ui {
class MainWindow;
}

class Osm_Sqlite;

class MainWindow : public QMainWindow {
Q_OBJECT
private:
Osm_Sqlite osmdb;
public:
MainWindow(QWidget *parent = 0);
~MainWindow();

protected:
void changeEvent(QEvent *e);

private:
Ui::MainWindow *ui;

private slots:
void on_pushButton_clicked();
};

#endif // MAINWINDOW_H


#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "osm_sqlite.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
osmdb=Osm_Sqlite("d:\\db1");
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

void MainWindow::on_pushButton_clicked()
{
osmdb.Create_clean_database();
//this->osmdb.Create_clean_database();
}

my class:


#ifndef OSM_SQLITE_H
#define OSM_SQLITE_H
#include <QtSql>
#include <QSqlQuery>
class Osm_Sqlite
{
private:
QSqlDatabase db;//=QSqlDatabase::addDatabase("QSQLITE");
QSqlQuery query;
public:
Osm_Sqlite();
Osm_Sqlite(QString patch_to_db);
~Osm_Sqlite();

bool Create_clean_database();

};

#endif // OSM_SQLITE_H


#include "osm_sqlite.h"

//konstruktory
Osm_Sqlite::Osm_Sqlite()
{
}
Osm_Sqlite::Osm_Sqlite(QString patch_to_db)
{
this->db=QSqlDatabase::addDatabase("QSQLITE");
this->db.setDatabaseName(patch_to_db);
bool ok = db.open();
qDebug() << ok;
if (!ok)
{
qDebug() << "Błąd: nie można się połączyć z bazą!";
if(this->db.lastError().isValid())
{
qDebug() << this->db.lastError();
}
}
else
{
qDebug() << "Nawiązano połączenie z bazą danych.";
}

this->query=QSqlQuery::QSqlQuery(this->db);
//spowoduje że SQLite nie będzie czekało aż operacje na dysku zostaną wykonane. Po prostu zleci ich wykonanie i będzie dalej kontynuować swoje operacje. Może to znacząco zwiększyć szybkość SQLite.
//query.exec("PRAGMA synchronous=OFF");
db.close();
}
//destruktor
Osm_Sqlite::~Osm_Sqlite()
{
this->db.close();
delete &db;
}

// usuwa wszystkie wpiosy z bazy danych i tworzy czystÄ… strukture nowej bazy danych;
bool Osm_Sqlite::Create_clean_database()
{

bool ok = db.open();

QSqlDatabase::database().transaction();

// query.exec("select * from tb11");
//
// while (query.next())
// {
// QString name = query.value(0).toString();
//
// qDebug() << name;
// }

//usuniecie tabel
query.exec("drop table Pacjent");
query.exec("drop table Dysk");
query.exec("drop table PlikDicom");

query.exec("CREATE TABLE Pacjent(id_p INTEGER PRIMARY KEY,Imie VARCHAR(20),Nazwisko VARCHAR(20),Pesel VARCHAR(11),Plec VARCHAR(1));");
query.exec("CREATE TABLE Dysk(id_d INTEGER PRIMARY KEY,NazwaPlyty VARCHAR(20),DataArch VARCHAR(8),id_p INTEGER;");
query.exec("CREATE TABLE PlikDicom(id_pd INTEGER PRIMARY KEY,NazwaPliku VARCHAR(20),DataUtworzenia VARCHAR(8),SeriesDescript VARCHAR(20),Modality VARCHAR(10));");

query.exec("CREATE INDEX name_ind on Pacjent(Nazwisko);");
query.exec("CREATE INDEX pesel_ind on Pacjent(Pesel);");
query.exec("CREATE INDEX dane on Pacjent(Pesel,Nazwisko);");
query.exec("CREATE INDEX nazwaplyty_ind on Dysk(NazwaPlyty);");
query.exec("CREATE INDEX dataarch_ind on Dysk(DataArch);");

//spowoduje że SQLite nie będzie czekało aż operacje na dysku zostaną wykonane
//query.exec("PRAGMA synchronous=OFF");
QSqlDatabase::database().commit();
//wyswietla informacje jesli nastapily jakies bledy
if(db.lastError().isValid())
{
qDebug() << db.lastError();
return false;
}
db.close();
return true;
}

tbscope
30th May 2010, 17:54
Where do you call on_pushButton_clicked()?

You need to connect a signal to this slot.

squidge
30th May 2010, 18:07
Maybe he is depending on the connectSlotsByName() call in the setupui() method.

nqn
30th May 2010, 18:50
Maybe i write wrong. When i run my program nth happend when i clicked on button. When i uncomment in
Osm_Sqlite::Create_clean_database() :

section:

query.exec("select * from tb11");
while (query.next())
{
QString name = query.value(0).toString();
qDebug() << name;
}

i should have sth print but nth happen

tbscope
30th May 2010, 19:08
If you use the database like this:


QSqlDatabase::database().transaction();
You need to set a connection name.

Otherwise, try:


db.transaction();

And the same with commit();