【问题标题】:QPushButton set image, image doesn't apearQPushButton 设置图片,图片不出现
【发布时间】:2017-01-05 19:26:19
【问题描述】:

我正在像这样在 QPushButton 上设置图像(25x25 原始尺寸):icon =

QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("pause.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton = QtGui.QPushButton()
        self.pushButton.setIcon(icon)

也试过这个:

self.pushButton.setIcon(QtGui.QIcon("pause.png"))

还有"/pause.png",没有任何效果。我确定我尝试了更多我在网上看到的东西,并且全部编译但按钮上没有图像。尝试像这样设置图像大小:

self.pushButton.setIconSize(QSize(24, 24))

并且没有变化。 使用pycharm + qt5.7

【问题讨论】:

  • 如果您使用图像作为图标,我强烈建议您使用 Qt 的resource collection files。这不仅使访问文件更容易,而且使您的应用程序更便携。

标签: qt python-3.x user-interface pyqt


【解决方案1】:

您通过提供图像的相对路径来假设进程的当前工作目录。正如您所注意到的,您的假设是错误的。使用引用的绝对路径,例如application's location:

// Python
os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) 
  + QDir.separator() + "pause.png"`
// C++
QCoreApplication.applicationDirPath() + QDir.separator() + "pause.png"`

【讨论】:

  • 你的帮助很大。谢谢
猜你喜欢
  • 2013-10-05
  • 1970-01-01
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多