You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何移除使用statusBar.addPermanentWidget()添加的部件间分隔线?

Removing Separator Lines Between QStatusBar Permanent Widgets

Absolutely, you can ditch those annoying separator lines between widgets added with statusBar.addPermanentWidget()—it’s totally doable, even if it’s not immediately obvious from the basic QStatusBar docs. Here are two reliable ways to pull it off:

1. Use Qt Style Sheets (Quickest Fix)

The easiest approach is to override the default styling for QStatusBar items. Those separators are just default borders applied to each widget in the status bar. A quick style sheet rule will make them disappear:

# For PyQt/PySide (C++ syntax is nearly identical)
self.statusBar().setStyleSheet("""
    QStatusBar::item {
        border: none;
    }
""")

This targets every item in the status bar—permanent widgets, temporary message containers, etc. If you only want to affect permanent widgets (though it’s rarely necessary), you could assign unique objectNames to your widgets and write more specific style rules, but the above one-liner works for most use cases.

2. Wrap Widgets in a Container (Full Layout Control)

If you want more control over spacing and arrangement, skip relying on QStatusBar’s default layout entirely. Instead, create a single container widget, add all your permanent widgets to it with a layout of your choice, then add that container as the only permanent widget in the status bar:

# PyQt/PySide example
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QPushButton

# Create a container and layout
container = QWidget()
layout = QHBoxLayout(container)
layout.setSpacing(10)  # Set your own spacing between widgets (no separators!)
layout.setContentsMargins(0, 0, 0, 0)  # Optional: Remove extra padding

# Add your widgets to the container's layout
widget1 = QLabel("Connected")
widget2 = QPushButton("Settings")
layout.addWidget(widget1)
layout.addWidget(widget2)

# Add the container to the status bar as a single permanent widget
self.statusBar().addPermanentWidget(container)

This way, the status bar only sees one "item" (the container), so no default separators are added. You can tweak the layout’s spacing, alignment, and margins to get exactly the look you want.

Why the Separators Exist

Just to clarify: QStatusBar automatically adds those borders as a default visual cue between multiple items. It’s part of the native style, but since Qt lets you override styles or take over layout control, you’re not stuck with them.

内容的提问来源于stack exchange,提问作者artomason

火山引擎 最新活动