0

我开发了一个应用程序(使用 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);
}
4

2 回答 2

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获取资源目录。

请参阅此说明以确定存储该信息的最佳位置(但是您可以使用 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'

...
于 2012-08-20T03:49:05.847 回答
0

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

有关图像的问题已使用资源文件解决,但其他文件(例如声音)也会出现相同的问题,并且相同的方法不适用于该问题。

当应用程序执行时,程序采用我的当前目录作为它的基目录,那么问题就来了。

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

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

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

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

于 2012-08-20T03:27:32.283 回答