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. QObject::connect fails ... Don't know why (solved)
Forum Updated to NodeBB v4.3 + New Features

QObject::connect fails ... Don't know why (solved)

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 3.6k 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.
  • O Offline
    O Offline
    onlydp
    wrote on last edited by onlydp
    #1

    Hi guys,

    I'm new at developing with Qt, but I promised a friend of mine to programm a Raspberry Pi Software to controll a hookah desk. Of course we want a grafical UI but we need C++ as programminung language due to some interfaces. So I thought I wanna try QML in combination with C++.

    Me - a total Qt noob - spend the last few hours figuring out why I always get this error messenge:

    QObject::connect: No such slot QObject::qmlSignal(QString) in ..\Shishatisch\main.cpp:18

    I searched for a sollution for this problem and it turned out that some other guys have that problem, too. The solution always was to name all functions the same way. But mine are since the beginning. So that didn't help me. 1.000.000 Google searches later I gave up and decided to ask some experts.

    Due to the fact that this isn't exactly a secret project I will now post all of my source code (beside the qml code cause it would be too long)

    connect.h:

    #ifndef CONNECT_H
    #define CONNECT_H
    
    #include <qstring.h>
    #include <QObject>
    
    
    class Connect : public QObject
    {
    public:
        explicit Connect(QObject *parent = 0);
    
    signals:
    
    public slots:
        void qmlSignal(QString msg);
    };
    
    #endif // CONNECT_H
    

    connect.cpp

    #include "connect.h"
    #include <QDebug>
    
    Connect::Connect(QObject *parent) : QObject(parent)
    {
    
    }
    
    
    
    void Connect::qmlSignal(QString msg)
    {
        qDebug() << msg;
    }
    

    main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <connect.h>
    #include <QQmlContext>
    #include <QQuickItem>
    #include <QtCore>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        Connect* data=new Connect();
        QObject* item=engine.rootObjects().value(0);
        QObject::connect(item,SIGNAL(qmlSignal(QString)),data,SLOT(qmlSignal(QString)));
    
    
    
        return app.exec();
    }
    
    C 1 Reply Last reply
    0
    • O onlydp

      Hi guys,

      I'm new at developing with Qt, but I promised a friend of mine to programm a Raspberry Pi Software to controll a hookah desk. Of course we want a grafical UI but we need C++ as programminung language due to some interfaces. So I thought I wanna try QML in combination with C++.

      Me - a total Qt noob - spend the last few hours figuring out why I always get this error messenge:

      QObject::connect: No such slot QObject::qmlSignal(QString) in ..\Shishatisch\main.cpp:18

      I searched for a sollution for this problem and it turned out that some other guys have that problem, too. The solution always was to name all functions the same way. But mine are since the beginning. So that didn't help me. 1.000.000 Google searches later I gave up and decided to ask some experts.

      Due to the fact that this isn't exactly a secret project I will now post all of my source code (beside the qml code cause it would be too long)

      connect.h:

      #ifndef CONNECT_H
      #define CONNECT_H
      
      #include <qstring.h>
      #include <QObject>
      
      
      class Connect : public QObject
      {
      public:
          explicit Connect(QObject *parent = 0);
      
      signals:
      
      public slots:
          void qmlSignal(QString msg);
      };
      
      #endif // CONNECT_H
      

      connect.cpp

      #include "connect.h"
      #include <QDebug>
      
      Connect::Connect(QObject *parent) : QObject(parent)
      {
      
      }
      
      
      
      void Connect::qmlSignal(QString msg)
      {
          qDebug() << msg;
      }
      

      main.cpp:

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <connect.h>
      #include <QQmlContext>
      #include <QQuickItem>
      #include <QtCore>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      
          Connect* data=new Connect();
          QObject* item=engine.rootObjects().value(0);
          QObject::connect(item,SIGNAL(qmlSignal(QString)),data,SLOT(qmlSignal(QString)));
      
      
      
          return app.exec();
      }
      
      C Offline
      C Offline
      code_fodder
      wrote on last edited by code_fodder
      #2

      @onlydp You have some little issues here regarding the difference between a slot and a signal I think.

      I signal is not really a function - you declare it but do not define its implementation. You will "emit" the signal at some point and pass parameters with it. The slot is where a signal can be received and you declare it in the header and also declare an implementation. So in the header file its:

      signals:
      void mySignalToSendData(QString msg);
      
      publis slots:
      void mySlotToReceiveData(QString msg);
      

      and in the cpp file:

      void myClass::mySlotToReceiveData(QString msg)
      {
      .. do stuff..
      }
      

      Then you connect a signal to a slot (maybe in main):

      myClass myclass;
      connect(&myclass, SIGNAL(mySignalToSendData(QString msg)), &myclass, SLOT(mySlotToReceiveData(QString msg))
      

      In your code you do not want to make your slots and signals have the same name - it will be very confusing I think. Signals are like "send message" functions and slots are like "message receivers"

      1 Reply Last reply
      1
      • A Offline
        A Offline
        alex_malyu
        wrote on last edited by alex_malyu
        #3

        @onlydp said:

        class Connect : public QObject
        {
        public:
        explicit Connect(QObject *parent = 0);

        signals:

        public slots:

        I would suggest not to name slot FOO "signalFoo" so people are not confused.
        But you are missing Q_OBJECT macro:

        class Connect : public QObject
        {
        Q_OBJECT
        public:
        ......

        1 Reply Last reply
        2
        • O Offline
          O Offline
          onlydp
          wrote on last edited by
          #4

          Thx guys,

          the missing Macro was my fault. I added it and magic it works. And I renamed the slot into qmlslot so there shouldn't be problems by that, too.

          Big thank you!

          Joel BodenmannJ 1 Reply Last reply
          0
          • O onlydp

            Thx guys,

            the missing Macro was my fault. I added it and magic it works. And I renamed the slot into qmlslot so there shouldn't be problems by that, too.

            Big thank you!

            Joel BodenmannJ Offline
            Joel BodenmannJ Offline
            Joel Bodenmann
            wrote on last edited by
            #5

            @onlydp Can you please mark this question as solved (by adding [Solved] in front of your post title when editing the first post) so others don't drop by in order to try to help?

            Industrial process automation software: https://simulton.com
            Embedded Graphics & GUI library: https://ugfx.io

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved