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 pointer randomly becomes null
Forum Updated to NodeBB v4.3 + New Features

QPushButton pointer randomly becomes null

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpushbuttonnull pointer
9 Posts 3 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 last edited by Qtstarter121
    #1

    Created a private QPushButton pointer object in my header, and in my constructor I call an Init function which sets my pointer to a new QPushButton, then I add it to an existing grid layout. I proceed to connect my button to a slot I have created,
    and that works fine. That slot calls another function which then checks that same pointer variable and somehow now it's set to null.

    I'm just not sure how it's possible that my button pointer becomes null.

    Any ideas?

    Thanks.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Can you show us your code?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qtstarter121
        wrote on last edited by
        #3

        // in my header :
        QPushButton* buttonPtr;

        // In Init() called by constructor:

        QGridLayout *layout = QWidget::findChild<QGridLayout *>("displayControlGridLayout");

        if(layout)
        {

        buttonPtr = new QPushButton("my button");
        layout->addWidget(buttonPtr, 1, 1, 1, 1);
        connect(buttonPtr, SIGNAL(clicked()),this,SLOT(on_button_clicked()));

        }

        // In my slot:
        void MyClass::on_button_clicked()
        {
        switch_page("Page 1");
        }
        // switch_page

        void MyClass::switch_page(QString page)
        {
        QPushbutton *button;
        if(page == "Page 1")
        button = buttonPtr;

        if(button == 0)
        {
        cout << " Button not found" << std::endl;
        }

        }
        // So basically i'm always seeing " Button not found" , but I can confirm that the slot is called prior to this function.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So that means that
          if(page == "Page 1") do not work as you think.

          have you debug it and tested if page does contain "Page 1" its actually sets
          button ?
          You compare a QString to a const char * which should work
          http://doc.qt.io/qt-5/qstring.html#operator-eq-eq-1

          But please debug and verify.

          1 Reply Last reply
          1
          • Q Offline
            Q Offline
            Qtstarter121
            wrote on last edited by
            #5

            Well i have other function calls such as switch_page("Page 2");
            and they work fine , the only difference is that in switch_page I would have
            if(page == "Page 2")
            button = QWidget::findChild<QPushButton *>("myButton");

            so in the first case I was setting button to my "global" pointer buttonPtr, and in this case I'm using
            QWidget::findChild.

            So if(page == "some string")

            definitely shouldn't be my issue otherwise if(page == "Page 2")
            button = QWidget::findChild<QPushButton *>("myButton"); wouldn't work either. I think it's something to do with buttonPtr.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              can you add if(buttonPtr) qDebug("It's not a buttonPtr problem"); inside MyClass::switch_page

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Well just debug it.
                place break point at
                buttonPtr = new QPushButton("my button");
                and in
                void MyClass::on_button_clicked()
                in the top

                and see what happens by single stepping.

                1 Reply Last reply
                2
                • Q Offline
                  Q Offline
                  Qtstarter121
                  wrote on last edited by
                  #8

                  Hmm looks like i wasted all your time. I made a ridiculous mistake and didn't notice that at the start of my switch_page function I had the following( it was someone else's code):

                  // find the widget for the requested page
                  widget = QWidget::findChild<QWidget *>(pPage);

                    if (widget == 0)
                    {
                       cout << qPrintable(pPage) << " not found" << std::endl;
                       return;
                    }
                  

                  facepalm
                  So if the above code is commented out, then I will have a valid buttonPtr.

                  Sorry everyone and thanks for your help anyway.

                  1 Reply Last reply
                  2
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi
                    That is totally ok. We just hang out here to help people so
                    its just super you found the actual reason :)

                    1 Reply Last reply
                    3

                    • Login

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