Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Clearing QLineEdit leads to segmentation fault and crash

Clearing QLineEdit leads to segmentation fault and crash

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 873 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shoaib Muhammad
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chris_rookie
      wrote on last edited by
      #2

      I thought your class CGui should extends QMainWindow

      S 1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        QLineEdit *StrtEdit are local variable. Looks like you have same member variables. replace QLineEdit *StrtEdit with *StrtEdit. it should work.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        S 1 Reply Last reply
        2
        • C chris_rookie

          I thought your class CGui should extends QMainWindow

          S Offline
          S Offline
          Shoaib Muhammad
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • dheerendraD dheerendra

            QLineEdit *StrtEdit are local variable. Looks like you have same member variables. replace QLineEdit *StrtEdit with *StrtEdit. it should work.

            S Offline
            S Offline
            Shoaib Muhammad
            wrote on last edited by
            #5

            @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

            jsulmJ 1 Reply Last reply
            0
            • S Shoaib Muhammad

              @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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #6

              @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.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply
              7
              • jsulmJ jsulm

                @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.

                S Offline
                S Offline
                Shoaib Muhammad
                wrote on last edited by
                #7

                @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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved