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. How to find button name?
Qt 6.11 is out! See what's new in the release blog

How to find button name?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 2.0k Views 2 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.
  • P Offline
    P Offline
    part-time roadman
    wrote on last edited by
    #1

    I'm currently trying to find the button name with this but it says 'expression must have class type'.

    QPushButton* button = (QPushButton*)sender();
    QString btn = button.objectName();
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      When casting QWidgets please use the Qt version and not c-casts.

      void MainWindow::on_pushButton_2_clicked()
      {
      
          QPushButton *but = qobject_cast<QPushButton * >( sender() );
          if (but)
              qDebug() << but ->objectName();
      }
      

      Note that if sender is not a QPushButtonm then "but" will be null and will crash
      if you use it. Hence always use the if check :)

      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