ubuntu QT Creator Fatal IO error 2 (没有那个文件或目录) on X server :0

这几天,碰到一个很奇怪的问题,用QT Creator 编写的程序,在Windows上运行良好;移植到MAC上,也测试运行没有问题。现在计划把程序移植到Ubuntu上。

移植完成之后,编译没有问题(在移植到MAC上时已经把Windows与类Unix的语法不兼容的问题全解决了),然后运行程序,发现程序崩溃,并提示如下错误:


(程序名:88351): Gdk-WARNING ** 程序名:  Fatal IO error 2 (没有那个文件或目录) on X server :0.

查相关资料,有这样的资料:

1)http://m.bubuko.com/infodetail-701169.html

2)https://blog.csdn.net/weixin_30883271/article/details/94776498

3)https://www.cnblogs.com/tibetanmastiff/p/4368755.html

其中博客1和博客3是同一个。这几个技术博客大致一个意思,就是图片资源被多线程抢占了,导致资源不可用。

我自己也试图调用Ubuntu上调用GTK线程的方法,但我发现方法不起作用。因为,我自己的程序是,就这部分运行的,属于单独调用的部分,显然不是多线程的问题。

但这几篇博客有一个提醒,或者启示,就是问题为资源被占用了。

我继续调试,想起一个方法:如果程序出问题,把出问题的地方注掉,看程序是否能运行。

经过多次注释,发现当文件中存在这图形框设置ICon的时候,程序就会出问题。

扫描二维码关注公众号,回复: 11498494 查看本文章

我自己提供了一个简单的Demo:

    //DLGTITLE, STRINFO 和 LOGOPATH为外部信息
    QMessageBox box(QMessageBox::Information, DLGTITLE, STRINFO);
    box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    box.setButtonText(QMessageBox::Yes, QString("是"));
    box.setButtonText(QMessageBox::No, QString("否"));

    QString qstrFileName = LOGOPATH;
    QIcon qIcon(qstrFileName);
    box.setWindowIcon(qIcon);  //当设置Icon的时候,必然会发生上面问题。

    box.exec();

发现当程序setWindowIcon存在的时候,必然发生这个问题;把这句话注掉,程序运行正常。

于是查询更多的资料,有关Ubuntu设置Icon的。

查到资料如下:

1)https://forum.qt.io/topic/102167/how-to-add-qt-application-icon-in-ubuntu/4

2)https://doc.qt.io/qt-5/appicon.html#setting-the-application-icon-on-common-linux-desktops

原话摘抄如下:

“In this section we briefly describe the issues involved in providing icons for applications for two common Linux desktop environments: KDE and GNOME. The core technology used to describe application icons is the same for both desktops, and may also apply to others, but there are details which are specific to each. The main source of information on the standards used by these Linux desktops is freedesktop.org. For information on other Linux desktops please refer to the documentation for the desktops you are interested in."

"If you are developing exclusively for GNOME, you may wish to use the standard set of GNU Build Tools, also described in the relevant section of the GTK+/Gnome Application Development book. This ensures that your icons are installed in the appropriate locations for GNOME."

大致意思是,Linux图形化显示用了两种策略KDE和GNONE,分别执行了不同标准。而显然,Gdk-WARNING属于GNOME;也就是GNOME对设置ICON是有一定限制的。

我自己继续查找了资料,大致给出的结论是:在Ubuntu上和Windows上,资源路径的寻找方式是不同的。

我修改了下资源路径,不再用Ubuntu默认的方法:获得路径法,而采用URL方法,而不是Path方法,发现不再出现这个问题。

我发现使用URL方法成功了,但是QT中的ICON没有成功显示。我发现,在Ubuntu上,图标是不再是在左边,于是,我想了个方法,先把所有有关ICON的方法注掉,程序运行正确。

如何设置正确的ICON,会在以后继续寻找方法。

——————————————————————————————————————————————分割线

经过测试,在Ubuntu上,setWindowIcon仅仅为设置程序的Icon(即在启动器一栏上的图标),而不能如Windows一样,是既设置程序的图片,又设置程序左上角的Icon。

猜你喜欢

转载自blog.csdn.net/wangzhezhilu001/article/details/105516609