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

Confusion about signals/slots



  • Hey,

    I am very new to Qt and this question will have probably already been answered a dozen times (although I cannot find it). The program I'm writing basically creates a QDockWidget of which I want to connect the visibilityChanged(bool) signal to a custom slot that then checks/unchecks a menu item. It all works as I expect the problem I'm having is I get the error message

    QMetaObject::connectSlotsByName: No matching signal for on_actionFirstDock_visibilityChanged(bool)
    

    And I don't really understand what the problem is since it is working correctly.

    In mainwindow.h I declare:

    private slots:
        void on_actionFirstDock_visibilityChanged(bool visible);
    

    mainwindow.cpp

     MainWindow::MainWindow(QWidget *parent) :
         QMainWindow(parent),
         ui(new Ui::MainWindow){
         ui->setupUi(this);
    
         firstDock = new QDockWidget(this);
    
         connect(firstDock,SIGNAL(visibilityChanged(bool)),this,SLOT(on_actionFirstDock_visibilityChanged(bool)));
     }
     void MainWindow::on_actionFirstDock_visibilityChanged(bool visible){
         ui->actionFirst_menu->setChecked(visible);
     }

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    That's because you named your slot the same way as is expected for the auto connection feature.

    Just remove the underscores (and make it full camelcase for easier reading) and you should be good to go.


  • Lifetime Qt Champion

    Hi
    as a note , you might want to know about the new syntax
    https://wiki.qt.io/New_Signal_Slot_Syntax
    Its better than the SIGNAL/SLOT macros as it fails compile time and not
    run time if something is not right.


Log in to reply