【问题标题】:PyQT Attach/drag one window with another PyQT window... - same programPyQT 用另一个 PyQT 窗口附加/拖动一个窗口... - 相同的程序
【发布时间】:2016-05-17 18:35:36
【问题描述】:

这就是交易...我有 1 个“AA”窗口。我点击按钮,我在它旁边看到另一个“BB”窗口,使用...

    a = str(self.geometry())
    a= a.strip("PyQt4.QtCore.QRect(").strip(")").replace(" ","").split(",")
    UI.setGeometry(int(a[0])+520,int(a[1]),700,900)

现在在新的“BB”窗口中,我想点击显示“链接到主“AA”窗口”的复选框(我可以实现只需要函数/信号来获取鼠标/窗口拖动信息... )。这意味着当我将主“AA”窗口向左移动 100 像素时,我希望我的新“BB”窗口也向左移动......任何人都可以帮助我吗? 我相信我需要鼠标/移动点击信号不确定我在这里迷路了:- (

希望这有某种意义。
大流士

编辑1 再往下走,我也设法得到了一些电线:

def mousePressEvent(self, QMouseEvent):
    print QMouseEvent.pos()

def mouseReleaseEvent(self, QMouseEvent):
    cursor =QtGui.QCursor()
    print cursor.pos()

可悲的是,当我拖动/移动窗口时,我没有任何绳索,也无法判断我是在拖动窗口还是只是在 ui 上移动鼠标...棘手。我有一个替代方案,但它涉及重做框架系统,但这有点痛苦,希望仍有办法解决它......

【问题讨论】:

  • 那是你在那儿进行的一些丑陋、骇人听闻的字符串处理。
  • 是的,很久以前做过,需要重新审视它并正确地做我认为......仍在学习:-)

标签: python pyqt window pyqt4


【解决方案1】:

这是一个如何完成的示例。这在一定程度上取决于您是想要相对运动还是绝对定位(这目前是相对运动)。

基本上,您只需要覆盖主窗口的moveEvent。这通常在用户完成窗口移动时调用。然后您可以决定需要重新定位哪些子窗口。

另外,我添加了一种更好的复制窗口几何图形的方法,不需要超级字符串hackiness。

import sys
import time

from PyQt4 import QtCore, QtGui



class MyWindow(QtGui.QMainWindow):

    def __init__(self):
        super(MyWindow, self).__init__()
        self.ui_lay = QtGui.QVBoxLayout()
        self.ui_widget = QtGui.QWidget(self)
        self.setCentralWidget(self.ui_widget)
        self.ui_widget.setLayout(self.ui_lay)
        self.ui_btn = QtGui.QPushButton('Create Window', self)
        self.ui_lay.addWidget(self.ui_btn)
        self.ui_btn.clicked.connect(self.create_window)
        self.other_window = None

    def create_window(self):
        self.other_window = QtGui.QMainWindow(self)
        self.other_widget = QtGui.QWidget(self.other_window)
        self.other_window.setCentralWidget(self.other_widget)
        self.other_lay = QtGui.QVBoxLayout()
        self.other_widget.setLayout(self.other_lay)
        self.ui_chk = QtGui.QCheckBox('Link', self.other_window)
        self.other_lay.addWidget(self.ui_chk)
        self.other_window.show()
        self.position_other_window()

    def position_other_window(self):
        geo = self.geometry()
        geo.moveLeft(geo.left() + geo.width() + 10)
        self.other_window.setGeometry(geo)


    def moveEvent(self, event):
        super(MyWindow, self).moveEvent(event)
        print 'HERE', time.time()
        if self.other_window and self.ui_chk.isChecked():
            #self.position_other_window()
            diff = event.pos() - event.oldPos()
            geo = self.other_window.geometry()
            geo.moveTopLeft(geo.topLeft() + diff)
            self.other_window.setGeometry(geo)



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    app.exec_()

【讨论】:

  • 天哪,有 moveEvent 信号!啊啊啊谢谢!我知道我对 mousePressEvent 信号的挖掘太深了,我应该寻找其他东西。不管怎样,谢谢!这正是我想要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多