3

I'm implementing a custom widget to use it as a title bar on a dockable window. My problem arises only on Windows, namely, the window border disappears when the dock window is afloat.

Seems the problem is that, on Windows only, the window flags are changed. I.e. when I do this:

   print dock_window.windowFlags()
   dock_window.setTitleBarWidget(title_bar)
   print dock_window.windowFlags()

it prints out different setting for the flags before and after. However, it stays the same on linux and the borders remain unchanged.

My question is, how to restore the window border?

UPDATE: Since the custom title bar overrides the flags for the border when the dock window is floating, how can I edit the dock window so it has some kind of border? (It is crucial for the dock window to have a custom title bar when floating.)

vonPetrushev
  • 5,457
  • 6
  • 39
  • 51

2 Answers2

1

According to this answer this is expected behavior.

From the documentation of setTitleBarWidget:

If a title bar widget is set, QDockWidget will not use native window decorations when it is floated.

So Linux does it the wrong way then?

Anyway as a workaround for Windows I implemented the idea (unsetting the title bar widget before floating) from the answer in PySide/PyQt.

from PySide import QtGui, QtCore

class MyDockWidget(QtGui.QDockWidget):

    def __init__(self, title_widget):
        super().__init__()
        self.title_widget = title_widget
        self.toggle_title_widget(False)
        self.topLevelChanged.connect(self.toggle_title_widget)

    def toggle_title_widget(self, off):
        if off:
            self.setTitleBarWidget(None)
        else:
            self.setTitleBarWidget(self.title_widget)


app = QtGui.QApplication([])

w = QtGui.QMainWindow()
t = QtGui.QLabel('Title')
d = MyDockWidget(t)
w.addDockWidget(QtCore.Qt.LeftDockWidgetArea, d)
w.show()

app.exec_()

At least it keeps the standard decoration when floating.

Community
  • 1
  • 1
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
1

I found this to be an unresolved bug in QT and I don't see this as expected behavior. I found multiple cases of people stumbling on this issue eg1, eg2 and others.

  • Some recommend unsetting and setting the setTitleBarWidget as in Trilarion's answer. This, however, removes the custom title bar and was not ok with me.
  • others recommend setting flags on topLevelChanged event: window.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);. this adds the usual title bar to the dock widget, which again is not what I personally want.
  • the best solution I found is w->setWindowFlags(Qt::Tool|Qt::CustomizeWindowHint);. This uses Qt.CustomizeWindowHint instead of Qt.FramelessWindowHint and does not produce a huge title bar, merely a small bar.

Implementation

from PyQt5.QtCore import Qt
....
def dockfloatevent(isfloating):
    if isfloating:
        dock.setWindowFlags(Qt.Tool | Qt.CustomizeWindowHint)
dock.topLevelChanged.connect(dockfloatevent)

I am not using the most up to date Qt, but from what I can tell this is still an issue? If someone has a Qt account maybe post something to the above bug link? I have already wasted many hours on this and don't feel like pushing it further :|

Mecgrad
  • 64
  • 5