Reputation: 6779
I create sample application in that I was used first two Qwidget from UI form and third widget is custom one. I created one cpp file and header file. there is no issues when build while run the application the first two widget come fine and when i click the to navigate third one, it say's error( login.exe file has stop working) My header file is:
#ifndef LISTWIDGET_H
#define LISTWIDGET_H
#include <QObject>
#include <QWidget>
#include <QtGui>
#include <QPushButton>
class listWidget : public QWidget
{
Q_OBJECT
public:
explicit listWidget(QWidget *parent=0);
~listWidget();
public:
QPushButton *button;
signals:
};
#endif // LISTWIDGET_H
and my cpp file is:
#include "listwidget.h"
#include <QHBoxLayout>
#include <QObject>
#include <QWidget>
#include <QtGui>
listWidget::listWidget(QWidget *parent):QWidget(parent)
{
resize(100,100);
button = new QPushButton("Click here to go back");
QHBoxLayout *hLayout;
hLayout->addWidget(button);
setLayout(hLayout);
}
listWidget::~listWidget()
{
}
Upvotes: 2
Views: 4368
Reputation: 6389
Here is your problem:
QHBoxLayout *hLayout;
hLayout->addWidget(button);
You forgot to either:
instantiate and assign on object for hLayout to point to:
hLayout = new QHBoxLayout();
or instantiate hLayout on the spot:
QHBoxLayout hLayout;
hLayout.addWidget(button);
Basically you are dereferencing an uninitialized pointer and in most cases your application would crash.
Upvotes: 4