PDA

View Full Version : Error when building example from C++ GUI Progamming with Qt4



Rabbitl96l
28th March 2015, 14:28
Hi everyone, I have just downloaded QT 5.4.1 for a couple of days. Because there is no book for Qt 5, I use the book named "C++ GUI Programming with Qt 4". I'm learning about QDialog and Qt Designer, and I copied the source code from that book to run on my Qt. However, it didn't work but announced "The inferior stopped because it triggered an exception.". I don't know why. Here are he source code:
The header:

#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H

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

namespace Ui
{
class GoToCellDialog;
}

class GoToCellDialog : public QDialog, public Ui::GoToCellDialog
{
Q_OBJECT
public:
explicit GoToCellDialog(QWidget *parent = 0);
~GoToCellDialog();
private slots:
void on_lineEdit_textChanged();
private:
Ui::GoToCellDialog *ui;
};

#endif // GOTOCELLDIALOG_H

The implementation of the class:

#include "gotocelldialog.h"

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

QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
lineEdit->setValidator(new QRegExpValidator(regExp, this));

connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
}

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

void GoToCellDialog::on_lineEdit_textChanged()
{
okButton->setEnabled(lineEdit->hasAcceptableInput());
}

The main source code:

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

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

return a.exec();
}

There's also a .ui file that contains a label, lineEdit, okButton and cancelButton. Please help me solve this error. Thanks in advance.

anda_skoa
28th March 2015, 15:40
First, you don't need to inherit from Ui::GoToCellDialog.
Second, all widgets of the designer form are addressed through the ui pointter


connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(reject()));


You were accessing the inherited member cancelButton which was not initialized

Cheers,
_