I want to hide title bar in dockwidget when it is not floated.
dock = QDockWidget()
dock.setTitleBarWidget(QWidget())
dock.titleBarWidget().hide()
this is hide the title bar of dockwidget but when it is floated, It doesn't show title bar
I want to hide title bar in dockwidget when it is not floated.
dock = QDockWidget()
dock.setTitleBarWidget(QWidget())
dock.titleBarWidget().hide()
this is hide the title bar of dockwidget but when it is floated, It doesn't show title bar
You're aware, that when you hide the Title Bar of docked QDockWidget, it is not movable anymore, right?
The matter is not that simple. I ended up on stiching some events together:
I have Arrange (Edit) mode and View (normal) mode. In edit mode the Title bars are visible to allow drag&drop of the panels as requried. A toolbar button takes care of switching the modes. In your case you can asume View mode (arrange mode off) only.
When panel is undocked and form is not set to edit mode, undocked panel's title bar will be reset to None
, resulting in showing the window borders, while the docked panels have hidden Title Bar.
When undocked panels gets docked, if the form is in view mode, the title bar is set to the widget itself, thus hiding it. topLevelChanged
signal is used to trigger the check and change.
The result is that windows are arrangable in many ways, docked or undocked, with clean user experience. Note that while in the example you can set tabs closable, the close buttons are not handled in the example.
Here is fully functional test app:
from PyQt5 import QtCore, QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QMainWindow, QTextEdit, QDockWidget, QToolBar, QTabWidget, QAction, QLayout, QTabBar
import PyQt5.QtWidgets
from PyQt5.QtGui import QIcon, QPixmap
#from PyQt5.QtCore import QMainWindow
_DOCK_OPTS = PyQt5.QtWidgets.QMainWindow.AllowNestedDocks
_DOCK_OPTS |= PyQt5.QtWidgets.QMainWindow.AllowTabbedDocks
class Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle("Test")
tb = self.addToolBar("Test")
self.ShowTitleBars = False
secondQMainWindow = QMainWindow()
self.setTabPosition (Qt.LeftDockWidgetArea, QTabWidget.North)
self.central = secondQMainWindow
self.setDockOptions(_DOCK_OPTS)
self.dw1 = QDockWidget("One")
self.dw1.topLevelChanged.connect(self.HideTitleBar)
self.dw1.setTitleBarWidget(QWidget(self.dw1))
textArea = QTextEdit()
textArea.setText("Text area 1")
self.dw1.setWidget(textArea)
self.dw2 = QDockWidget("Two")
textArea2 = QTextEdit()
textArea2.setText("Text area 2")
self.dw2.setWidget(textArea2)
self.dw2.topLevelChanged.connect(self.HideTitleBar)
self.dw2.setTitleBarWidget(QWidget(self.dw2))
self.addDockWidget(Qt.LeftDockWidgetArea, self.dw1)
self.addDockWidget(Qt.RightDockWidgetArea, self.dw2)
self.tabifyDockWidget(self.dw1, self.dw2)
self.dw3 = QDockWidget("Three")
self.dw3.topLevelChanged.connect(self.HideTitleBar)
self.dw3.setTitleBarWidget(QWidget(self.dw3))
textArea3 = QTextEdit()
textArea3.setText("Text area 3")
self.dw3.setWidget(textArea3)
self.addDockWidget(Qt.LeftDockWidgetArea, self.dw3)
barOn = QAction("Tabs closable",self)
barOn.triggered.connect(self.CountTabWidgetsInTopWindow)
tb.addAction(barOn)
barOff = QAction("Anotherbar",self)
#barOff.triggered.connect(self.ToogleTwo)
tb.addAction(barOff)
barTog = QAction("Toggle Title Bars",self)
barTog.setCheckable(True)
barTog.triggered.connect(self.ToogleTitles2)
tb.addAction(barTog)
def ToogleTwo(self):
self.dw1.setTitleBarWidget(QWidget(self.dw1))
self.dw2.setTitleBarWidget(QWidget(self.dw2))
self.dw3.setTitleBarWidget(QWidget(self.dw3))
print("Test OFF")
def ToogleTitles(self):
#self.dw1.setTitleBarWidget(self.dw1)
self.dw1.setTitleBarWidget(None)
self.dw2.setTitleBarWidget(None)
self.dw3.setTitleBarWidget(None)
print("Test ON")
def SetTabsClosable(self):
for widget in self.children():
if isinstance(widget, QTabBar):
widget.setTabsClosable(True)
def CountTabWidgetsInTopWindow(self):
for widget in self.children():
if isinstance(widget, QTabBar):
widget.setTabsClosable(True)
else:
print("QTabWidget " + widget.objectName() + " -- " + widget.__class__.__name__)
print("Counted.")
def HideTitleBar(self):
dockw = self.sender()
if dockw.isFloating() == False and self.ShowTitleBars == False:
dockw.setTitleBarWidget(QWidget(dockw))
def ToogleTitles2(self):
if self.ShowTitleBars == True:
self.ShowTitleBars = False
else:
self.ShowTitleBars = True
for widget in self.children():
if isinstance(widget, QDockWidget):
if widget.titleBarWidget() == None and self.ShowTitleBars == False:
if widget.isFloating() == False:
widget.setTitleBarWidget(QWidget(widget))
else:
widget.setTitleBarWidget(None)
print("Test Toggle")
if __name__ == '__main__':
import sys
app = PyQt5.QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
app.exec_()
in qt C++, but i think it is the same in py: Initially a floating window will have the following flags: QFlags<Qt::WindowType>(Tool|X11BypassWindowManagerHint|WindowTitleHint|WindowSystemMenuHint|CustomizeWindowHint|WindowCloseButtonHint) You will need to set the Qt::CustomizeWindowH and remove the other flags on the dock widget. Then: You need to set the flags Qt:Tool and respectively the Qt:FramelessWindowHint (depending on the system visu you use - need also the X11 flag to set) QFlags<Qt::WindowType>(Tool|X11BypassWindowManagerHint|FramelessWindowHint)
Setting the flag will need to be done using the method setWindowFlags:https://doc.qt.io/qt-5/qwidget.html#windowFlags-prop
Additionally there is another method: setWindowFlag(flag,bool):https://doc.qt.io/qt-5/qwidget.html#setWindowFlag