3

I've got a strange error when I run my program under Linux. The message says: QApplication::qAppName: Please instantiate QApplication object first. It runs normally under Windows 8, but I want to port it to Linux without this dependency. Here is my code:

#include <QtCore/QCoreApplication>
#include <iostream>
#include <omd/opto.h>
#include <QThread>

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

OptoPorts ports;
OPort* list=ports.listPorts(true);

std::cout<<"Available ports:"<<std::endl;
std::cout<<std::endl;
int i=0;
for (i=0;i<ports.getLastSize();i++)
    std::cout<<"   "<<i+1<<". "<<list[i].name<<" "<<list[i].deviceName<<std::endl;

std::cout<<ports.getLastSize();

std::cout<<std::endl;

if (i==0)
    {
    std::cout<<"No sensor found"<<std::endl;
    return -1;
    }

        int input;
        int timing;

        if (ports.getLastSize()==1)
            input=1;
        else
            std::cin>>input;

        OptoDAQ daq;
        daq.open(list[input-1]);


        while (true)
        {
        if (daq.getVersion()!=_95)
        {
                    OptoPackage* pa=0;

                    int size=daq.readAll(pa);

                    for (int i=0;i<size;i++)
                    {
                    std::cout<<"x: "<<pa[i].x<<" y: "<<pa[i].y<<" z: "<<pa[i].z<<" s1: "<<pa[i].s1<<" s2: "<<pa[i].s2<<" s3: "<<pa[i].s3<<" s4: "<<pa[i].s4<<" TEMP: "<<pa[i].temp<<std::endl;
                    }
        }
        else if (daq.getVersion()==_95)
        {
        OptoPackage6D* p6d=0;
        int size=daq.readAll6D(p6d,false);

        std::cout<<"SIZE:"<<size<<std::endl;
        for (int i=0;i<size;i++)
        {
            std::cout<<"Sensor1.x: "<<p6d[i].Sensor1.x<<" Sensor1.y: "<<p6d[i].Sensor1.y<<" Sensor1.z: "<<p6d[i].Sensor1.z<<" Sensor1.s1: "<<p6d[i].Sensor1.s1<<" Sensor1.s2: "<<p6d[i].Sensor1.s2<<" Sensor1.s3: "<<p6d[i].Sensor1.s3<<"  Sensor1.s4: "<<p6d[i].Sensor1.s4<<" TEMP: "<<p6d[i].Sensor1.temp<<std::endl;
            std::cout<<"Sensor2.x: "<<p6d[i].Sensor2.x<<" Sensor2.y: "<<p6d[i].Sensor2.y<<" Sensor2.z: "<<p6d[i].Sensor2.z<<" Sensor2.s1: "<<p6d[i].Sensor2.s1<<" Sensor2.s2: "<<p6d[i].Sensor2.s2<<" Sensor2.s3: "<<p6d[i].Sensor2.s3<<"  Sensor2.s4: "<<p6d[i].Sensor2.s4<<" TEMP: "<<p6d[i].Sensor2.temp<<std::endl;
            std::cout<<"Sensor3.x: "<<p6d[i].Sensor3.x<<" Sensor3.y: "<<p6d[i].Sensor3.y<<" Sensor3.z: "<<p6d[i].Sensor3.z<<" Sensor3.s1: "<<p6d[i].Sensor3.s1<<" Sensor3.s2: "<<p6d[i].Sensor3.s2<<" Sensor3.s3: "<<p6d[i].Sensor3.s3<<"  Sensor3.s4: "<<p6d[i].Sensor3.s4<<" TEMP: "<<p6d[i].Sensor3.temp<<std::endl;
            std::cout<<"Sensor4.x: "<<p6d[i].Sensor4.x<<" Sensor4.y: "<<p6d[i].Sensor4.y<<" Sensor4.z: "<<p6d[i].Sensor4.z<<" Sensor4.s1: "<<p6d[i].Sensor4.s1<<" Sensor4.s2: "<<p6d[i].Sensor4.s2<<" Sensor4.s3: "<<p6d[i].Sensor4.s3<<"  Sensor4.s4: "<<p6d[i].Sensor4.s4<<" TEMP: "<<p6d[i].Sensor4.temp<<std::endl;
        }
        }

        QThread::msleep(50);
        }

        daq.close();


//return a.exec();
return 0;

}

3
  • Why is the QCoreApplication line commented out? Commented Jan 6, 2015 at 17:48
  • 7
    QCoreApplication is mandatory. If you are only using QThread::msleep from Qt you should consider moving to std::this_thread::sleep_for from C++11 and get rid of your Qt dependency completely. Commented Jan 6, 2015 at 17:51
  • What can I do to ignore QCoreApplication to run my code under Code::Blocks or NetBeans? Commented Jan 7, 2015 at 12:07

1 Answer 1

1

You don't need QCoreApplication if all you're doing is QThread::msleep. I'd suggest using a standard library for sleep, but if you insist, this worked fine for me in both Windows 8.1 and Ubuntu 15.04 in Qt5.

main.cpp:

#include <QThread>

int main()
{
    QThread::sleep(5);
    return 0;
}

Used CMake for configuring on both ends, without changing a thing:

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.11)

find_package(Qt5Core REQUIRED)

add_executable(main main.cpp)
qt5_use_modules(main Core)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.