underMouse()无法识别QWidget

时间:2018-09-11 23:59:44

标签: python pyqt pyqt5

找不到用于在无框架窗口上创建边缘的任何示例代码,因此,我创建了一个QFrame,并添加了一个跨整个框架的sizegrip以允许调整大小。

sizegrip具有对角箭头光标。我想将其更改为垂直调整大小箭头。但是,使用mouseMoveEvent时,.underMouse()方法无法识别sizegrip。

问题:进入QFrame子小部件时,还能如何触发光标更改?

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(124, 39)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(124, 0))
        MainWindow.setMaximumSize(QtCore.QSize(124, 16777215))
        MainWindow.setMouseTracking(True)

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHeightForWidth(self.centralwidget.sizePolicy().hasHeightForWidth())
        self.centralwidget.setSizePolicy(sizePolicy)
        self.centralwidget.setMinimumSize(QtCore.QSize(124, 0))
        self.centralwidget.setMaximumSize(QtCore.QSize(124, 16777215))
        self.centralwidget.setMouseTracking(True)
        self.centralwidget.setObjectName("centralwidget")

        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")

        self.frame = QtWidgets.QFrame(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
        self.frame.setSizePolicy(sizePolicy)
        self.frame.setMinimumSize(QtCore.QSize(106, 21))
        self.frame.setMaximumSize(QtCore.QSize(106, 16777215))
        self.frame.setMouseTracking(True)
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")

        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.frame)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.verticalLayout.addWidget(self.frame)

        MainWindow.setCentralWidget(self.centralwidget)


class windowMain(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.setupUi(self)

        self.sizegrip = QtWidgets.QSizeGrip(self)
        self.sizegrip.setFixedSize(106,21)
        self.verticalLayout_2.addWidget(self.sizegrip)


    def mouseMoveEvent(self, event):

        # print('I see you mouse.')

        if self.sizegrip.underMouse():
            self.setCursor(QtGui.QCursor(QtCore.Qt.SizeVerCursor))
            # print('I see you mouse.')
        else:
            self.unsetCursor


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    win = windowMain()
    win.setWindowFlags(QtCore.Qt.FramelessWindowHint)
    win.show()
    sys.exit(app.exec_())

0 个答案:

没有答案