gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 12:40:43 30 4
gpt4 key购买 nike

我正在 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'sdesktop environment, and returns true if successful; otherwise returnsfalse.

[...]

openUrl 函数尝试通过 Web 浏览器(chrome、firefox 等)打开文件,但 Web 浏览器无法打开可执行文件。在您的情况下,解决方案是使用 QProcess:

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

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

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com