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.

Program compiles but generates a segmentation fault.

Scheduled Pinned Locked Moved Solved General and Desktop
c++ qt5 gui
16 Posts 7 Posters 2.6k Views
  • 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.
  • 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

                      16/16

                      5 Jul 2019, 19:36

                      • Login

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