在Qt(PyQt)中导致这种“纸牌”效应的原因是什么?

时间:2017-03-06 05:44:29

标签: python qt pyqt

enter image description here

在拖动小部件时会导致这种“纸牌”效果的原因是什么?当屏幕调整大小或任何变化时它会消失,但它仍然非常烦人。任何帮助将不胜感激!

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QSizePolicy


# Draggable Frame
class Pod(QtGui.QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.__mousePressPos = None
        self.__mouseMovePos = None
        self.setSizePolicy(QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred))
        self.setFrameShadow(QtGui.QFrame.Raised)
        self.setFrameShape(QtGui.QFrame.StyledPanel)
        QtGui.QLabel('Drag Me', self)

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            self.__mousePressPos = event.globalPos()
            self.__mouseMovePos = event.globalPos()

    def mouseMoveEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:
            curr_pos = self.mapToGlobal(self.pos())
            global_pos = event.globalPos()
            diff = global_pos - self.__mouseMovePos
            new_pos = self.mapFromGlobal(curr_pos + diff)
            self.move(new_pos)
            self.__mouseMovePos = global_pos

    def mouseReleaseEvent(self, event):
        if self.__mousePressPos is not None:
            moved = event.globalPos() - self.__mousePressPos
            if moved.manhattanLength() > 3:
                event.ignore()
                return


# Base Widget
class Dock(QtGui.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.set_bg_color()

    # Set background red
    def set_bg_color(self):
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), QtGui.QColor(240, 0, 0, 127))
        self.setPalette(p)


if __name__ == "__main__":
    app = QtGui.QApplication([])
    dock = Dock()
    dock.resize(800, 600)
    pod1 = Pod(dock)
    pod1.move(300, 300)
    dock.show()
    app.exec_()

0 个答案:

没有答案