开发者

create custom Qwidget in Qt?

开发者 https://www.devze.com 2023-02-09 00:51 出处:网络
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 applic

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 wo开发者_运维知识库rking) 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()
{

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消