Important: Please read the 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

    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