我在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;
}
答案 0 :(得分:1)
如果您所做的只是QThread :: msleep,那么您不需要QCoreApplication。我建议使用标准库进行睡眠,但如果你坚持认为,在Qt5的Windows 8.1和Ubuntu 15.04中,这对我来说都很好。
main.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)