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. Check if an object of QPushButton has been created?

Check if an object of QPushButton has been created?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 634 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.
  • L Offline
    L Offline
    legitnameyo
    wrote on last edited by
    #1

    I create a QPushButton in my .h using the following code

    QPushButton *global_button;
    

    It's in my .h since different functions access it and modify it in different ways. The issue I got is that there is a probability that I don't create a global_button using

    global_button = new QPushButton(this);
    

    but rather do something like

    if(global_button->text() == "end") {
        qDebug() << "End.";
    }
    

    before even implementing global_button. If I try to call global_button before global_button = new QPushButton(this); then the program crashes. Is there a way to circumvent this? AKA to check if there is a new instance of global_button?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @legitnameyo said in Check if an object of QPushButton has been created?:

      global_button

      Since it's a pointer you should properly initialize it to nullptr in the ctor and the you can check if it's still a nullptr or not - c++ basics.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • L Offline
        L Offline
        legitnameyo
        wrote on last edited by
        #3
        //in .h
        QPushButton *global_button = nullptr;
        
        //in .cpp
        if(global_button == nullptr { /* actions */}
        

        worked! Thanks!

        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