QT运行出错
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT运行出错相关的知识,希望对你有一定的参考价值。
我编写了一个 QT 应用程序界面 编译时没有出错 运行时出错了 我这里面也没有空指针啊
下面是 main.cpp
main.cpp
#include <QApplication>
#include <QSplashScreen>
#include "mainwindow.h"
int main(int argc,char **argv)
QApplication app(argc,argv);
QSplashScreen *splash = new QSplashScreen;
// ³ÌÐòÆô¶¯»Ãæ
splash -> setPixmap(QPixmap(":/images/splash.png"));
splash -> show();
Qt::Alignment topRignt = Qt::AlignRight |Qt::AlignTop;
splash -> showMessage(QObject::tr("The author : XuMaoWen"),
topRignt,Qt::white);
splash -> showMessage(QObject::tr("Setting up the main window..."),
topRignt,Qt::white);
MainWindow mainWin ;
splash -> showMessage(QObject::tr("Loading modules..."),
topRignt,Qt::white);
// loadModules();
splash -> showMessage(QObject::tr("Establishing connections..."),
topRignt,Qt::white);
// establishConnections();
mainWin.show();
splash -> finish(&mainWin);
delete splash;
return app.exec();
在 Qt 中将 x 可执行文件作为按钮事件运行时出错
【中文标题】在 Qt 中将 x 可执行文件作为按钮事件运行时出错【英文标题】:Error while Running x-executable files as a button event in Qt 【发布时间】:2017-07-14 14:09:48 【问题描述】:我正在 Qt Creator (Linux) 中创建一个项目,我需要按下一个按钮来运行一个 x 可执行文件(C++ 程序的输出)。这是它的代码:
void MainWindow::on_pushButton_clicked()
QString file = "/home/Test";
QUrl url = QUrl::fromLocalFile(file);
QDesktopServices::openUrl(url);
但是,当我运行项目并使用按钮运行上述Test
文件时,出现以下错误:
gvfs-open: file:///home/Test: error opening location: No application is registered as handling this file
我应该如何解决这个问题?
编辑 1:Test
文件在我双击它或在终端中使用命令 ./Test
时正常运行。
编辑2:原始Test.cpp文件的源代码:
#include<GL/glut.h>
#include<stdio.h>
void myinit()
GLfloat mat_ambient[]=0.3,0.3,0.3,1.0;
GLfloat mat_diffuse[]=0.6,0.6,0.6,1.0;
GLfloat mat_specular[]=0.9,0.9,0.9,1.0;
GLfloat mat_shininess[]=100.0;
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);
GLfloat light0_ambient[]=0.5,0.5,0.5,1.0;
GLfloat light0_diffuse[]=1.0,1.0,1.0,1.0;
GLfloat light0_specular[]=1.0,1.0,1.0,1.0;
GLfloat light0_position[]=5.0,5.0,5.0,0.0;
glLightfv(GL_LIGHT0,GL_AMBIENT,light0_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,light0_specular);
glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE,light0_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
void sphere()
glPushMatrix();
glutSolidSphere(1.0,100,100);
glPopMatrix();
static GLfloat theta[]=0.0,0.0,0.0;
static GLint axis=2.0;
static GLfloat translate[]=0.0,0.0,0.0;
void display()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef( translate[0], translate[1], translate[2]);
glRotatef(theta[0],1.0,0.0,0.0);
glRotatef(theta[1],0.0,1.0,0.0);
glRotatef(theta[2],0.0,0.0,1.0);
sphere();
glFlush();
glutSwapBuffers();
void spinsphere()
theta[axis]+=10.0;
if(theta[axis]>360.0)
theta[axis]-=360.0;
translate[0] += 0.01; // or any value you see fit
translate[1] += 0.01; // or any value you see fit
translate[2] += 0.01; // or any value you see fit
glutPostRedisplay();
void myreshape(int w,int h)
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
int main(int argc,char **argv)
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("TEST");
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
glutIdleFunc(spinsphere);
myinit();
glutMainLoop();
return 0;
【问题讨论】:
Test 可执行文件是 gui 还是控制台应用程序? 对不起,我不清楚两者之间的区别。但它是 OpenGL CPP 程序的输出文件。我使用文本编辑器(不是 Qt)编写了代码,并使用了命令g++ Test.cpp -o -Test -lGL -lGLU -lglut
。
您可以共享Test.cpp文件以便您可以正确测试
在原帖中添加了文件的源代码
【参考方案1】:
根据documentation:
bool QDesktopServices::openUrl(const QUrl & url)
在适当的 Web 浏览器中为用户打开给定的 url 桌面环境,成功则返回true;否则返回 假的。
[...]
openUrl
函数尝试通过 Web 浏览器(chrome、firefox 等)打开文件,但 Web 浏览器无法打开可执行文件。在您的情况下,解决方案是使用 QProcess
:
QString file = "/home/Test";
QProcess::startDetached(file);
【讨论】:
以上是关于QT运行出错的主要内容,如果未能解决你的问题,请参考以下文章