使用pyqt4的可拖动窗口

发布于 2024-11-30 22:23:17 字数 1430 浏览 2 评论 0原文

只是一个简单的问题:我正在使用 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()

现在,您会看到我在窗口中放置了一个背景标签。我希望可以通过拖动此标签来在屏幕上拖动窗口。我的意思是:您单击标签,拖动标签,整个窗口就会出现在屏幕上。这可能吗?我也接受非优雅的方式,因为正如您所看到的,我隐藏了标题栏,因此如果我不通过背景标签使其可拖动,则无法拖动窗口。

希望我正确解释了我的问题 非常感谢!!

马泰奥·蒙蒂

just a simple question: I'm using pyqt4 to render a simple window. Here's the code, I post the whole thing so it easier to explain.

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()

Now, you see that I put a background label in my window. I would like that the window could be dragged around the screen by dragging this label. I mean: you click on the label, you drag the label, and the whole window comes around the screen. Is this possible? I accept non-elegant ways as well, because as you can see I hid the title bar so it would be impossible to drag the window if I don't make it draggable via the background label.

Hope I explained my problem properly
Thank you very much!!

Matteo Monti

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗化ㄋ丶相逢 2024-12-07 22:23:17

您可以覆盖 mousePressEvent()mouseMoveEvent() 获取鼠标光标的位置并将小部件移动到该位置。 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)

You can override mousePressEvent() and mouseMoveEvent() to get the location of the mouse cursor and move your widget to that location. mousePressEvent will give you the offset from the cursor position to the top left corner of your widget, and then you can calculate what the new position of the top left corner should be. You can add these methods to your FenixGui class.

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文