Important: Please read the Qt Code of Conduct -

Segmentation fault(core dumped) occured after add an private member in widget class.

  • I am building a tool with QT 5,12 to run in the arm64 board. It run well when there was only one private member. When I add more private members, it can compile successfully, but run to the segmentation fault.

    namespace Ui { class Widget; }
    class Widget : public QWidget
        Widget(QWidget *parent = nullptr);
        QLCDNumber    *rebootTimeLcdNum;
        QTimer *mtimer;
        Ui::Widget *ui;
        QPushButton *startButton;
        //QLCDNumber    *rebootTimeLcdNum;
        //QTimer *mtimer;
        //int a;
    private slots:
        void on_startButton_clicked();
        void onTimerTimeout();
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
        connect(startButton, SIGNAL(clicked()), this, SLOT(on_startButton_clicked()));

    I use gdb to debug and find that the fault occurred when connecting the slot function on_startButton_clicked() . And the fault won't occur if I put the member to public.

    Does anyone experencd the same problem?

  • Lifetime Qt Champion

    @Hupe You did not assign a valid pointer to startButton

  • Thanks! Is there any advice on how to assign a valid pointer or some textbooks to refer to?

  • @Hupe
    You don't need a textbook to refer to, you need to assign your startButton to whatever button you have which you are wanting to attach the signal/slot to!

  • Lifetime Qt Champion

    @Hupe This belongs to absolute basics of C++. Do you know how memory is allocated in C++ using "new"?

  • I found setupUi(QWidget *Widget) in ui_widget.h has already new the button. And it works well if there is only one private member *startButton.

  • @Hupe
    You need to attach the signal to the button defined in the Designer. But it sounds/looks like you are declaring some new QPushButton *startButton; in your Widget class, which is not the one from Designer? Why are you doing that?

  • Thanks! I found the problem.

Log in to reply