【问题标题】:PyQt5 StatusBar SeparatorsPyQt5 状态栏分隔符
【发布时间】:2020-01-16 12:35:04
【问题描述】:

如何向状态栏添加垂直分隔符?

(Red arrows) 在此屏幕截图中。

如果我成功了,如何显示选定的 LineColumn

(Blue arrows) 在同一张截图中。

这适用于窗户。

【问题讨论】:

  • 你好,你想做一个文本编辑器吗?然后请至少显示您当前用于状态栏的代码是什么。
  • 是的,我正在尝试制作文本编辑器。我使用self.statusBar()
  • countchu.blogspot.com/2014/04/… 它在 PyQt4 中,但只需将 PyQt4 替换为 PyQt5 并添加 from PyQt5.QtWidgets import QMainWindow, QApplication, QTextEdit
  • 同样在第 40 行中使其成为 App = QApplication(sys.argv)

标签: python python-3.x pyqt pyqt5


【解决方案1】:

void QStatusBar::addPermanentWidget(QWidget *widget, int stretch = 0)

将给定的小部件永久添加到此状态栏,如果它还不是此 QStatusBar 对象的子级,则重新设置小部件的父级。当状态栏增长和缩小时,stretch 参数用于计算给定小部件的合适大小。默认拉伸因子为 0,即为小部件提供最小空间。

import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QStatusBar, QLabel, 
                             QPushButton, QFrame)

class VLine(QFrame):
    # a simple VLine, like the one you get from designer
    def __init__(self):
        super(VLine, self).__init__()
        self.setFrameShape(self.VLine|self.Sunken)

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.statusBar().showMessage("bla-bla bla")
        self.lbl1 = QLabel("Label: ")
        self.lbl1.setStyleSheet('border: 0; color:  blue;')
        self.lbl2 = QLabel("Data : ")
        self.lbl2.setStyleSheet('border: 0; color:  red;')
        ed = QPushButton('StatusBar text')

        self.statusBar().reformat()
        self.statusBar().setStyleSheet('border: 0; background-color: #FFF8DC;')
        self.statusBar().setStyleSheet("QStatusBar::item {border: none;}") 
        
        self.statusBar().addPermanentWidget(VLine())    # <---
        self.statusBar().addPermanentWidget(self.lbl1)
        self.statusBar().addPermanentWidget(VLine())    # <---
        self.statusBar().addPermanentWidget(self.lbl2)
        self.statusBar().addPermanentWidget(VLine())    # <---
        self.statusBar().addPermanentWidget(ed)
        self.statusBar().addPermanentWidget(VLine())    # <---
        
        self.lbl1.setText("Label: Hello")
        self.lbl2.setText("Data : 15-09-2019")

        ed.clicked.connect(lambda: self.statusBar().showMessage("Hello "))
        
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

【讨论】:

    猜你喜欢
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2012-06-30
    • 2021-06-26
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    相关资源
    最近更新 更多