PyQt - QWidget - 仅显示标题栏

Posted

技术标签:

【中文标题】PyQt - QWidget - 仅显示标题栏【英文标题】:PyQt - QWidget - Show Title bar only 【发布时间】:2016-12-11 13:14:36 【问题描述】:

我正在我的应用程序中打开多个文件。对于大文件,打开需要几秒钟,因此我显示了一个带有打开文件状态的 QWidget。

主应用程序向 qwidget 传递一个信号,并且标题栏得到更新。

我使用setWindowFlags(Qt.WindowTitleHint) 去掉了最小化/最大化按钮。

    这样做是否正确?

    QWidget 可以只显示标题栏吗?

#This is the method for reading files in the main class
#Some other method passes filenames to this method
def readfiles(self,filenames):
    fopenstins=FileOpenStatusWidget()
    fopenstins.show()
    for i in range(len(filenames)): # Read files one by one
        self.emit(SIGNAL('fopenstsig'),i+1,len(filenames)) 
        #### More Code for reading files#####

class FileOpenStatusWidget(QtGui.QWidget):
    def __init__(self):
        super(FileOpenStatusWidget, self).__init__()
        self.connect(main,SIGNAL('fopenstsig'),self.qwrtt)
        self.layout = QtGui.QVBoxLayout()
        self.status=QtGui.QLabel()
        self.layout.addWidget(self.status) # This does not work -- Window remains blank 
        self.setWindowFlags(Qt.WindowTitleHint)
        self.setLayout(self.layout)


     def qwrtt(self,openedfiles,totalfiles):
         self.status.setText('Opening File '+str(openedfiles)+'/'+str(totalfiles))
         if openedfiles==totalfiles:
            self.hide()
         else:
            self.setWindowTitle(self.status.text())

【问题讨论】:

欢迎来到 SO。可以给个代码sn-p吗? 已更新代码 sn-p...(害怕) 我最初的想法是添加一个进度条,但即使添加了小部件,窗口仍然是空白的。唯一有效的是标题栏。 这个问题是关于拥有一个仅由其标题栏组成的窗口还是关于向 QWidget 添加标签?我认为您上面的代码应该可以工作,您的 Window 保持空白的唯一原因是标签中没有文本,人们会看到。 只是一个问题:反过来,即没有标题栏和框架边框的窗口是否适合您?这可以使用setWindowFlags(QtCore.Qt.FramelessWindowHint) 来实现。 【参考方案1】:

使用标志组合:Qt.CustomizeWindowHint | Qt.WindowTitleHint

窗口图标和关闭按钮由默认添加的Qt.WindowSystemMenuHint 控制。 Qt.CustomizeWindowHint 禁用所有默认提示。

【讨论】:

好的..这去掉了exe图标和关闭按钮,但标题栏下方的空白窗口仍然存在。

以上是关于PyQt - QWidget - 仅显示标题栏的主要内容,如果未能解决你的问题,请参考以下文章

setParent 的 PyQt5 行为以显示没有布局的 QWidget

QWidget 无法在 QMainWindow 实例 PyQt5 上显示

PyQt5 中的 QWebEngineView 和 QWidget

PyQt QWidget 窗口在显示后立即关闭?

Pyqt使用总结

QWidget的PyQt绝对位置