How to remove the divider between widgets when using `statusBar.addPermanentWidget()`?

2.3k Views Asked by At

Is it possible to remove the divider line between two widgets that were added to the status bar using .addPermanentWidget()? I suspect that it is possible, but I haven't really found any literature on how to proceed.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QStatusBar, QLabel


class MainWindow(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)

        statusBar = QStatusBar()
        self.setStatusBar(statusBar)
        statusBar.addPermanentWidget(QLabel("Label: "))
        statusBar.addPermanentWidget(QLabel("Data"))


app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

enter image description here

2

There are 2 best solutions below

0
artomason On BEST ANSWER

In order to remove the divider between the two elements you need to set the stylesheet for QStatusBar::item in either Qt Creator, or the project source.

Qt Creator Example:

enter image description here

Project Source Example:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QStatusBar, QLabel


class MainWindow(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)

        statusBar = QStatusBar()

        statusBar.setStyleSheet('QStatusBar::item {border: None;}')

        self.setStatusBar(statusBar)
        statusBar.addPermanentWidget(QLabel("Label: "))
        statusBar.addPermanentWidget(QLabel("Data"))


app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
0
imoir On

Another way is to combine several widgets into one to group them, something like the C++ below:

    QWidget *widget = new QWidget;
    QLayout* layout = new QHBoxLayout(widget);
    layout->setMargin(0);

    QLabel *label = new QLabel;
    label->setText("Recording status");
    layout->addWidget(label);

    QLabel *m_RecordingStatus = new QLabel;
    m_RecordingStatus->setFrameShape(QFrame::Shape::Box);
    m_RecordingStatus->setFixedWidth(100);

    layout->addWidget(m_RecordingStatus);
    ui.m_statusBar->addPermanentWidget(widget);

You can group associated widgets to be together between dividers.