PDA

View Full Version : Problem with DB GUI



Shien
31st May 2011, 21:54
Sorry, I made topic there also but in newbie forum nobody even looked :/ I am making GUI for my database. But the problem is that is that I cant see my database's table :/ can anyone tell me the problem? :)



#ifndef CUSTOMIZEGAMETABLE_H
#define CUSTOMIZEGAMETABLE_H

#include <QtGui>
#include <QtSql>
#include "GameUpdateDialog.h"

class CustomizeGameTable : public QWidget
{
Q_OBJECT
private:
QSqlQueryModel *CurrentQueryModel;
QSqlRecord CurrentRecord;
bool DataSourceConnected;
QTableView *tableViewGameList;
GameUpdateDialog *dialogUpdate;
QPushButton *btnUpdate;
QGridLayout *grdLayout;
public:
explicit CustomizeGameTable(QWidget *parent = 0);

signals:
void executeQuery(QString parSQLQuery);
void executeUpdate(QString parSQLQuery);
void changedRecord(QSqlRecord parRecord);
void clickedUpdate(void);

private slots:
void clickedButtonUpdate(void);

public slots:
void connectData(void);
void processQuery(QSqlQueryModel *parModel);
void changeData(void);

};

#endif // CUSTOMIZEGAMETABLE_H


#include "customizegametable.h"

CustomizeGameTable::CustomizeGameTable(QWidget *parent) :
QWidget(parent)
{
CurrentQueryModel = 0;
//CurrentRecord = 0;
DataSourceConnected = false;
tableViewGameList = new QTableView(this);
dialogUpdate = new GameUpdateDialog(this);
btnUpdate = new QPushButton("Update",this);
grdLayout = new QGridLayout(this);

connect(btnUpdate, SIGNAL(clicked()), this, SLOT(clickedButtonUpdate()));
connect(this, SIGNAL(clickedUpdate()), dialogUpdate, SLOT(show()));
connect(this, SIGNAL(changedRecord(QSqlRecord)), dialogUpdate, SLOT(changeRecord(QSqlRecord)));
connect(dialogUpdate, SIGNAL(executeQuery(QString)), this, SIGNAL(executeUpdate(QString)));


grdLayout->addWidget(tableViewGameList, 0,0,1,2);
grdLayout->addWidget(btnUpdate, 1,1,1,1);
this->setLayout(grdLayout);
}
void CustomizeGameTable::clickedButtonUpdate(void)
{
emit changedRecord(CurrentRecord);
emit clickedUpdate();
//dlgUpdate->show();
}
void CustomizeGameTable::connectData(void)
{

DataSourceConnected = true;
//emit executeQuery(tmpQuery);
changeData();
qDebug() << "Emitted signal executeQuery";

}

void CustomizeGameTable::processQuery(QSqlQueryModel *parModel)
{
CurrentQueryModel = parModel;
if(CurrentQueryModel == 0) {
qDebug() << "CurrentQueryModel == 0";
} else {
qDebug() << "CurrentQueryModel != 0";
tableViewGameList->setModel(CurrentQueryModel);
CurrentRecord = CurrentQueryModel->record(0);
emit changedRecord(CurrentRecord);
}
}
void CustomizeGameTable::changeData(void)
{
QString tmpQuery = "SELECT ID, Name, realease_date, critic_score, esrb_id FROM Game";
emit executeQuery(tmpQuery);
}


#ifndef DATAOBJECT_H
#define DATAOBJECT_H

#include <QtGui>
#include <QtSql>

class DataObject : public QObject
{
Q_OBJECT
private:
QSqlDatabase dataBase;
QSqlQueryModel *CurrentQueryModel;

public:
explicit DataObject(QObject *parent = 0);
void initConnection(void);

signals:
void isOk(void);
void ececutedQuery(QSqlQueryModel *parModel);
void ececutedUpdate(void);

public slots:
void executeQuery(QString parSQLQuery);
void executeUpdate(QString parSQLQuery);

};

#endif // DATAOBJECT_H


#include "dataobject.h"

DataObject::DataObject(QObject *parent) :
QObject(parent)
{
dataBase = QSqlDatabase::addDatabase( "QODBC", "Atsiskaitymas" );
dataBase.setHostName( "localhost" );
dataBase.setDatabaseName( "Atsiskaitymas" );
dataBase.setUserName( "" );
dataBase.setPassword( "" );

if ( !dataBase.open() ) {
QMessageBox::critical( 0, "DB error", dataBase.lastError ().text () );
CurrentQueryModel = 0;
qDebug() << "DataBase is not open!";
} else {
qDebug() << "DataBase is open";
CurrentQueryModel = new QSqlQueryModel();
emit isOk();
}
}

void DataObject::initConnection(void)
{
if ( !dataBase.open() ) {
QMessageBox::critical( 0, "DB error", dataBase.lastError ().text () );
CurrentQueryModel = 0;
qDebug() << "DataBase is not open!";
} else {
qDebug() << "DataBase is open!";
CurrentQueryModel = new QSqlQueryModel();
emit isOk();
}
}

void DataObject::executeQuery(QString parSQLQuery)
{
CurrentQueryModel->setQuery(parSQLQuery, dataBase);
if(CurrentQueryModel->lastError().isValid()) {
qDebug() << "Klaida:";
qDebug() << CurrentQueryModel->lastError();
} else {
emit ececutedQuery(CurrentQueryModel);
qDebug() << "Stulpeliu ir eiluciu:";
qDebug() << CurrentQueryModel->columnCount();
qDebug() << CurrentQueryModel->rowCount();
qDebug() << "Irasas Nr. 0:";
qDebug() << CurrentQueryModel->record(0).field(0).value();
qDebug() << CurrentQueryModel->record(0).field(1).value();
qDebug() << CurrentQueryModel->record(0).field(2).value();
qDebug() << CurrentQueryModel->record(0).field(3).value();
qDebug() << CurrentQueryModel->record(0).field(4).value();
qDebug() << CurrentQueryModel->record(0).field(5).value();
}
}

void DataObject::executeUpdate(QString parSQLQuery)
{
CurrentQueryModel->setQuery(parSQLQuery, dataBase);
if(CurrentQueryModel->lastError().isValid()) {
qDebug() << "Klaida:";
qDebug() << CurrentQueryModel->lastError();
} else {
emit ececutedUpdate();
}
}


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui>
#include "MainWidget.h"
#include "customizegametable.h"
#include "DataObject.h"

class MainWindow : public QMainWindow
{
Q_OBJECT
private:
void createActions();
void createMenus();
QMenu *newMenu;
QMenu *helpMenu;
QAction *exitAct;
QAction *aboutAct;
MainWidget *mainWidget;
DataObject *dataObject;
CustomizeGameTable *customizeGameTable;


public:
explicit MainWindow(QWidget *parent = 0);

signals:

private slots:
void about();
public slots:

};

#endif // MAINWINDOW_H


#include "mainwindow.h"
#include <QtSql>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
mainWidget = new MainWidget(this);
dataObject = new DataObject(this);
customizeGameTable = new CustomizeGameTable(this);
this->setCentralWidget(mainWidget);

connect(dataObject, SIGNAL(isOk()), customizeGameTable, SLOT(connectData()));
connect(dataObject, SIGNAL(ececutedQuery(QSqlQueryModel*)), customizeGameTable, SLOT(processQuery(QSqlQueryModel*)));
connect(customizeGameTable, SIGNAL(executeQuery(QString)), dataObject, SLOT(executeQuery(QString)));
connect(dataObject, SIGNAL(ececutedUpdate()), customizeGameTable, SLOT(changeData()));
connect(customizeGameTable, SIGNAL(executeUpdate(QString)), dataObject, SLOT(executeUpdate(QString)));


createActions();
createMenus();

setWindowTitle("GUI");
setMinimumSize(200,300);
resize(800,600);
}

void MainWindow::about()
{
QMessageBox::about(this,"About","This is GUI");
}
void MainWindow::createActions()
{
exitAct = new QAction("Exit",this);
exitAct->setShortcuts(QKeySequence::Quit);
// exitAct->setStatusTip("Exit aplication");
connect(exitAct, SIGNAL(triggered()),this,SLOT(close()));

aboutAct = new QAction("About", this);
// aboutAct->setStatusTip("Show info about application ");
connect(aboutAct,SIGNAL(triggered()),this,SLOT(abo ut()));
}
void MainWindow::createMenus()
{
newMenu = menuBar()->addMenu("New");
newMenu->addAction(exitAct);

helpMenu = menuBar()->addMenu("Help");
helpMenu->addAction(aboutAct);
}

SixDegrees
31st May 2011, 22:10
Sorry, I made topic there also but in newbie forum nobody even looked

There is a 9 minute difference between this posting time and the one on the other forum.

Knock it off. You're rapidly wearing out your welcome around here. Spamming the boards is very poor form, and will guarantee that no one looks at your posts.

It wouldn't hurt, either, if you would stop posting hundreds of lines of code. Give a general, detailed description of the problem, or a small sample of code that explicitly demonstrates the problem. No one is interested in reading your entire application.

wysota
31st May 2011, 22:10
Do you expect anyone to analyze almost 300 lines of your code?

I don't know what the problem is but I can see another problem -- that you don't understand what signals and slots are meant for. There is no point in using signals and slots if you perfectly know (and have access to) who is the sole receiver of the signal. Your objects are tightly coupled and using signals to pass entities between those tightly coupled objects doesn't make much sense. I'd start looking for a solution to your problem by simplifying your code, there is a good chance the problem will go away on its own and if not, it will surely be easier to find than currently.