PDA

View Full Version : how to solve dialog creation crash.



rahulvishwakarma
2nd January 2018, 15:49
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.



#include "myappmainwindow.h"
#include "ui_myappmainwindow.h"

MyAppMainWindow::MyAppMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyAppMainWindow)
{
ui->setupUi(this);

db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));
db->setHostName("oracle");
db->setDatabaseName("test");
//db->setDatabaseName("music");
db->setUserName("rahul");
db->setPassword("rahul");
db->setPort(3306);

query = new QSqlQuery(*db);
model = new QSqlQueryModel(this);
//mydlg = new MyDialog(this);
}

MyAppMainWindow::~MyAppMainWindow()
{
db->close();
delete ui;
}

void MyAppMainWindow::on_pushButtonLogin_clicked()
{

QString str;

QString su = ui->lineEditUser->text().trimmed();
QString sp = ui->lineEditPassword->text().trimmed();

str = "select *from tablelogin where user = \"" + su + "\" and password = \"" + sp +"\";";

if(db->open() )
{
query = new QSqlQuery(str, *db);

if(query->exec())
{
int count = 0;
while(query->next())
count++;

if(count == 1)
{

QMessageBox::information(this, "sqlqueymodel", "user name and password is correct");
model->setQuery(*query);
ui->tableViewLogin->setModel(model);
QMessageBox::information(this, "sqlqueymodel", "ui->tableViewLogin->setModel(model);");
mydlg = new MyDialog(this);//here program crashes
QMessageBox::information(this, "sqlqueymodel", " mydlg = new MyDialog(this);");
mydlg->setModal(true);
mydlg->exec();

}
else if( count > 1 )
QMessageBox::information(this, "sqlqueymodel", "duplicate username ... access denied ");
else
QMessageBox::critical(this, "sqlqueymodel", "user name or password is not correct");
}
}
else
{
QMessageBox::critical(this, "db->open", db->lastError().text().trimmed());
}

db->close();
}

void MyAppMainWindow::on_pushButtonSecondForm_clicked()
{
this->hide();

mydlg = new MyDialog(this);

mydlg->setModal(true);

mydlg->exec();
}

QSqlDatabase * MyAppMainWindow::open_db(QSqlDatabase *db)
{
return (db = this->db);
}


myappaminwindow.h
-------------------


#include <QtDebug>
#include <QSqlDriver>
#include <QSqlError>
#include <QSqlQueryModel>

#include <QMessageBox>

#include "mydialog.h"

namespace Ui {
class MyAppMainWindow;
}

//class MyDialog;

class MyAppMainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MyAppMainWindow(QWidget *parent = 0);
~MyAppMainWindow();

private slots:
void on_pushButtonLogin_clicked();

void on_pushButtonSecondForm_clicked();

public slots:

QSqlDatabase *open_db(QSqlDatabase *db);

private:
Ui::MyAppMainWindow *ui;
QSqlDatabase *db;
QSqlQuery *query;

QSqlQueryModel *model;

MyDialog *mydlg;


};

#endif // MYAPPMAINWINDOW_H

mydialog.h


#ifndef MYDIALOG_H
#define MYDIALOG_H

#include <QDialog>
//#include "myappmainwindow.h"
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QtSql>

namespace Ui {
class MyDialog;
}

class MyAppMainWindow;

class MyDialog : public QDialog
{
Q_OBJECT

public:
explicit MyDialog(QWidget *parent = 0);
~MyDialog();

void fillcombo();

signals:

void open_db(QSqlDatabase *dbdlg);

private slots:
void on_pushButton_clicked();

void on_comboBoxEmployee_currentTextChanged(const QString &arg1);

private:
Ui::MyDialog *ui;
MyAppMainWindow *mywindow;
QSqlQuery *query;
QSqlDatabase *dbdlg;

};

#endif // MYDIALOG_H


mydialog.cpp



#include "mydialog.h"
#include "ui_mydialog.h"

#include "myappmainwindow.h"

MyDialog::MyDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::MyDialog)
{
ui->setupUi(this);
mywindow = new MyAppMainWindow(this);

//mywindow->db->open();
connect(this, SIGNAL(open_db(QSqlDatabase *)), mywindow, SLOT(open_db(QSqlDatabase *)) );

emit open_db(dbdlg);

this->query = new QSqlQuery(*dbdlg);
fillcombo();
}

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

void MyDialog::on_pushButton_clicked()
{

QString strname = ui->lineEditUserName->text().trimmed();

ui->comboBoxEmployee->addItem(strname);

}

void MyDialog::on_comboBoxEmployee_currentTextChanged(c onst QString &arg1)
{
//ui->labelUserNameCombo->setText(ui->comboBoxEmployee->currentText().trimmed());
ui->lineEditUserName->setText(arg1);
}

void MyDialog::fillcombo()
{
//if(mywindow->db->open())
if(dbdlg->open())
{
QString str = "select user\ name from tableEmployeeData;";
this->query = new QSqlQuery(str, *dbdlg);
if(this->query->exec())
while(this->query->next())
ui->comboBoxEmployee->addItem(this->query->value(1).toString());

}
}




#include "myappmainwindow.h"
#include <QApplication>

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

return a.exec();
}


how to solve this problem.

d_stranz
2nd January 2018, 18:23
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?