2017-07-14 11 views
1

我正在Qt Creator(Linux)中製作一個項目,我需要按下一個按鈕來運行x-executable文件(C++程序的輸出) 。下面是它的代碼:在Qt中將x可執行文件作爲按鈕事件運行時出錯

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; 
} 
+0

是測試執行的GUI或控制檯應用程序? – eyllanesc

+0

對不起,我不清楚兩者之間的區別。但它是OpenGL CPP程序的輸出文件。我使用文本編輯器(而不是Qt)編寫代碼,並使用命令'g ++ Test.cpp -o -Test -lGL -lGLU -lglut'。 –

+0

您可以共享Test.cpp文件,以便您可以正確測試 – eyllanesc

回答

0

按照documentation

bool QDesktopServices::openUrl(const QUrl & url)

Opens the given url in the appropriate Web browser for the user's desktop environment, and returns true if successful; otherwise returns false.

[...]

openUrl函數試圖通過打開Web瀏覽器文件(鉻,火狐等),但Web瀏覽器無法打開可執行文件。在你的情況下,解決辦法是使用QProcess

QString file = "/home/Test"; 
QProcess::startDetached(file); 
相關問題