6

How do I add vertical separators to my statusbar?

(Red arrows) in this screenshot.

enter image description here

And if I success this how can I show selected Line and Column?

(Blue arrows) in same screenshot.

That's for windows.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ForceVII
  • 355
  • 2
  • 16
  • Hi, are you trying to make a text editor? Then please show at least what is the current code you are using for the status bar. – Sid Sep 15 '19 at 12:00
  • Yes, I trying to make text editor. I using `self.statusBar()`. – ForceVII Sep 15 '19 at 12:02
  • And this - https://stackoverflow.com/questions/30371613/draw-vertical-lines-on-qtextedit-in-pyqt#30376201 – Sid Sep 15 '19 at 12:03
  • https://countchu.blogspot.com/2014/04/pyqt-qtextedit-get-current-line-number.html It's in PyQt4 but just replace ```PyQt4``` with ```PyQt5``` everywhere and add ```from PyQt5.QtWidgets import QMainWindow, QApplication, QTextEdit``` – Sid Sep 15 '19 at 12:35
  • Also in line 40 make it ```App = QApplication(sys.argv)``` – Sid Sep 15 '19 at 12:36
  • This is done very easily using the normal [QStatusBar APIs](https://doc.qt.io/qt-5/qstatusbar.html). Just add a [vertical line widget](https://doc.qt.io/qt-5/qframe.html#Shape-enum) to make a separator. – ekhumoro Sep 15 '19 at 12:45

1 Answers1

10

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

Adds the given widget permanently to this status bar, reparenting the widget if it isn't already a child of this QStatusBar object. The stretch parameter is used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space.

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_())

enter image description here

Community
  • 1
  • 1
S. Nick
  • 12,879
  • 8
  • 25
  • 33