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

How to use functions of the QObject class in Visual Studio



  • Hello Everyone,

    I'm using qt in Visual Studio 2017.
    I'm trying to connect a signal to a slot using

    QTcpServer*pTcpServer = new QTcpServer();	
    QObject::connect(pTcpServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
    

    And I'm getting the following errors:

    QObject::connect : illegal call of non-static member function
    this: can only be referenced inside non-static member functions or non-static data member initializers.

    I've done some research and found out that the first error means i need to create an instance of the class in order to call the function Connect. I tried to do that but i can't seem to find the right syntax to do it.

    Thanks !



  • @JohnSRV
    Please start by using the new signal/slot syntax in all new code, as per e.g. https://wiki.qt.io/New_Signal_Slot_Syntax. It will then be much easier to diagnose what you have done wrong. Which will have nothing to do with Visual Studio.


  • Qt Champions 2019

    @JohnSRV To add to @JonB : where do you have this code? It needs to be in a QObject based class. There is no need for QObject:: at all.



  • Basically the main problem here seems to be that this is not a QObject class. This is not necessarily an issue if you use the new syntax as @JonB is suggesting


Log in to reply