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 widget is pushbutton
Qt 6.11 is out! See what's new in the release blog

Check if widget is pushbutton

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 6 Posters 3.2k 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.
  • hskoglundH hskoglund

    Hi, you could try:

    if (QString("QPushButton") == widget->metaObject()->className())
    ...
    
    Christian EhrlicherC Online
    Christian EhrlicherC Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @hskoglund no, use proper c++ instead string comparison please.

    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
    1
    • Please_Help_me_DP Please_Help_me_D

      Hi,

      I have a widget QWidget* and I need to check is that widget is a QPushButton* (or QFrame* or any inherited from QWidget*). How to do that? The code:

      if (static_cast<QPushButton*>(widget)){
          ....
      }
      

      doesn't seem to work...

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #5

      @Please_Help_me_D hi,
      use qobject_cast

      1 Reply Last reply
      1
      • hskoglundH Offline
        hskoglundH Offline
        hskoglund
        wrote on last edited by
        #6

        Yes it's better than string comparison, but it's 2 lines :-)

        auto pushButton = qobject_cast<QPushButton*>(widget);
        if (nullptr != pushButton)
        
        ODБOïO 1 Reply Last reply
        3
        • hskoglundH hskoglund

          Yes it's better than string comparison, but it's 2 lines :-)

          auto pushButton = qobject_cast<QPushButton*>(widget);
          if (nullptr != pushButton)
          
          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by
          #7

          @hskoglund can't you do?

          if(auto pushButton = qobject_cast<QPushButton*>(pushButton ))
          
          hskoglundH S 2 Replies Last reply
          0
          • ODБOïO ODБOï

            @hskoglund can't you do?

            if(auto pushButton = qobject_cast<QPushButton*>(pushButton ))
            
            hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by
            #8

            @LeLev sure but doesn't the compiler complain about '=' inside an if statement?

            ODБOïO 1 Reply Last reply
            0
            • hskoglundH hskoglund

              @LeLev sure but doesn't the compiler complain about '=' inside an if statement?

              ODБOïO Offline
              ODБOïO Offline
              ODБOï
              wrote on last edited by ODБOï
              #9

              @hskoglund i just tested with qt5.14/minGw, it compiles and i don't see any warnings

              1 Reply Last reply
              0
              • hskoglundH Offline
                hskoglundH Offline
                hskoglund
                wrote on last edited by
                #10

                Ok I guess that eliminates all arguments for the string comparison...

                Please_Help_me_DP 1 Reply Last reply
                0
                • hskoglundH hskoglund

                  Ok I guess that eliminates all arguments for the string comparison...

                  Please_Help_me_DP Offline
                  Please_Help_me_DP Offline
                  Please_Help_me_D
                  wrote on last edited by Please_Help_me_D
                  #11

                  @hskoglund said in Check if widget is pushbutton:

                  if (QString("QPushButton") == widget->metaObject()->className())
                  ...

                  this works, thank you!

                  But when I try to execute:

                  auto plot = qobject_cast<QwtPlot*>(widget);
                  

                  I get an error:
                  mainwindow.obj:-1: error: LNK2001: unresolved external symbol "public: static struct QMetaObject const QwtPlot::staticMetaObject" (?staticMetaObject@QwtPlot@@2UQMetaObject@@B)

                  QwtPlot is from QWT library. It is declared as:
                  class QWT_EXPORT QwtPlot: public QFrame, public QwtPlotDict

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • Please_Help_me_DP Please_Help_me_D

                    @hskoglund said in Check if widget is pushbutton:

                    if (QString("QPushButton") == widget->metaObject()->className())
                    ...

                    this works, thank you!

                    But when I try to execute:

                    auto plot = qobject_cast<QwtPlot*>(widget);
                    

                    I get an error:
                    mainwindow.obj:-1: error: LNK2001: unresolved external symbol "public: static struct QMetaObject const QwtPlot::staticMetaObject" (?staticMetaObject@QwtPlot@@2UQMetaObject@@B)

                    QwtPlot is from QWT library. It is declared as:
                    class QWT_EXPORT QwtPlot: public QFrame, public QwtPlotDict

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @Please_Help_me_D Then you forgot to link against the qwt library.

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

                    Please_Help_me_DP 1 Reply Last reply
                    2
                    • Christian EhrlicherC Christian Ehrlicher

                      @Please_Help_me_D Then you forgot to link against the qwt library.

                      Please_Help_me_DP Offline
                      Please_Help_me_DP Offline
                      Please_Help_me_D
                      wrote on last edited by
                      #13

                      @Christian-Ehrlicher thank you! I added in .pro file:

                      DEFINES += QWT_DLL
                      

                      Could you please explain what this line means? I've found it in internet but I don't understand how it works

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

                        Even it works you should follow the official documentation: https://qwt.sourceforge.io/qwtinstall.html#COMPILEANDLINKAPP

                        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
                        1
                        • ODБOïO ODБOï

                          @hskoglund can't you do?

                          if(auto pushButton = qobject_cast<QPushButton*>(pushButton ))
                          
                          S Offline
                          S Offline
                          SimonSchroeder
                          wrote on last edited by
                          #15

                          @LeLev said in Check if widget is pushbutton:

                          @hskoglund can't you do?

                          if(auto pushButton = qobject_cast<QPushButton*>(pushButton ))
                          

                          It looks like several C++ features got mixed up in this post. The original question wasn't too far off:

                          if (static_cast<QPushButton*>(widget))
                          

                          The only problem here is that the wrong type of cast was used. static_cast<>() means: "I know what I am doing.", i.e. assume that the cast is always allowed. The correct C++ feature is dynamic_cast<>(). So, a C++-only solution would look like this:

                          if(dynamic_cast<QPushButton*>(widget))
                          

                          In the context of Qt it was correctly pointed out that qobject_cast<>() is preferrable over dynamic_cast<>() (as it might be faster and does not need RTTI). A normal replacement of the cast is the easiest solution here:

                          if(qobject_cast<QPushButton*>(widget))
                          

                          I am not entirely sure what C++ feature would enable the variant suggested by @LeLev :

                          if(auto pushButton = qobject_cast<QPushButton*>(pushButton ))
                          

                          As far as I understand variable declarations are not allowed in if statements. C++17 introduced such a feature, however it has the declaration and the condition separated:

                          if(auto pushButton = qobject_cast<QPushButton*>(pushButton ); pushButton)  // this works since C++17
                          

                          Has anyone any idea why the version suggested by @LeLev would work?

                          B 1 Reply Last reply
                          0
                          • S SimonSchroeder

                            @LeLev said in Check if widget is pushbutton:

                            @hskoglund can't you do?

                            if(auto pushButton = qobject_cast<QPushButton*>(pushButton ))
                            

                            It looks like several C++ features got mixed up in this post. The original question wasn't too far off:

                            if (static_cast<QPushButton*>(widget))
                            

                            The only problem here is that the wrong type of cast was used. static_cast<>() means: "I know what I am doing.", i.e. assume that the cast is always allowed. The correct C++ feature is dynamic_cast<>(). So, a C++-only solution would look like this:

                            if(dynamic_cast<QPushButton*>(widget))
                            

                            In the context of Qt it was correctly pointed out that qobject_cast<>() is preferrable over dynamic_cast<>() (as it might be faster and does not need RTTI). A normal replacement of the cast is the easiest solution here:

                            if(qobject_cast<QPushButton*>(widget))
                            

                            I am not entirely sure what C++ feature would enable the variant suggested by @LeLev :

                            if(auto pushButton = qobject_cast<QPushButton*>(pushButton ))
                            

                            As far as I understand variable declarations are not allowed in if statements. C++17 introduced such a feature, however it has the declaration and the condition separated:

                            if(auto pushButton = qobject_cast<QPushButton*>(pushButton ); pushButton)  // this works since C++17
                            

                            Has anyone any idea why the version suggested by @LeLev would work?

                            B Offline
                            B Offline
                            Bonnie
                            wrote on last edited by
                            #16

                            @SimonSchroeder said in Check if widget is pushbutton:

                            Has anyone any idea why the version suggested by @LeLev would work?

                            I've been using this feature for many years, it's definitely NOT a C++17 feature.
                            As I look into https://en.cppreference.com/w/cpp/language/if , it is called

                            declaration of a single non-array variable with a brace-or-equals initializer

                            1 Reply Last reply
                            1

                            • Login

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