Results 1 to 3 of 3

Thread: Problem with DB GUI

  1. #1
    Join Date
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with DB GUI

    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?


    Qt Code:
    1. #ifndef CUSTOMIZEGAMETABLE_H
    2. #define CUSTOMIZEGAMETABLE_H
    3.  
    4. #include <QtGui>
    5. #include <QtSql>
    6. #include "GameUpdateDialog.h"
    7.  
    8. class CustomizeGameTable : public QWidget
    9. {
    10. Q_OBJECT
    11. private:
    12. QSqlQueryModel *CurrentQueryModel;
    13. QSqlRecord CurrentRecord;
    14. bool DataSourceConnected;
    15. QTableView *tableViewGameList;
    16. GameUpdateDialog *dialogUpdate;
    17. QPushButton *btnUpdate;
    18. QGridLayout *grdLayout;
    19. public:
    20. explicit CustomizeGameTable(QWidget *parent = 0);
    21.  
    22. signals:
    23. void executeQuery(QString parSQLQuery);
    24. void executeUpdate(QString parSQLQuery);
    25. void changedRecord(QSqlRecord parRecord);
    26. void clickedUpdate(void);
    27.  
    28. private slots:
    29. void clickedButtonUpdate(void);
    30.  
    31. public slots:
    32. void connectData(void);
    33. void processQuery(QSqlQueryModel *parModel);
    34. void changeData(void);
    35.  
    36. };
    37.  
    38. #endif // CUSTOMIZEGAMETABLE_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "customizegametable.h"
    2.  
    3. CustomizeGameTable::CustomizeGameTable(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. CurrentQueryModel = 0;
    7. //CurrentRecord = 0;
    8. DataSourceConnected = false;
    9. tableViewGameList = new QTableView(this);
    10. dialogUpdate = new GameUpdateDialog(this);
    11. btnUpdate = new QPushButton("Update",this);
    12. grdLayout = new QGridLayout(this);
    13.  
    14. connect(btnUpdate, SIGNAL(clicked()), this, SLOT(clickedButtonUpdate()));
    15. connect(this, SIGNAL(clickedUpdate()), dialogUpdate, SLOT(show()));
    16. connect(this, SIGNAL(changedRecord(QSqlRecord)), dialogUpdate, SLOT(changeRecord(QSqlRecord)));
    17. connect(dialogUpdate, SIGNAL(executeQuery(QString)), this, SIGNAL(executeUpdate(QString)));
    18.  
    19.  
    20. grdLayout->addWidget(tableViewGameList, 0,0,1,2);
    21. grdLayout->addWidget(btnUpdate, 1,1,1,1);
    22. this->setLayout(grdLayout);
    23. }
    24. void CustomizeGameTable::clickedButtonUpdate(void)
    25. {
    26. emit changedRecord(CurrentRecord);
    27. emit clickedUpdate();
    28. //dlgUpdate->show();
    29. }
    30. void CustomizeGameTable::connectData(void)
    31. {
    32.  
    33. DataSourceConnected = true;
    34. //emit executeQuery(tmpQuery);
    35. changeData();
    36. qDebug() << "Emitted signal executeQuery";
    37.  
    38. }
    39.  
    40. void CustomizeGameTable::processQuery(QSqlQueryModel *parModel)
    41. {
    42. CurrentQueryModel = parModel;
    43. if(CurrentQueryModel == 0) {
    44. qDebug() << "CurrentQueryModel == 0";
    45. } else {
    46. qDebug() << "CurrentQueryModel != 0";
    47. tableViewGameList->setModel(CurrentQueryModel);
    48. CurrentRecord = CurrentQueryModel->record(0);
    49. emit changedRecord(CurrentRecord);
    50. }
    51. }
    52. void CustomizeGameTable::changeData(void)
    53. {
    54. QString tmpQuery = "SELECT ID, Name, realease_date, critic_score, esrb_id FROM Game";
    55. emit executeQuery(tmpQuery);
    56. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef DATAOBJECT_H
    2. #define DATAOBJECT_H
    3.  
    4. #include <QtGui>
    5. #include <QtSql>
    6.  
    7. class DataObject : public QObject
    8. {
    9. Q_OBJECT
    10. private:
    11. QSqlDatabase dataBase;
    12. QSqlQueryModel *CurrentQueryModel;
    13.  
    14. public:
    15. explicit DataObject(QObject *parent = 0);
    16. void initConnection(void);
    17.  
    18. signals:
    19. void isOk(void);
    20. void ececutedQuery(QSqlQueryModel *parModel);
    21. void ececutedUpdate(void);
    22.  
    23. public slots:
    24. void executeQuery(QString parSQLQuery);
    25. void executeUpdate(QString parSQLQuery);
    26.  
    27. };
    28.  
    29. #endif // DATAOBJECT_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "dataobject.h"
    2.  
    3. DataObject::DataObject(QObject *parent) :
    4. QObject(parent)
    5. {
    6. dataBase = QSqlDatabase::addDatabase( "QODBC", "Atsiskaitymas" );
    7. dataBase.setHostName( "localhost" );
    8. dataBase.setDatabaseName( "Atsiskaitymas" );
    9. dataBase.setUserName( "" );
    10. dataBase.setPassword( "" );
    11.  
    12. if ( !dataBase.open() ) {
    13. QMessageBox::critical( 0, "DB error", dataBase.lastError ().text () );
    14. CurrentQueryModel = 0;
    15. qDebug() << "DataBase is not open!";
    16. } else {
    17. qDebug() << "DataBase is open";
    18. CurrentQueryModel = new QSqlQueryModel();
    19. emit isOk();
    20. }
    21. }
    22.  
    23. void DataObject::initConnection(void)
    24. {
    25. if ( !dataBase.open() ) {
    26. QMessageBox::critical( 0, "DB error", dataBase.lastError ().text () );
    27. CurrentQueryModel = 0;
    28. qDebug() << "DataBase is not open!";
    29. } else {
    30. qDebug() << "DataBase is open!";
    31. CurrentQueryModel = new QSqlQueryModel();
    32. emit isOk();
    33. }
    34. }
    35.  
    36. void DataObject::executeQuery(QString parSQLQuery)
    37. {
    38. CurrentQueryModel->setQuery(parSQLQuery, dataBase);
    39. if(CurrentQueryModel->lastError().isValid()) {
    40. qDebug() << "Klaida:";
    41. qDebug() << CurrentQueryModel->lastError();
    42. } else {
    43. emit ececutedQuery(CurrentQueryModel);
    44. qDebug() << "Stulpeliu ir eiluciu:";
    45. qDebug() << CurrentQueryModel->columnCount();
    46. qDebug() << CurrentQueryModel->rowCount();
    47. qDebug() << "Irasas Nr. 0:";
    48. qDebug() << CurrentQueryModel->record(0).field(0).value();
    49. qDebug() << CurrentQueryModel->record(0).field(1).value();
    50. qDebug() << CurrentQueryModel->record(0).field(2).value();
    51. qDebug() << CurrentQueryModel->record(0).field(3).value();
    52. qDebug() << CurrentQueryModel->record(0).field(4).value();
    53. qDebug() << CurrentQueryModel->record(0).field(5).value();
    54. }
    55. }
    56.  
    57. void DataObject::executeUpdate(QString parSQLQuery)
    58. {
    59. CurrentQueryModel->setQuery(parSQLQuery, dataBase);
    60. if(CurrentQueryModel->lastError().isValid()) {
    61. qDebug() << "Klaida:";
    62. qDebug() << CurrentQueryModel->lastError();
    63. } else {
    64. emit ececutedUpdate();
    65. }
    66. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui>
    5. #include "MainWidget.h"
    6. #include "customizegametable.h"
    7. #include "DataObject.h"
    8.  
    9. class MainWindow : public QMainWindow
    10. {
    11. Q_OBJECT
    12. private:
    13. void createActions();
    14. void createMenus();
    15. QMenu *newMenu;
    16. QMenu *helpMenu;
    17. QAction *exitAct;
    18. QAction *aboutAct;
    19. MainWidget *mainWidget;
    20. DataObject *dataObject;
    21. CustomizeGameTable *customizeGameTable;
    22.  
    23.  
    24. public:
    25. explicit MainWindow(QWidget *parent = 0);
    26.  
    27. signals:
    28.  
    29. private slots:
    30. void about();
    31. public slots:
    32.  
    33. };
    34.  
    35. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QtSql>
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent)
    6. {
    7. mainWidget = new MainWidget(this);
    8. dataObject = new DataObject(this);
    9. customizeGameTable = new CustomizeGameTable(this);
    10. this->setCentralWidget(mainWidget);
    11.  
    12. connect(dataObject, SIGNAL(isOk()), customizeGameTable, SLOT(connectData()));
    13. connect(dataObject, SIGNAL(ececutedQuery(QSqlQueryModel*)), customizeGameTable, SLOT(processQuery(QSqlQueryModel*)));
    14. connect(customizeGameTable, SIGNAL(executeQuery(QString)), dataObject, SLOT(executeQuery(QString)));
    15. connect(dataObject, SIGNAL(ececutedUpdate()), customizeGameTable, SLOT(changeData()));
    16. connect(customizeGameTable, SIGNAL(executeUpdate(QString)), dataObject, SLOT(executeUpdate(QString)));
    17.  
    18.  
    19. createActions();
    20. createMenus();
    21.  
    22. setWindowTitle("GUI");
    23. setMinimumSize(200,300);
    24. resize(800,600);
    25. }
    26.  
    27. void MainWindow::about()
    28. {
    29. QMessageBox::about(this,"About","This is GUI");
    30. }
    31. void MainWindow::createActions()
    32. {
    33. exitAct = new QAction("Exit",this);
    34. exitAct->setShortcuts(QKeySequence::Quit);
    35. // exitAct->setStatusTip("Exit aplication");
    36. connect(exitAct, SIGNAL(triggered()),this,SLOT(close()));
    37.  
    38. aboutAct = new QAction("About", this);
    39. // aboutAct->setStatusTip("Show info about application ");
    40. connect(aboutAct,SIGNAL(triggered()),this,SLOT(about()));
    41. }
    42. void MainWindow::createMenus()
    43. {
    44. newMenu = menuBar()->addMenu("New");
    45. newMenu->addAction(exitAct);
    46.  
    47. helpMenu = menuBar()->addMenu("Help");
    48. helpMenu->addAction(aboutAct);
    49. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem with DB GUI

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with DB GUI

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.