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

标签 c++ linux qt ubuntu

当我在 Linux 下运行我的程序时,我遇到了一个奇怪的错误。消息说:QApplication::qAppName:请首先实例化 QApplication 对象。 它在 Windows 8 下运行正常,但我想将它移植到 Linux 而没有这种依赖。 这是我的代码:

#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;

最佳答案

如果您所做的只是 QThread::msleep,则不需要 QCoreApplication。我建议使用标准库来 sleep ,但如果你坚持,这对我来说在 Qt5 中的 Windows 8.1 和 Ubuntu 15.04 中都很好。

主要.cpp:

#include <QThread>

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

使用 CMake 在两端进行配置,无需更改任何东西:

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.11)

find_package(Qt5Core REQUIRED)

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

关于c++ - 在 Linux(Ubuntu 13.10 和 14.04)下运行没​​有 QtCoreApplication/QCoreApplication 的 Qt C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27804211/

相关文章:

c++ - 使用 clang 编译 std::tuple 时遇到问题

c++ - wchar_t *到char *的转换问题

c++ - QT通过单击菜单栏c++移动整个窗口/应用程序

python - 如何更改标题数据

c++ - 在 C++ 中读/写二进制文件

c++ - 使用 boost::shared_ptr 时有哪些潜在危险?

python boto3 put_item 在使用本地 dynamodb 容器时失败

linux - 用于命令行 GUI 应用程序接口(interface)的 ImageMagick API 到 `display`

java - Eclipse 无法在 Linux Mint 中打开,Java 无法在不同目录中打开

c++ - 如何在QT中组合2个不同的样式表