c++ - 在 Qt 中将 x 可执行文件作为按钮事件运行时出错

标签 c++ linux qt opengl qt5

我正在 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;
}

最佳答案

根据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 浏览器(chrome、firefox 等)打开文件,但 Web 浏览器无法打开可执行文件。在您的情况下,解决方案是使用 QProcess:

QString file = "/home/Test";
QProcess::startDetached(file);

关于c++ - 在 Qt 中将 x 可执行文件作为按钮事件运行时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45105072/

相关文章:

linux - 如何在/etc/fstab 中指定带有空格的标签/路径?

user-interface - Qt4:QAbstractTableModel 拖放,不带 MIME

c++ - Qt Creator 组合框

c++ - 在 C++ 中重载显式构造函数

c++ - "Expected unqualified-id before ' 命名空间 '"错误

c++ - 如何洗牌 std::vector?

c++ - 类型的无效操作数 - C++

linux - cmake 中 'ldd' 命令的任何等效项?

linux - 如何将 fzf 绑定(bind)到 zsh key ?

c++ - 如何使用 Qt 创建暂停/等待功能?