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. QPushButton set checked causes program to crash
Forum Updated to NodeBB v4.3 + New Features

QPushButton set checked causes program to crash

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 2 Posters 3.1k 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.
  • Q Offline
    Q Offline
    Qtstarter121
    wrote on 23 Apr 2018, 15:07 last edited by Qtstarter121
    #1

    I have a situation where I create a new QPushButton dynamically and then add it to an existing layout
    obtained from a .ui file. It is a class private variable named cButton.

    I initialize my button and do some other things:

    cButton = new QPushButton;
    cButton->setObjectName("button1");
    cButton->setCheckable(true);
    connect(cButton, SIGNAL(clicked(bool)), this, SLOT(on_button1_clicked(bool)));
    

    afterwards I do this:

     cButton->setChecked(true);
    on_button1_clicked(true); 
    
    

    With the above two lines, my program crashes when launched. Without them it runs fine.
    With just the first line where it's setting the checked state it also still crashes.
    What could be the reason for this?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 23 Apr 2018, 15:27 last edited by
      #2

      I think you made infinite loop.
      set breakpoint in on_button1_clicked
      and see if its called over and over

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qtstarter121
        wrote on 23 Apr 2018, 15:31 last edited by
        #3

        Nope no loop there. My slot isn't even called once.

        M 1 Reply Last reply 23 Apr 2018, 15:33
        0
        • Q Qtstarter121
          23 Apr 2018, 15:31

          Nope no loop there. My slot isn't even called once.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 23 Apr 2018, 15:33 last edited by
          #4

          @Qtstarter121
          ok, try single step and see what lines it crashes in

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 23 Apr 2018, 15:35 last edited by
            #5

            Also make sure u didnt do the classic

            QPushButton *cButton = new QPushButton; // local, not class member in .h
            and then later
            cButton->setChecked(true); // the one from .h that is just dangling pointer

            Q 1 Reply Last reply 23 Apr 2018, 15:41
            0
            • Q Offline
              Q Offline
              Qtstarter121
              wrote on 23 Apr 2018, 15:40 last edited by
              #6

              Unfortunately I can't single step because I don't even have time to attach a debugger. My program crashes within seconds of launching. I was hoping to get maybe some possible reasons for what I could have done wrong.

              1 Reply Last reply
              0
              • M mrjj
                23 Apr 2018, 15:35

                Also make sure u didnt do the classic

                QPushButton *cButton = new QPushButton; // local, not class member in .h
                and then later
                cButton->setChecked(true); // the one from .h that is just dangling pointer

                Q Offline
                Q Offline
                Qtstarter121
                wrote on 23 Apr 2018, 15:41 last edited by
                #7

                @mrjj I actually had this beforehand where it was a local variable but it is a class pointer in my code right now.

                M 1 Reply Last reply 23 Apr 2018, 15:43
                0
                • Q Qtstarter121
                  23 Apr 2018, 15:41

                  @mrjj I actually had this beforehand where it was a local variable but it is a class pointer in my code right now.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 23 Apr 2018, 15:43 last edited by mrjj
                  #8

                  @Qtstarter121
                  Ok, i cant guess. But i promise you it wont crash from setChecked
                  so its an actual code bug.

                  Without debugger life will be really hard.

                  what do u do in on_button1_clicked ?

                  Q 1 Reply Last reply 23 Apr 2018, 15:56
                  0
                  • M mrjj
                    23 Apr 2018, 15:43

                    @Qtstarter121
                    Ok, i cant guess. But i promise you it wont crash from setChecked
                    so its an actual code bug.

                    Without debugger life will be really hard.

                    what do u do in on_button1_clicked ?

                    Q Offline
                    Q Offline
                    Qtstarter121
                    wrote on 23 Apr 2018, 15:56 last edited by Qtstarter121
                    #9

                    @mrjj Fair enough. I agree actually that setChecked shouldn't be my issue and that it's likely something else I've done. I'm not sure if it's worth mentioning, but prior to my new button initialization and slot connection, I do the following:

                    QMetaObject::connectSlotsByName(this);
                    

                    and I see this in my output log : No matching signal for on_button1_clicked()

                    To answer your question, my slot currently does this:

                    void MyClass:on_button1_clicked(bool checked)
                    {
                       if (checked)
                          cButton->setText("ENABLED");
                       else
                          cButton->setText("DISABLED");
                    
                       SetWidgetProperty(dynamic_cast<QWidget*>(cButton), "highlighted", checked);
                    
                    }
                    
                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 23 Apr 2018, 16:00 last edited by
                      #10

                      hi
                      do
                      SetWidgetProperty
                      called setChecked ?
                      if that triggers clicked(bool) u get infinite calls.

                      Q 1 Reply Last reply 23 Apr 2018, 16:03
                      0
                      • M mrjj
                        23 Apr 2018, 16:00

                        hi
                        do
                        SetWidgetProperty
                        called setChecked ?
                        if that triggers clicked(bool) u get infinite calls.

                        Q Offline
                        Q Offline
                        Qtstarter121
                        wrote on 23 Apr 2018, 16:03 last edited by
                        #11

                        @mrjj The thing is, If I do this:

                         cButton->setChecked(true);
                        //on_button1_clicked(true); 
                        

                        I still get the same crash. Which tells me on_button1_clicked(bool) is irrelevant. Right?

                        M 1 Reply Last reply 23 Apr 2018, 16:05
                        0
                        • Q Qtstarter121
                          23 Apr 2018, 16:03

                          @mrjj The thing is, If I do this:

                           cButton->setChecked(true);
                          //on_button1_clicked(true); 
                          

                          I still get the same crash. Which tells me on_button1_clicked(bool) is irrelevant. Right?

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 23 Apr 2018, 16:05 last edited by
                          #12

                          @Qtstarter121
                          yeah, seems so.
                          so it seems to crash on ?
                          cButton->setChecked(true);
                          since you dont have debugger
                          use qDebug

                          qDebug() << "before";
                          cButton->setChecked(true);
                          qDebug() << "after";

                          to fidn WHAT makes it crash

                          1 Reply Last reply
                          0
                          • Q Offline
                            Q Offline
                            Qtstarter121
                            wrote on 23 Apr 2018, 16:06 last edited by
                            #13

                            Hold on I might be wrong about that. Let me try something and get back to you.

                            M 1 Reply Last reply 23 Apr 2018, 16:07
                            0
                            • Q Qtstarter121
                              23 Apr 2018, 16:06

                              Hold on I might be wrong about that. Let me try something and get back to you.

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 23 Apr 2018, 16:07 last edited by
                              #14

                              @Qtstarter121
                              You really should install debugger. :)
                              Its ment to find stuff like this :)

                              Q 1 Reply Last reply 23 Apr 2018, 16:32
                              1
                              • Q Offline
                                Q Offline
                                Qtstarter121
                                wrote on 23 Apr 2018, 16:22 last edited by
                                #15

                                Okay so it looks like i am having an off-day because I just found out the source of my problem.
                                Turns out the issue was with code in the slot ( not Qt stuff but c++ using a proprietary framework) I neglected to post which was using an uninitialized variable. facepalm

                                I neglected to include it because I wrongly assumed the code wasn't relevant.

                                Sorry for wasting your time and thanks for the help.

                                1 Reply Last reply
                                0
                                • M mrjj
                                  23 Apr 2018, 16:07

                                  @Qtstarter121
                                  You really should install debugger. :)
                                  Its ment to find stuff like this :)

                                  Q Offline
                                  Q Offline
                                  Qtstarter121
                                  wrote on 23 Apr 2018, 16:32 last edited by
                                  #16

                                  @mrjj I actually do have a debugger available and installed. The problem is that the way my program is launched, i'm not sure how I can run it with gdb. if I attempt a command such as " gdb < my usual program launch command>
                                  I get an error like this: not in executable format: File format not recognized

                                  However I can attach a debugger once my process is running via gdb attach < pid > but in this case the issue was in my constructor so that would run immediately and no time to attach the debugger to catch the crash issue so print statements are my only option.

                                  M 1 Reply Last reply 23 Apr 2018, 17:12
                                  0
                                  • Q Qtstarter121
                                    23 Apr 2018, 16:32

                                    @mrjj I actually do have a debugger available and installed. The problem is that the way my program is launched, i'm not sure how I can run it with gdb. if I attempt a command such as " gdb < my usual program launch command>
                                    I get an error like this: not in executable format: File format not recognized

                                    However I can attach a debugger once my process is running via gdb attach < pid > but in this case the issue was in my constructor so that would run immediately and no time to attach the debugger to catch the crash issue so print statements are my only option.

                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 23 Apr 2018, 17:12 last edited by
                                    #17

                                    @Qtstarter121
                                    You are not using Creator?

                                    Q 1 Reply Last reply 23 Apr 2018, 19:01
                                    0
                                    • M mrjj
                                      23 Apr 2018, 17:12

                                      @Qtstarter121
                                      You are not using Creator?

                                      Q Offline
                                      Q Offline
                                      Qtstarter121
                                      wrote on 23 Apr 2018, 19:01 last edited by
                                      #18

                                      @mrjj nope, gdb.

                                      M 1 Reply Last reply 23 Apr 2018, 19:01
                                      0
                                      • Q Qtstarter121
                                        23 Apr 2018, 19:01

                                        @mrjj nope, gdb.

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 23 Apr 2018, 19:01 last edited by
                                        #19

                                        @Qtstarter121
                                        Ok so you compile outside of Creator and not using the dgb integration ?

                                        1 Reply Last reply
                                        0

                                        1/19

                                        23 Apr 2018, 15:07

                                        • Login

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