简体   繁体   English

在Linux下运行没有QtCoreApplication / QCoreApplication的Qt C ++代码(Ubuntu 13.10和14.04)

[英]Running Qt C++ code without QtCoreApplication/QCoreApplication under Linux (Ubuntu 13.10 and 14.04)

I've got a strange error when I run my program under Linux. 当我在Linux下运行程序时,我遇到了一个奇怪的错误。 The message says: QApplication::qAppName: Please instantiate QApplication object first. 消息说:QApplication :: qAppName:请先实例化QApplication对象。 It runs normally under Windows 8, but I want to port it to Linux without this dependency. 它在Windows 8下正常运行,但我想将它移植到没有这种依赖的Linux。 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;

} }

You don't need QCoreApplication if all you're doing is QThread::msleep. 如果您所做的只是QThread :: msleep,则不需要QCoreApplication。 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. 我建议使用标准库进行睡眠,但是如果你坚持,这对我来说在Qt5的Windows 8.1和Ubuntu 15.04中都适用。

main.cpp: main.cpp中:

#include <QThread>

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

Used CMake for configuring on both ends, without changing a thing: 使用CMake进行两端配置,无需更改内容:

CMakeLists.txt: 的CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.11)

find_package(Qt5Core REQUIRED)

add_executable(main main.cpp)
qt5_use_modules(main Core)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM