0

I'm trying to get the position updates in QCoreApplication, Following is the code i'm trying but i'm getting error as

QObject::connect: Cannot connect (null)::positionUpdated(QGeoPositionInfo) to QObject::positionUpdated(QGeoPositionInfo)

Code:

#include <QCoreApplication>
#include <QDebug>
#include <QObject>
#include <QtCore>


class MyClass : public QObject
{
//    Q_OBJECT
public:
    MyClass(QObject *parent = 0)
        : QObject(parent)
    {
        qDebug() << "In class";

        QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(this);
        qDebug() << source;
            connect(source, SIGNAL(positionUpdated(QGeoPositionInfo)),
                    this, SLOT(positionUpdated(QGeoPositionInfo)));
            source->startUpdates();
    }

private slots:
    void positionUpdated(const QGeoPositionInfo &info)
    {
        qDebug() << "Position updated:" << info;
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qDebug("Qt running !");

    MyClass *objmyclass = new MyClass;

    return a.exec();
}

If i uncomment the Q_OBJECT i get error as:

:-1: error: the vtable symbol may be undefined because the class is missing its key function
K Vinay
  • 13
  • 6
  • First of all you must uncomment Q_OBJECT. Second one is what is the source variable value? Can you show QGeoPositionInfoSource::createDefaultSource's body? – Alexander Chernin Mar 17 '16 at 07:34
  • If Q_OBJECT is uncommented, i get the error as: :-1: error: the vtable symbol may be undefined because the class is missing its key function, QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(this); – K Vinay Mar 17 '16 at 08:07
  • That's ok! Error is in the another place. – Alexander Chernin Mar 17 '16 at 08:10
  • About createDefaultSource from the doc **Returns 0 if the system has no default position source, no valid plugins could be found or the user does not have the permission to access the current position** Can you comment this function call? – Alexander Chernin Mar 17 '16 at 08:12
  • Permission for location access is been added, this works fine for GUI application, only has this issue with QCoreApplication – K Vinay Mar 17 '16 at 08:18
  • Add #include "main.moc" after the main method! :) Then run qmake, build and all will be ok – Alexander Chernin Mar 17 '16 at 08:20
  • Thankyou for reply, but #include after main() ? – K Vinay Mar 17 '16 at 08:22
  • Yes, after main() {} – Alexander Chernin Mar 17 '16 at 08:25
  • But i get error as main.moc no such file or directory. – K Vinay Mar 17 '16 at 08:26
  • Right mouse click on your project's name -> Clear -> Run qmake – Alexander Chernin Mar 17 '16 at 08:27
  • i have tried running qmake also cleaned the build, but same error as main.moc dosent exists, should that main.moc need to be created first? – K Vinay Mar 17 '16 at 08:30
  • And i'm building this project for Android platform, thought would be important to be mentioned – K Vinay Mar 17 '16 at 08:31
  • qmake must create all Q_OBJECT classes so your main.moc must be created autimatically by it. I've run your code and it works. Try manually clear the working directory – Alexander Chernin Mar 17 '16 at 08:34
  • Thankyou very much, that solved the issue but, now getting weird error as, W/ (22093): (null):0 ((null)): QObject::connect: Cannot connect (null)::positionUpdated(QGeoPositionInfo) to MyClass::positionUpdated(QGeoPositionInfo) F/libc (22093): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 22110 (mycompany.test1) "com.mycompany.test1" died. – K Vinay Mar 17 '16 at 08:43
  • Can you please help me with that? – K Vinay Mar 17 '16 at 08:44
  • It seems to me the error with **createDeafultSource** it returns 0 – Alexander Chernin Mar 17 '16 at 09:12
  • Its returning 0 here in QCoreApplication, works fine with QApplication, does QGeoPositionInfoSouce not works with QCoreApplication? – K Vinay Mar 17 '16 at 09:13
  • And you need QCoreApplication only? – Alexander Chernin Mar 17 '16 at 09:14
  • Actually the application is running as background service on Android, so needed to have QCoreApplication – K Vinay Mar 17 '16 at 09:15
  • Try get available sources with **QStringList QGeoPositionInfoSource::availableSources()** and if it's not empty create source variable by QGeoPositionInfoSource::createSource(availableSources[0]) – Alexander Chernin Mar 17 '16 at 09:31
  • The available sources list is empty – K Vinay Mar 17 '16 at 09:41
  • What is your Qt version and may be it helps [link](http://stackoverflow.com/questions/21604569/qt5-2-1-get-gps-location-on-samsung-galaxy-s2plus-i9105p) – Alexander Chernin Mar 17 '16 at 09:45
  • its Qt 5.4.1, checked the link but i'm using 5.4.1 – K Vinay Mar 17 '16 at 09:47
  • Temporary replace QCoreApplication with QApplication and check again availableSources – Alexander Chernin Mar 17 '16 at 09:50
  • Temporary changing the QCoreApplication to QApplication caused this error and application crashed, F/ (30850): (null):0 ((null)): This application failed to start because it could not find or load the Qt platform plugin "android". F/ (30850): F/ (30850): Reinstalling the application may fix this problem. F/libc (30850): Fatal signal 6 (SIGABRT), code -6 in tid 30869 (mycompany.test1) – K Vinay Mar 17 '16 at 14:37
  • Ok. So turn it back into core and solve the issue with empty available sources – Alexander Chernin Mar 17 '16 at 17:22
  • If i do it with empty sources i get error, D/ (28435): (null):0 ((null)): Available Sources: () W/ (28435): (null):0 ((null)): QObject::connect: Cannot connect (null)::positionUpdated(QGeoPositionInfo) to MyClass::positionUpdated(QGeoPositionInfo) F/libc (28435): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 28454 (mycompany.test1) "com.mycompany.test1" died. – K Vinay Mar 18 '16 at 03:22
  • That's all I can help [availableSources is empty](http://www.google.ru/search?q=QGeoPositionInfoSource%3A%3AavailableSources+is+empty&newwindow=1&oq=QGeoPositionInfoSource%3A%3AavailableSources+is+empty&gs_l=mobile-heirloom-serp.12...2473.6415.0.7378.11.10.0.1.1.0.170.664.9j1.10.0....0...1c.1j2.34.mobile-heirloom-serp..10.1.100.7EqdnyHzKb4) – Alexander Chernin Mar 18 '16 at 04:18
  • Thankyou for your help, will see the provided link – K Vinay Mar 18 '16 at 05:29
  • Possible duplicate of [Putting class declaration in .cpp file](https://stackoverflow.com/questions/5657528/putting-class-declaration-in-cpp-file) – jpo38 May 31 '17 at 19:40

0 Answers0