0

I created two dialogs "listdialog.ui" and "editdialog.ui" using QtDesigner, shipped with Qt5.3.1, and then added to project "phone book.pro" "with source code" using wizard. Everytime I start my compiled phone book.exe with all needed dll’s, when i try to start the program in the QtCreator (or individually), There comes an Runtime Error with some problem details and there it says:

Problem Event Name: APPCRASH
Fault Module Name: Qt5Cored.dll

other programms which uses GUI are fine working(earlier complied .exe files). Also when debugging(in win7) ( and running in centos6.4 ) it displays a message box :-

The inferior stopped because it recieved a signal from the operating system.

signal name : SIGSEGV
signal meaning : segmentation fault

files codes are :-
file - 1 : Editdialog.cpp :-

#include <QDialog>
#include "ui_EditDialog.h"


class EditDialog : public QDialog, public Ui::EditDialog
{
public :
EditDialog (QWidget *parent=0 );

const QString name() const;
void setName( const QString& );

const QString number() const;
void setNumber( const QString& );

private :

EditDialog *ui;
};

file - 2 : listdialog.h

#ifndef LISTDIALOG_H
#define LISTDIALOG_H

#include <QDialog>
#include "ui_ListDialog.h"

class ListDialog : public QDialog, public Ui::ListDialog
{
Q_OBJECT

public :

//ListDialog(QObject *parent = 0);
ListDialog(QWidget *parent = 0);
//I don't know when to write "QWidget *parent=0" and when "QObject *parent=0)

private slots :
    void addItem();
    void editItem();
    void deleteItem();

private :

    ListDialog *ui;
};

#endif //LISTDIALOG_H

file - 3 : ListDialog.cpp

#include "ListDialog.h"
#include "EditDialog.h"

ListDialog::ListDialog(QWidget *parent) :  QDialog(parent), ui(new ListDialog)
{
 ui->setupUi(this);

connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addItem()) );
connect(ui->editButton, SIGNAL(clicked()), this, SLOT(editItem()) );
connect(ui->deleteButton, SIGNAL(clicked()), this, SLOT(deleteItem()) );
}

void ListDialog::addItem()
{
//EditDialog *dlg = EditDialog(this);

EditDialog dlg(this);

if(QDialog::Accepted == dlg.exec())
   ui->listDialogWidget->addItem(dlg.name() + " -- " + dlg.number());
}

void ListDialog::deleteItem()
{
delete ui->listDialogWidget->currentItem();
}

void ListDialog::editItem()
{
 if(!ui->listDialogWidget->currentItem())
    return;

QStringList parts = ui->listDialogWidget->currentItem()->text().split("--");

EditDialog dlg(this);
dlg.setName(parts[0].trimmed());
dlg.setNumber(parts[1].trimmed());

if(dlg.exec() == QDialog::Accepted)
    ui->listDialogWidget->currentItem()->setText(dlg.name() + " -- " + dlg.number());
}

file - 4 : EditDialog.cpp

#include "EditDialog.h"
EditDialog :: EditDialog(QWidget *parent) : QDialog(parent)
{
ui->setupUi(this);
}

const QString EditDialog::name() const
{
return ui->nameLineEdit->text().replace("--","").trimmed();
}
void EditDialog::setName(const QString &name)
{
ui->nameLineEdit->setText(name);
}

const QString EditDialog::number() const
{
return ui->numberLineEdit->text().replace("--","").trimmed();
}

void EditDialog::setNumber(const QString &number)
{
ui->numberLineEdit->setText(number);
}

Along with them project has :- "ui_editdialog.h" and "ui_listdialog.h" generated by QtDesigner.

file - 5 : main.cpp

#include "EditDialog.h"
#include "ListDialog.h"
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

//QObject parent;

QWidget parent;

/*
ListDialog *listdlg = new ListDialog;
listdlg->show();
*/
ListDialog dlg(&parent);

dlg.show();

return a.exec();
}

please any one tell me - how to overcome this problem. I searched in some sites that this is a problem of pointer which is set to null or memory accessing outside of its domain. I think it may be *parent=0 set by constructor ( file - 2 : listdialog.h - ListDialog(QWidget *parent = 0); ) but how to overcome, I don't know.

rahul
  • 67
  • 4
  • 11
  • What does your debugger say? – cmannett85 Sep 27 '14 at 13:56
  • @rahul in `ListDialog()` you initialize your `ui` datamember but in `EditDialog()` you don't. Additionally you are both inheriting from and trying to compose your UI classes, this doesn't really make sense. You should try to understand why things are done and not just do them because you saw it somewhere. – PeterT Sep 27 '14 at 14:54
  • @rahul Oh and `ListDialog::ListDialog(QWidget *parent) : QDialog(parent), ui(new ListDialog)` is infinitely recursive. Every time you create a ListDialog, you create a new ListDialog, which creates a new ListDialog upon creation, etc., etc.. I'm pretty sure you mean to make it `Ui::ListDialog *ui;` and simply `class ListDialog : public QDialog` instead of `class ListDialog : public QDialog, Ui::ListDialog` – PeterT Sep 27 '14 at 15:09
  • @rahul read both answers to this question: http://stackoverflow.com/questions/9696317/how-to-use-ui-file-for-making-a-simple-widget . You can do either approach but you shouldn't try to do both like you did. – PeterT Sep 27 '14 at 15:29

1 Answers1

0

@PeterT Thank you veryy much for giving me a way. I changed code of constructor as you said to "ListDialog::ListDialog(QWidget *parent) : QDialog(parent), ui(new Ui::ListDialog)" and "EditDialog :: EditDialog(QWidget *parent) : QDialog(parent),ui(new Ui::EditDialog)" program is now fine working. Thanx a LOT.

rahul
  • 67
  • 4
  • 11