Move a border-less window in PyQt4 (drag)

Source: Internet
Author: User

I searched a lot of articles about the drag of the C ++ version of Qt Without Borders window:

For example, the Qt untitled borderless program drag and change the size of the http://blog.csdn.net/kfbyj/article/details/9284923

There are mainly two methods, but the implementation of PyQt (Python version of Qt) is not found. The following describes the implementation of PyQt4.

 

Method 1: override the mousePressEvent and mouseMoveEvent methods in QWidget/QDialog, and move the window using the move method.

This method is relatively simple, but the defect is that the whole window will be moved in real time during the mouse Press Process, and the real-time re-painting will produce a shadow (due to multiple re-painting ).

 

#!/usr/bin/python  #-*-coding:utf-8-*-from PyQt4.QtGui import *from PyQt4.Qt import *from PyQt4.QtCore import *class AboutUsDialog(QDialog):        def __init__(self, parent=None):        super(AboutUsDialog, self).__init__(parent)        self.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog)            def mousePressEvent(self, event):        if event.button() == Qt.LeftButton:            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()            QApplication.postEvent(self, QEvent(174))            event.accept()    def mouseMoveEvent(self, event):        if event.buttons() == Qt.LeftButton:            self.move(event.globalPos() - self.dragPosition)            event.accept()            if __name__ == '__main__':        import sys    app = QApplication(sys.argv)    aboutus = AboutUsDialog()    aboutus.show()    sys.exit(app.exec_())    

In normal windows Forms, the dotted border is displayed after the mouse is pressed. Only the dotted border is moved.

Method 2: Use winEvent to process messages, and click the event WM_NCHITTEST in the form to simulate the event HTCAPTION when you click the native title bar. In the borderless window, add the isInTitle method to determine whether the mouse position is in the Custom title bar of the window.

This method allows you to drag the mouse in the Custom bar, but you cannot double-click the mouse to maximize switching and restore.

#! /Usr/bin/python #-*-coding: UTF-8-*-from PyQt4.QtGui import * from PyQt4.Qt import * from PyQt4.QtCore import * class AboutUsDialog (QWidget ): def _ init _ (self, parent = None): super (AboutUsDialog, self ). _ init _ (parent) self. setWindowFlags (Qt. frameless=whint | Qt. dialog) def isInTitle (self, xPos, yPos): return yPos <30 class MyApplication (QApplication): def _ init _ (self, args): super (MyApplication, self ). _ init _ (args) def GET_X_LPARAM (self, param): # define LOWORD (l) (WORD) (DWORD_PTR) (l) & 0 xffff )) # define HIWORD (l) (WORD) (DWORD_PTR) (l)> 16) # define GET_X_LPARAM (lp) (int) (short) LOWORD (lp )) # define GET_Y_LPARAM (lp) (int) (short) HIWORD (lp) return param & 0 xffff def GET_Y_LPARAM (self, param ): return param> 16 def winEventFilter (self, msg): if msg. message = 0x84: # WM_NCHITTEST form = self. activeWindow () if form: xPos = self. GET_X_LPARAM (msg. lParam)-form. frameGeometry (). x () yPos = self. GET_Y_LPARAM (msg. lParam)-form. frameGeometry (). y () # The mouse is within the range of the custom title of the form, and the form is used to customize an isInTitle. # if yPos <30 and xPos <456: if not form. isMaximized () and hasattr (form, 'isintitle') and form. isInTitle (xPos, yPos): return True, 0x2 # HTCAPTION return False, 0 if _ name _ = '_ main __': import sys app = MyApplication (sys. argv) aboutus = AboutUsDialog () aboutus. showNormal () sys.exit(app.exe c _())

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.