Results 1 to 2 of 2

Thread: how to solve dialog creation crash.

  1. #1
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default how to solve dialog creation crash.

    i've centos 6.6 as server and client in vm ware. one has mysql server and other has client mysql. i've two dialogs in client. one is "MyAppMainWindow" and other one is "MyDialog". problenm is that when i want to create MyDialog's object in MyAppMainWindow implementation file "myappmainwindow.cpp" programm crashes.

    here is myappmainwindow.cpp
    messageboxes as only to track error.

    Qt Code:
    1. #include "myappmainwindow.h"
    2. #include "ui_myappmainwindow.h"
    3.  
    4. MyAppMainWindow::MyAppMainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MyAppMainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));
    11. db->setHostName("oracle");
    12. db->setDatabaseName("test");
    13. //db->setDatabaseName("music");
    14. db->setUserName("rahul");
    15. db->setPassword("rahul");
    16. db->setPort(3306);
    17.  
    18. query = new QSqlQuery(*db);
    19. model = new QSqlQueryModel(this);
    20. //mydlg = new MyDialog(this);
    21. }
    22.  
    23. MyAppMainWindow::~MyAppMainWindow()
    24. {
    25. db->close();
    26. delete ui;
    27. }
    28.  
    29. void MyAppMainWindow::on_pushButtonLogin_clicked()
    30. {
    31.  
    32. QString str;
    33.  
    34. QString su = ui->lineEditUser->text().trimmed();
    35. QString sp = ui->lineEditPassword->text().trimmed();
    36.  
    37. str = "select *from tablelogin where user = \"" + su + "\" and password = \"" + sp +"\";";
    38.  
    39. if(db->open() )
    40. {
    41. query = new QSqlQuery(str, *db);
    42.  
    43. if(query->exec())
    44. {
    45. int count = 0;
    46. while(query->next())
    47. count++;
    48.  
    49. if(count == 1)
    50. {
    51.  
    52. QMessageBox::information(this, "sqlqueymodel", "user name and password is correct");
    53. model->setQuery(*query);
    54. ui->tableViewLogin->setModel(model);
    55. QMessageBox::information(this, "sqlqueymodel", "ui->tableViewLogin->setModel(model);");
    56. mydlg = new MyDialog(this);//here program crashes
    57. QMessageBox::information(this, "sqlqueymodel", " mydlg = new MyDialog(this);");
    58. mydlg->setModal(true);
    59. mydlg->exec();
    60.  
    61. }
    62. else if( count > 1 )
    63. QMessageBox::information(this, "sqlqueymodel", "duplicate username ... access denied ");
    64. else
    65. QMessageBox::critical(this, "sqlqueymodel", "user name or password is not correct");
    66. }
    67. }
    68. else
    69. {
    70. QMessageBox::critical(this, "db->open", db->lastError().text().trimmed());
    71. }
    72.  
    73. db->close();
    74. }
    75.  
    76. void MyAppMainWindow::on_pushButtonSecondForm_clicked()
    77. {
    78. this->hide();
    79.  
    80. mydlg = new MyDialog(this);
    81.  
    82. mydlg->setModal(true);
    83.  
    84. mydlg->exec();
    85. }
    86.  
    87. QSqlDatabase * MyAppMainWindow::open_db(QSqlDatabase *db)
    88. {
    89. return (db = this->db);
    90. }
    To copy to clipboard, switch view to plain text mode 

    myappaminwindow.h
    -------------------
    Qt Code:
    1. #include <QtDebug>
    2. #include <QSqlDriver>
    3. #include <QSqlError>
    4. #include <QSqlQueryModel>
    5.  
    6. #include <QMessageBox>
    7.  
    8. #include "mydialog.h"
    9.  
    10. namespace Ui {
    11. class MyAppMainWindow;
    12. }
    13.  
    14. //class MyDialog;
    15.  
    16. class MyAppMainWindow : public QMainWindow
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. explicit MyAppMainWindow(QWidget *parent = 0);
    22. ~MyAppMainWindow();
    23.  
    24. private slots:
    25. void on_pushButtonLogin_clicked();
    26.  
    27. void on_pushButtonSecondForm_clicked();
    28.  
    29. public slots:
    30.  
    31. QSqlDatabase *open_db(QSqlDatabase *db);
    32.  
    33. private:
    34. Ui::MyAppMainWindow *ui;
    35. QSqlQuery *query;
    36.  
    37.  
    38. MyDialog *mydlg;
    39.  
    40.  
    41. };
    42.  
    43. #endif // MYAPPMAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mydialog.h
    Qt Code:
    1. #ifndef MYDIALOG_H
    2. #define MYDIALOG_H
    3.  
    4. #include <QDialog>
    5. //#include "myappmainwindow.h"
    6. #include <QSqlQuery>
    7. #include <QSqlDatabase>
    8. #include <QtSql>
    9.  
    10. namespace Ui {
    11. class MyDialog;
    12. }
    13.  
    14. class MyAppMainWindow;
    15.  
    16. class MyDialog : public QDialog
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. explicit MyDialog(QWidget *parent = 0);
    22. ~MyDialog();
    23.  
    24. void fillcombo();
    25.  
    26. signals:
    27.  
    28. void open_db(QSqlDatabase *dbdlg);
    29.  
    30. private slots:
    31. void on_pushButton_clicked();
    32.  
    33. void on_comboBoxEmployee_currentTextChanged(const QString &arg1);
    34.  
    35. private:
    36. Ui::MyDialog *ui;
    37. MyAppMainWindow *mywindow;
    38. QSqlQuery *query;
    39. QSqlDatabase *dbdlg;
    40.  
    41. };
    42.  
    43. #endif // MYDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    mydialog.cpp

    Qt Code:
    1. #include "mydialog.h"
    2. #include "ui_mydialog.h"
    3.  
    4. #include "myappmainwindow.h"
    5.  
    6. MyDialog::MyDialog(QWidget *parent) :
    7. QDialog(parent),
    8. ui(new Ui::MyDialog)
    9. {
    10. ui->setupUi(this);
    11. mywindow = new MyAppMainWindow(this);
    12.  
    13. //mywindow->db->open();
    14. connect(this, SIGNAL(open_db(QSqlDatabase *)), mywindow, SLOT(open_db(QSqlDatabase *)) );
    15.  
    16. emit open_db(dbdlg);
    17.  
    18. this->query = new QSqlQuery(*dbdlg);
    19. fillcombo();
    20. }
    21.  
    22. MyDialog::~MyDialog()
    23. {
    24. delete ui;
    25. }
    26.  
    27. void MyDialog::on_pushButton_clicked()
    28. {
    29.  
    30. QString strname = ui->lineEditUserName->text().trimmed();
    31.  
    32. ui->comboBoxEmployee->addItem(strname);
    33.  
    34. }
    35.  
    36. void MyDialog::on_comboBoxEmployee_currentTextChanged(const QString &arg1)
    37. {
    38. //ui->labelUserNameCombo->setText(ui->comboBoxEmployee->currentText().trimmed());
    39. ui->lineEditUserName->setText(arg1);
    40. }
    41.  
    42. void MyDialog::fillcombo()
    43. {
    44. //if(mywindow->db->open())
    45. if(dbdlg->open())
    46. {
    47. QString str = "select user\ name from tableEmployeeData;";
    48. this->query = new QSqlQuery(str, *dbdlg);
    49. if(this->query->exec())
    50. while(this->query->next())
    51. ui->comboBoxEmployee->addItem(this->query->value(1).toString());
    52.  
    53. }
    54. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "myappmainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MyAppMainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    how to solve this problem.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to solve dialog creation crash.

    You are creating new instances of MyDialog and MyAppMainWindow on every button click, none of which has any relationship to the MyAppMainWindow you presumably create in main(). So you get this infinite series of alternating MyDialog and MyAppMainWindow instances, and because you keep hiding them with each button press, who knows which of these instances is actually the one responding to the interactions?

    Why don't you take a huge step back, think about what your app really should be doing, and then study some of the Qt example programs that use main windows and dialogs to see how they do it?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. folder creation using the dialog box .
    By riarioriu3 in forum Qt Programming
    Replies: 4
    Last Post: 14th June 2012, 15:17
  2. Replies: 7
    Last Post: 11th June 2012, 11:50
  3. New dialog/windows creation hook
    By gilamran in forum Qt Programming
    Replies: 9
    Last Post: 29th March 2011, 00:01
  4. Replies: 2
    Last Post: 19th September 2009, 06:18
  5. Crash during OpenGL context creation
    By PaladinOfKaos in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2009, 07:39

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.