Clearing QLineEdit leads to segmentation fault and crash
-
Hi All,
This may be an easy solution that I just don't understand so please bear with me as I am a complete newbie. I have a month to create a GUI and I learn things on the go.
So to start, I wanted to clear my input fields (named StrtEdit and StopEdit) upon pressing the PrcBtn QPushButton. However, the App crashes when I do it.
Here is my header.h:
#pragma once #include <QWidget> #include <QPainter> #include <QApplication> #include <QPushButton> #include <QLabel> #include <QIntValidator> #include <Qt> #include <QtDebug> #include <QLineEdit> #include <QString> #include <QFont> #include <QGraphicsScene> class CGui : public QWidget { Q_OBJECT public: CGui(QWidget *parent = 0); QLineEdit *StrtEdit; QLineEdit *StopEdit; private slots: void clickProcess(); void clickDatacap(); private: QLabel *rltxt; QLabel *imtxt; QLabel *strttxt; QLabel *stoptxt; protected: void paintEvent(QPaintEvent *e); void drawRectangles(); void drawGraphs(); };
And here is header.cpp
#include "mainheader.h" CGui::CGui(QWidget *parent) : QWidget(parent) { QPushButton *PrcBtn = new QPushButton("PROCESS", this); PrcBtn->setFont(font1); PrcBtn->setStyleSheet("background-color:rgb(65,182,230); border:none; color:white;"); PrcBtn->setGeometry(405, 195, 193, 60); QLineEdit *StrtEdit = new QLineEdit("",this); StrtEdit->setFont(font2); StrtEdit->setAlignment(Qt::AlignLeft); StrtEdit->setGeometry(555, 140, 240, 50); QLineEdit *StopEdit = new QLineEdit("",this); StopEdit->setAlignment(Qt::AlignLeft); StopEdit->setFont(font2); StopEdit->setGeometry(555, 85, 240, 50); connect(PrcBtn, &QPushButton::clicked, this, &CGui::clickProcess); } void CGui::clickProcess(){ qDebug() << "Process Button Clicked"; this->StrtEdit->setText(""); this->StopEdit->setText(""); }
finally, here is main.cpp
#include "mainheader.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); CGui window; window.resize(800, 445); window.setWindowTitle("Complex Impedance Analyzer"); window.show(); return app.exec(); }
I have omitted the parts of the code that are unrelated and that is why some objects in the header are unused.
What could be the problem?
Kind regards,
Shoaib -
I thought your class CGui should extends QMainWindow
-
QLineEdit *StrtEdit are local variable. Looks like you have same member variables. replace QLineEdit *StrtEdit with *StrtEdit. it should work.
-
@chris_rookie said in Clearing QLineEdit leads to segmentation fault and crash:
I thought your class CGui should extends QMainWindow
I'm not sure I follow. I don't know CGui extends QMainWindow.
By the way I have been following tutorials here since I am using qt version 5.6.1 with an AM4379 Development Kit on a Ubuntu Linux 14.04 Operating System.
-
@dheerendra said in Clearing QLineEdit leads to segmentation fault and crash:
QLineEdit *StrtEdit are local variable. Looks like you have same member variables. replace QLineEdit *StrtEdit with *StrtEdit. it should work.
Hi dheerendra,
You are absolutely right. To be precise I changed it to StrtEdit (without asterisk) in my header.cpp file. I would love to learn more about this. Will you be able to give a brief explanation or link explaining what member variables and local variables are? Thank you very much.Kind regards,
Shoaib -
@Shoaib-Muhammad Member variables are variables defined in a class, like:
class A { private: int aVar; // aVar is a member of class A };
Local variables are variables defined in a local scope. Local scope can be inside function or method for example:
class A { private: int aVar; // aVar is a member of class A void someMethod() { int aVar = 0; // This is a local variable which only exists inside someMethod and is NOT the same as aVar above! // aVar = 0; //If you do it like this then you access the member variable } };
The problem with your code is exactly this: you define local variables with exactly same names as your member variables.
-
@jsulm said in Clearing QLineEdit leads to segmentation fault and crash:
@Shoaib-Muhammad Member variables are variables defined in a class, like:
class A { private: int aVar; // aVar is a member of class A };
Local variables are variables defined in a local scope. Local scope can be inside function or method for example:
class A { private: int aVar; // aVar is a member of class A void someMethod() { int aVar = 0; // This is a local variable which only exists inside someMethod and is NOT the same as aVar above! // aVar = 0; //If you do it like this then you access the member variable } };
The problem with your code is exactly this: you define local variables with exactly same names as your member variables.
Thank you for the concise and clear explanation. It does make sense now that you mention it.