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

Error when connecting a custom class and MainWindow



  • Hello, I made a class called Button and I have an object of type Button*. When I try connecting a signal of that object with a slot of MainWindow, it just says "no matching member function for call to 'connect'" and "no known conversion from 'Button *' to 'const QObject *' for 1st argument.

    Button* button = new Button();
    connect(button, SIGNAL(Button::sendDeleteSignal()), this, SLOT(checkForDeletes())); //error
    

    Note, I do not have much experience. How do I fix this? qobject_cast does not work.



  • The problem was that my class inherited QGraphicsItem, not QObject, but I found another way to do what I wanted. From now on, I will use the new syntax. Thank you!



  • @Forfunckle
    In a word, especially if you sound like you are writing new Qt code, switch to the new, compile-time-supported way of connecting signals & slots, as documented in https://doc.qt.io/qt-5/signalsandslots.html or read through https://wiki.qt.io/New_Signal_Slot_Syntax, instead of using SIGNAL() & SLOT() macros. You will get a (more) meaningful compile-time error for whatever your fault is.

    In this case, "no known conversion from 'Button *' to 'const QObject *' leads me to wonder what your Button class is? It sounds like it isn't even a QWidget....?


  • Lifetime Qt Champion

    Hi
    the syntax is not correct should be
    connect(button, SIGNAL(sendDeleteSignal()), this, SLOT(checkForDeletes()));
    with no classname:: and would show paramters if it has any.

    Also
    Did you remember Q_OBJECT

    class Button : public QPushButton
    {
    Q_OBJECT

    Please show Buttons .h file

    It must also inherit from a QWidget or QObject which your error suggests you did not.

    And as @JonB says, the new syntax is far better as it helps to find errors.

    connect(button, &Button::sendDeleteSignal, this, &MainWindow::checkForDeletes);



  • The problem was that my class inherited QGraphicsItem, not QObject, but I found another way to do what I wanted. From now on, I will use the new syntax. Thank you!