Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to find button name?



  • 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();
    

  • Lifetime Qt Champion

    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 :)


Log in to reply