应用程序窗口标题,包含多个主窗口的文件路径

时间:2017-07-15 08:28:02

标签: python qt ubuntu pyqt

我有一个带有两个主窗口的应用程序。我希望两者都有标准的标题,其中包含文件名和应用程序名称。但这很奇怪,因为两个文件都显示文件名,但只有第二个窗口显示应用程序名称。第一个显示" x.py"而第二个" y.py - 我的应用程序"。任何人都知道为什么会这样以及如何解决它?这是一个错误还是预期的行为?

from qtpy.QtWidgets import QApplication, QMainWindow

app = QApplication([])
app.setApplicationDisplayName("My App")
wnd1 = QMainWindow()
wnd2 = QMainWindow()
wnd1.setWindowFilePath("x.py")  # in most cases it shows only "x.py" - this is wrong
wnd2.setWindowFilePath("y.py")  # correctly shows "y.py - My App"
wnd1.show()
wnd2.show()
app.exec_()

在Ubuntu 16.04。,PyQt 5.8.2。

上测试

更新:所以我也发现它表现得非确定性。有时两个应用程序标题都正确有时只有一个。这似乎是一个错误。

1 个答案:

答案 0 :(得分:2)

作为这个可能的bug的解决方法,我将覆盖我的主窗口类的setWindowFilePath()。这将给我带来另一个好处,例如显示完整的文件路径而不仅仅是文件名,并且如果文件是尚未保存或加载的新文件,则表明该文件是未命名的,这也是我想要的。它也适用于改变窗口修改状态。我知道我正在牺牲100%'但是......我可以忍受它。

def setWindowFilePath(self, filePath):
    super(MainWindow, self).setWindowFilePath(filePath)
    if not filePath:
        filePath = "unnamed"
    self.setWindowTitle("{}[*] - {}".format(filePath, qApp.applicationDisplayName()))

也许有人会找到更好的解决方案。