Qt应用程序不显示图像

时间:2012-08-19 19:06:20

标签: linux qt gnu

我开发了一个应用程序(使用Qt Creator),它使用一些图片作为背景和图标。现在我正在尝试部署它,应用程序已成功安装:

$ make
$ sudo make install

安装目录为/usr/local/ctimer/,二进制文件位于/usr/local/ctimer/ctimer。问题是,当我执行程序时,例如,使用Alt + F2 /usr/local/ctimer/ctimer,图像不会显示。

我注意到如果我从其文件夹中执行程序一切正常:

$ cd /usr/local/ctimer/
$ ./ctimer

可以使用带有上述命令的bash脚本解决问题并将其放在/usr/local/bin/中,但我想知道为什么会出现这种情况。

有什么想法吗?

修改

以下是代码:

void Stopwatch::createMenu()
{
  startAction = new QAction(QIcon("pictures/start.png"), tr("&Start/Continue"), this);
  startAction->setToolTip("Start/Continue stopwatch");
  startAction->setShortcut(Qt::CTRL + Qt::Key_S);
  connect(startAction, SIGNAL(triggered()), this, SLOT(start()));

  pauseAction = new QAction(QIcon("pictures/pause.png"), tr("&Pause"), this);
  pauseAction->setToolTip("Pause stopwatch");
  pauseAction->setShortcut(Qt::CTRL + Qt::Key_P);
  connect(pauseAction, SIGNAL(triggered()), this, SLOT(pause()));

  resetAction = new QAction(QIcon("pictures/reset.png"), tr("&Reset"), this);
  resetAction->setToolTip("Reset stopwatch");
  resetAction->setShortcut(Qt::CTRL + Qt::Key_C);
  connect(resetAction, SIGNAL(triggered()), this, SLOT(reset()));

  countDownAction = new QAction(QIcon("pictures/down.png"), tr("&Countdown"), this);
  countDownAction->setToolTip("Create a custom count down");
  countDownAction->setShortcut(Qt::CTRL + Qt::Key_D);
  connect(countDownAction, SIGNAL(triggered()),
          countDownDialog, SLOT(show()));

  aboutAction = new QAction(QIcon("pictures/about.png"), tr("&About"), this);
  aboutAction->setToolTip("About this timer!");
  aboutAction->setShortcut(Qt::CTRL + Qt::Key_H);
  connect(aboutAction, SIGNAL(triggered()), aboutDialog, SLOT(show()));


  quitAction = new QAction(QIcon("pictures/quit.png"), tr("&Quit"), this);
  quitAction->setToolTip("Quit timer!");
  quitAction->setShortcut(Qt::CTRL + Qt::Key_Q);
  connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));

  contextMenu = new QMenu(this);

  contextMenu->addAction(startAction);
  contextMenu->addAction(pauseAction);
  contextMenu->addAction(resetAction);
  contextMenu->addAction(countDownAction);
  contextMenu->addAction(aboutAction);
  contextMenu->addAction(quitAction);

  this->addAction(startAction);
  this->addAction(pauseAction);
  this->addAction(resetAction);
  this->addAction(countDownAction);
  this->addAction(aboutAction);
  this->addAction(quitAction);
}

2 个答案:

答案 0 :(得分:1)

对于图标,我会像你一样使用资源文件。

如果要将其他文件存储在可执行文件目录(或子目录)中,可以添加从QCoreApplication::applicationDirPath()返回的字符串:

QString prefix = QCoreApplication::applicationDirPath();
QIcon startIcon(prefix + "/pictures/start.png");
startAction = new QAction(startIcon, tr("&Start/Continue"), this);
...

然而,这不符合Linux / Unix文件系统,因为可执行文件可能位于/usr/bin中,并且您无法将资源放在该目录中。您可以将资源存储在其他位置(例如/usr/share/yourapp),并在安装期间将该目录存储在/etc中的某个位置。然后,您的应用程序通过QSettings获取资源目录。

请参阅this note以确定存储该信息的最佳位置(但您可以使用QSettings读取任何文件)。

QSettings settings(QSettings::SystemScope,
                   QCoreApplication::organizationName(),
                   QCoreApplication::applicationName());
// This points to /etc/xdg/MySoft.conf on Unix.

QString prefix = settings.value("resourcesDirectory");
// Inside /etc/xdg/MySoft.conf you have a 'resourcesDirectory'
// entry with value '/usr/share/MySoft/resources'

...

答案 1 :(得分:0)

嗯,通常我不会回答我自己的问题,但由于缺乏回应,这是必要的。

有关图像的问题已经使用资源文件解决了,但是其他文件(例如声音)也会出现同样的问题,并且相同的配方对此不起作用。

执行应用程序时,程序采用我当前的目录作为基本目录,然后问题。

解决方案是创建一个Bash脚本,如下所示:

#!/bin/bash
$ cd /path/to/myprogram/
$ ./myprogram

并将其放入/usr/local/bin//usr/bin/

也许这不是最佳解决方案,但它确实有效。如果有人有更好的解决方案,请分享。

相关问题