Search code examples
qtqt4signalssignals-slotsslot

Seg Fault After Setting Up New Public Signal in QT


I am just getting my feet wet with Qt, I am trying to pull the string from a QlineEdit and append it to a QTextBrowser after clicking a button(for simplicity/error checking I am just having it append the word appended at the moment).

The program runs, and the GUI gets brought up on the screen, but whenever I click the button, my program seg faults.

Here's my code, I cut a lot out that was unnecessary:

HEADER:

#ifndef TCD2_GUI_H
#define TCD2_GUI_H
//bunch of includes

class TCD2_GUI : public QWidget
{
    Q_OBJECT

public:
     TCD2_GUI(QWidget *window = 0);
     //bunch of other members
     QLineEdit *a1_1;
     QTextBrowser *stdoutput;

public slots:
     void applySettings(void);

private:

};
#endif // TCD2_GUI_H

and here is the snippet of the cpp of which causes the fault

 QTextBrowser *stdoutput = new QTextBrowser();

    stdoutput->append("Welcome!");

    QObject::connect(apply, SIGNAL(clicked()), this, SLOT(applySettings()));

    //------------------------------------------------------Standard Output END
    //layout things

}

void TCD2_GUI::applySettings()
{
    stdoutput->append("appended");
}

Solution

  • stdoutput in your applySettings() function refer to the member of the TCD2_GUI class whereas stdoutput in your piece of code where the crash happens is a local variable. Try to add in your constructor by example:

    stdoutput = new QTextBrowser();
    

    andremove the following line from your piece of code:

    QTextBrowser stdoutput = new QTextBrowser();