带pyqt4的可拖动窗口

2024-10-01 11:40:25 发布

您现在位置:Python中文网/ 问答频道 /正文

一个简单的问题:我使用pyqt4渲染一个简单的窗口。这是代码,我把所有的东西都贴出来,这样更容易解释。在

from PyQt4 import QtGui, QtCore, Qt
import time
import math

class FenixGui(QtGui.QWidget):

    def __init__(self):
            super(FenixGui, self).__init__()

        # setting layout type
        hboxlayout = QtGui.QHBoxLayout(self)
        self.setLayout(hboxlayout)

        # hiding title bar
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        # setting window size and position
        self.setGeometry(200, 200, 862, 560)
        self.setAttribute(Qt.Qt.WA_TranslucentBackground)
        self.setAutoFillBackground(False)

                # creating background window label
        backgroundpixmap = QtGui.QPixmap("fenixbackground.png")
        self.background = QtGui.QLabel(self)
        self.background.setPixmap(backgroundpixmap)
        self.background.setGeometry(0, 0, 862, 560)


        # fenix logo
        logopixmap = QtGui.QPixmap("fenixlogo.png")
        self.logo = QtGui.QLabel(self)
        self.logo.setPixmap(logopixmap)
        self.logo.setGeometry(100, 100, 400, 150)

def main():

    app = QtGui.QApplication([])
    exm = FenixGui()
    exm.show()
    app.exec_()


if __name__ == '__main__':
    main()

现在,你看到我在窗口里放了一个背景标签。我希望通过拖动这个标签可以在屏幕上拖动窗口。我的意思是:你点击标签,拖动标签,整个窗口就环绕着屏幕。这可能吗?我不可能通过拖拽的方式把标签隐藏起来,因为我不可能把它隐藏起来。在

希望我能解释清楚我的问题 非常感谢你!!在

蒙蒂


Tags: importselfinitmaindef标签qtsetting
1条回答
网友
1楼 · 发布于 2024-10-01 11:40:25

您可以重写mousePressEvent()和{a2}以获取鼠标光标的位置并将小部件移动到该位置。mousePressEvent将为您提供从光标位置到小部件左上角的偏移量,然后您可以计算左上角的新位置。您可以将这些方法添加到您的FenixGui类中。在

def mousePressEvent(self, event):
    self.offset = event.pos()

def mouseMoveEvent(self, event):
    x=event.globalX()
    y=event.globalY()
    x_w = self.offset.x()
    y_w = self.offset.y()
    self.move(x-x_w, y-y_w)

相关问题 更多 >