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. Program compiles but generates a segmentation fault.
Forum Updated to NodeBB v4.3 + New Features

Program compiles but generates a segmentation fault.

Scheduled Pinned Locked Moved Solved General and Desktop
c++ qt5 gui
16 Posts 7 Posters 2.9k Views 3 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.
  • F Offline
    F Offline
    fcarney
    wrote on 2 Jul 2019, 17:49 last edited by
    #3

    @zlinux20 said in Program compiles but generates a segmentation fault.:

    public:
    Mlabelwidget(QWidget *parent = 0);
    QLabel *label;
    void changeText(QString input);

    A coding style suggestion:

    QLabel *m_label;
    

    Naming all member variables with an m_ prefix tells you that you are using a member variable. This helps distinguish between temporary/stack variables and the member variables in your class. Its a nice mental reminder.

    C++ is a perfectly valid school of magic.

    K 1 Reply Last reply 2 Jul 2019, 20:56
    4
    • F fcarney
      2 Jul 2019, 17:49

      @zlinux20 said in Program compiles but generates a segmentation fault.:

      public:
      Mlabelwidget(QWidget *parent = 0);
      QLabel *label;
      void changeText(QString input);

      A coding style suggestion:

      QLabel *m_label;
      

      Naming all member variables with an m_ prefix tells you that you are using a member variable. This helps distinguish between temporary/stack variables and the member variables in your class. Its a nice mental reminder.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 2 Jul 2019, 20:56 last edited by kshegunov 7 Feb 2019, 20:57
      #4

      @fcarney said in Program compiles but generates a segmentation fault.:

      Naming all member variables with an m_ prefix tells you that you are using a member variable. This helps distinguish between temporary/stack variables and the member variables in your class.

      Although Qt uses that style I don't agree. Firstly, hungarian notation is very ugly, and verbose, and was never proven to be anything but actual noise. I'd rather see descriptive (possibly long) names that speak to me than to struggle with half-standard type/semantic prefixes.
      And secondly, a good IDE is going to draw you a picture (quite literally using different colors) for the different types of variables. Default color scheme for creator makes members orange-brown, while locals are dark blue and globals are black. Sorry, but no prefix, no matter how long, is going to give you more information than color-coding. If you don't trust me take a resistor and ponder a bit why they give the value in color bands; it's ultimately practical!

      Read and abide by the Qt Code of Conduct

      K F 2 Replies Last reply 2 Jul 2019, 21:16
      2
      • K kshegunov
        2 Jul 2019, 20:56

        @fcarney said in Program compiles but generates a segmentation fault.:

        Naming all member variables with an m_ prefix tells you that you are using a member variable. This helps distinguish between temporary/stack variables and the member variables in your class.

        Although Qt uses that style I don't agree. Firstly, hungarian notation is very ugly, and verbose, and was never proven to be anything but actual noise. I'd rather see descriptive (possibly long) names that speak to me than to struggle with half-standard type/semantic prefixes.
        And secondly, a good IDE is going to draw you a picture (quite literally using different colors) for the different types of variables. Default color scheme for creator makes members orange-brown, while locals are dark blue and globals are black. Sorry, but no prefix, no matter how long, is going to give you more information than color-coding. If you don't trust me take a resistor and ponder a bit why they give the value in color bands; it's ultimately practical!

        K Offline
        K Offline
        Kent-Dorfman
        wrote on 2 Jul 2019, 21:16 last edited by Kent-Dorfman 7 Feb 2019, 21:17
        #5

        @kshegunov

        Well, other than long identifiers...they suck

        and I have yet to find an IDE that works the way I develop code and that doesn't crash 30 minutes into using it.

        K 1 Reply Last reply 2 Jul 2019, 21:34
        0
        • K Kent-Dorfman
          2 Jul 2019, 21:16

          @kshegunov

          Well, other than long identifiers...they suck

          and I have yet to find an IDE that works the way I develop code and that doesn't crash 30 minutes into using it.

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 2 Jul 2019, 21:34 last edited by kshegunov 7 Feb 2019, 21:45
          #6

          @Kent-Dorfman said in Program compiles but generates a segmentation fault.:

          @kshegunov

          Well, other than long identifiers...they suck

          Long's a relative term and context is everything. Although I don't like abrreviating I'm known to do it on occasion. I'd always prefer HermitePolynomial, to HermPoly though. The other extreme is to go the way they did it in that crappy old language - fortran - where identifiers had unreasonable length limits, so zpotrf is supposedly doing a "Cholesky factorization of a complex Hermitian positive definite matrix A"[1], but no sane person could deduce that by name.

          and I have yet to find an IDE that works the way I develop code and that doesn't crash 30 minutes into using it.

          Well I write in C++ and it's strongly typed (more or less), so unless I hate myself and use auto (the dreaded compiler-inferred typing) all over, I know exactly what types I'm dealing with instantly. And Creator, which is far from perfect mind you, can provide tooltips if I suddenly and inexplicably had forgotten. It haven't crashed on me for a long time too. I don't know what's the status with python, though, so I can't comment on that.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1
          • K kshegunov
            2 Jul 2019, 20:56

            @fcarney said in Program compiles but generates a segmentation fault.:

            Naming all member variables with an m_ prefix tells you that you are using a member variable. This helps distinguish between temporary/stack variables and the member variables in your class.

            Although Qt uses that style I don't agree. Firstly, hungarian notation is very ugly, and verbose, and was never proven to be anything but actual noise. I'd rather see descriptive (possibly long) names that speak to me than to struggle with half-standard type/semantic prefixes.
            And secondly, a good IDE is going to draw you a picture (quite literally using different colors) for the different types of variables. Default color scheme for creator makes members orange-brown, while locals are dark blue and globals are black. Sorry, but no prefix, no matter how long, is going to give you more information than color-coding. If you don't trust me take a resistor and ponder a bit why they give the value in color bands; it's ultimately practical!

            F Offline
            F Offline
            fcarney
            wrote on 2 Jul 2019, 21:55 last edited by
            #7

            @kshegunov said in Program compiles but generates a segmentation fault.:

            rather see descriptive (possibly long) names that speak to me

            Definitely +1 on this point. With copy/paste and auto complete this really has no excuse not to use long variable names. It definitely makes thing more understandable.

            The m_ prefix helps me understand things a bit quicker with or without the color coding. The part I like the most is functions that take variables that are intended to be set to a member.

            void memberFunction(int index){
                m_index = index;
            }
            

            That way I can use the same variable name and show intent both ways.

            C++ is a perfectly valid school of magic.

            K 1 Reply Last reply 2 Jul 2019, 22:02
            1
            • F fcarney
              2 Jul 2019, 21:55

              @kshegunov said in Program compiles but generates a segmentation fault.:

              rather see descriptive (possibly long) names that speak to me

              Definitely +1 on this point. With copy/paste and auto complete this really has no excuse not to use long variable names. It definitely makes thing more understandable.

              The m_ prefix helps me understand things a bit quicker with or without the color coding. The part I like the most is functions that take variables that are intended to be set to a member.

              void memberFunction(int index){
                  m_index = index;
              }
              

              That way I can use the same variable name and show intent both ways.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 2 Jul 2019, 22:02 last edited by kshegunov 7 Feb 2019, 22:02
              #8

              @fcarney said in Program compiles but generates a segmentation fault.:

              That way I can use the same variable name and show intent both ways.

              You can be even more explicit without the prefix:

              void SomeClass::someMember(int index)
              {
                  this->index = index;
              }
              

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • K Offline
                K Offline
                Kent-Dorfman
                wrote on 2 Jul 2019, 22:05 last edited by
                #9
                This post is deleted!
                J K 2 Replies Last reply 3 Jul 2019, 04:43
                0
                • K Kent-Dorfman
                  2 Jul 2019, 22:05

                  This post is deleted!

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 3 Jul 2019, 04:43 last edited by
                  #10

                  @Kent-Dorfman I don't think @kshegunov was talking about loop variables like i or j.
                  "one line comments inserted throughout to explain what the thing does" - everywhere where the variable is used?
                  It is highly subjective but I prefer variable/class names which are self-descriptive over short names.

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

                  1 Reply Last reply
                  2
                  • K Kent-Dorfman
                    2 Jul 2019, 22:05

                    This post is deleted!

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 3 Jul 2019, 08:51 last edited by kshegunov 7 Mar 2019, 08:55
                    #11

                    @Kent-Dorfman, @jsulm

                    Indeed. As usual it's compromise between utility and brevity. I, personally, rarely have variables that are over the 12 chars too, but I'm much more lenient when it comes to the types or methods involved. Yes, it makes sense not to go too long on the variables, especially the locals, but when you call something out of the current scope it does help if you can tell what it does, hence my mention of the zpotrf, which really tells you nothing about what this does. If it were called choleskyHermitian, or if it were encapsulated in a proper type (i.e. a template or something) it'd make much more sense than just:

                    MyMatrixTypedef x = zpotrf(y, ....);
                    

                    Example (excerpt from a test case):

                    HermiteQuadrature integral(maxOrder);
                    for (Hermite<real>::Order n = 1; n <= maxOrder; n++)  {
                        Hermite<lreal> Hnm1(n - 1), Hn(n);
                        xHermite xHn(n);
                    
                        real lhs = std::sqrt(2 * lreal(n)) * integral.evaluate(Hnm1);
                        real rhs = integral.evaluate(xHn);
                        if (isEven(n))
                            QVERIFY(isEqual(lhs, rhs));        // Integrating an odd function gives zero
                        else  {
                            // ...
                        }
                    }
                    

                    You should be able to tell at a glance what this does, which is the point of a good style. If you can't then my style isn't good.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    3
                    • C Christian Ehrlicher
                      2 Jul 2019, 16:37

                      @zlinux20 said in Program compiles but generates a segmentation fault.:

                      QLabel *label = new QLabel();

                      This will not initialize the member variable.

                      Z Offline
                      Z Offline
                      zlinux20
                      wrote on 4 Jul 2019, 08:01 last edited by
                      #12

                      @Christian-Ehrlicher

                      I ended up using this

                      label = new QLabel("default label");
                      label->setText("This is Qt");
                      

                      to get it to work. I also realize that I might need to learn a lot more about C++ classes and pointers before translating my Python gui code to C++ gui code.

                      Thank you.

                      J 1 Reply Last reply 4 Jul 2019, 09:01
                      0
                      • Z zlinux20
                        4 Jul 2019, 08:01

                        @Christian-Ehrlicher

                        I ended up using this

                        label = new QLabel("default label");
                        label->setText("This is Qt");
                        

                        to get it to work. I also realize that I might need to learn a lot more about C++ classes and pointers before translating my Python gui code to C++ gui code.

                        Thank you.

                        J Offline
                        J Offline
                        JonB
                        wrote on 4 Jul 2019, 09:01 last edited by
                        #13

                        @zlinux20
                        Are you saying you have written these two lines literally one after the other? What is the point of initializing the label with one string if you are going to change it to a different string immediately thereafter?

                        Z 1 Reply Last reply 4 Jul 2019, 10:12
                        1
                        • J JonB
                          4 Jul 2019, 09:01

                          @zlinux20
                          Are you saying you have written these two lines literally one after the other? What is the point of initializing the label with one string if you are going to change it to a different string immediately thereafter?

                          Z Offline
                          Z Offline
                          zlinux20
                          wrote on 4 Jul 2019, 10:12 last edited by zlinux20 7 Apr 2019, 10:15
                          #14

                          @JonB

                          Initially I had this

                          // mlabelwidget.cpp snippet
                          QLabel *label = new QLabel();
                          label->setText("This is Qt");
                          QWidget::update();
                          
                          and this in my class declaration
                          //mlabelwidget.h snippet
                          class Mlabelwidget : public QWidget
                          {
                            public:
                              Mlabelwidget(QWidget *parent = 0);
                              QLabel *label;
                              void changeText(QString input);
                          };
                          

                          when I found the hint of removing "QLabel *" from the declaration at this link I removed the "QLabel *" part and it worked i.e. there were no more segmentation faults. I wanted to report my progress and posted those lines that you mention. I left the second line in there from my earlier experiments, trying to initialize the QLabel. It is not needed now and my code is working as intended.

                          p.s. I am familiar with GUI Programming with Python Tkinter etc. I am learning just enough C++ at the moment to create a GUI program that needs to be an exe file as opposed to a Python script (client requirement).

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            Kent-Dorfman
                            wrote on 4 Jul 2019, 22:26 last edited by
                            #15

                            FWIW, something else that seems conspicuously missing is that I'm not seeing the Q_OBJECT macro defined in your class definitions that inherit from QObject.

                            That macro is required so that the MOC system understands that the class is subject to Qt framework meta-processing. Just a wag, but revert to the original version and add the Q_OBJECT macro in the header file like follows:

                            MyClass: public QWidget {
                            
                            Q_OBJECT
                            .
                            .
                            .
                            }
                            
                            Z 1 Reply Last reply 5 Jul 2019, 19:36
                            0
                            • K Kent-Dorfman
                              4 Jul 2019, 22:26

                              FWIW, something else that seems conspicuously missing is that I'm not seeing the Q_OBJECT macro defined in your class definitions that inherit from QObject.

                              That macro is required so that the MOC system understands that the class is subject to Qt framework meta-processing. Just a wag, but revert to the original version and add the Q_OBJECT macro in the header file like follows:

                              MyClass: public QWidget {
                              
                              Q_OBJECT
                              .
                              .
                              .
                              }
                              
                              Z Offline
                              Z Offline
                              zlinux20
                              wrote on 5 Jul 2019, 19:36 last edited by
                              #16

                              @Kent-Dorfman

                              Thank you for the hint. I did see it in some examples and noted it for future reference. I forgot all about it by the time I was working through (working/tutorial) examples I found online. I will be using the Q_OBJECT to change my existing (toy) program(s) to see how it works out.

                              1 Reply Last reply
                              0

                              12/16

                              4 Jul 2019, 08:01

                              • Login

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