1

I am developing a PyQt application for a Windows environment (no cross platform required), in which I need to create custom title bar for a QMainWindow instance. There are suggestion to use self.setWindowFlags(Qt.FramelessWindowHint), but it creates undesired effect of application goes to fullscreen when maximizing it. Actually, there is solution which I believe to be the perfect solution for this case, that is drawing the NCA (Non Client Area) as describe in this page.

At the moment, this is my code:

class MainWindow(QMainWindow, Ui_MainWindow):
    def winEvent(self, msg):
        if msg.message == win32con.WM_NCPAINT:
            self.decorate_window(msg)
            return True, 0
        return super(MainWindow, self).winEvent(msg)

    def decorate_window(self, msg):
        painter = QPainter(self)
        painter.fillRect(self.rect(), QColor(255,0,0))

But it gives this warning, QPainter::begin: Paint device returned engine == 0, type: 1, and there is no drawing occur except of a white titlebar and border.

Any suggestion?

swdev
  • 4,997
  • 8
  • 64
  • 106
  • Painting is only allowed inside the `paintEvent(...)` method as far as i know, however, I'm not familiar with PyQt and i have never used the `winEvent(...)` before, hence this might not be the case for you. – Murat Şeker Jun 30 '14 at 23:40
  • Yes, drawing in `paintEvent` is trivial, but it only lets you draw in client area. What I am looking for is drawing in non client area. I am researching to use `ctypes` for this – swdev Jun 30 '14 at 23:47
  • Does [this](http://www.qtcentre.org/threads/55802-how-to-draw-custom-titlebar-for-QMainWindow) help at all? – Shadow9043 Jul 01 '14 at 06:30
  • Well, not quite. This is helpful : http://www.qtcentre.org/threads/55495-Non-client-area-painting-with-Qt Working on Pythonizing it ;) – swdev Jul 01 '14 at 09:04
  • 1
    I've mirrored that qtcentre post as a more accessible [githubgist](https://gist.github.com/d1manson/51aec9e2395478e775d8)...I'm also looking to make it work with pyqt. – dan-man Oct 02 '15 at 14:51
  • Thoughtful! Thanks dan! – swdev Oct 04 '15 at 13:27
  • I'm still struggling to make this work. I have come across a few other related questions: [a](http://stackoverflow.com/q/26826660/2399799) and [b](http://stackoverflow.com/questions/453069/qt-erase-background-windows-aero-glass) – dan-man Oct 05 '15 at 15:48
  • Try this: http://stackoverflow.com/questions/24582525/how-to-show-clickable-qframe-without-loosing-focus-from-main-window it's already in the answer, but got deleted – swdev Oct 17 '15 at 01:12

0 Answers0