简体   繁体   English

Pyqt5 鼠标事件不适用于我的自定义标签栏

[英]Pyqt5 mouse event not working for my custom tab bar

So I've been having problems with making a custom tab bar with pyqt5.因此,我在使用 pyqt5 制作自定义标签栏时遇到了问题。 I've seen some examples but when implementing them in they seem to never work for me.我看过一些例子,但是在实施它们时,它们似乎对我不起作用。 Here's my code so far (I just cut out some noted text because it isn't needed):到目前为止,这是我的代码(我只是删掉了一些注释的文本,因为它不是必需的):

from PyQt5 import QtWidgets, uic,QtCore
from PyQt5.QtCore import Qt,QObject,QThread,pyqtSignal,QCoreApplication,QPoint
import PyQt5.QtCore
from PyQt5.QtWidgets import QApplication,QPushButton,QVBoxLayout,QWidget,QFormLayout,QGridLayout,QLabel,QDialog,QHBoxLayout,QDesktopWidget

class LoginWindow(QWidget):
    def __init__(self):
        self.App = QtWidgets.QApplication([])
        self.LoadedApp = uic.loadUi("LoginWindow2.ui")

        self.LoadedApp.setWindowFlags(Qt.FramelessWindowHint)

        self.pressing = False
        self.start = QPoint(0, 0)
        self.center()
        self.oldPos = self.LoadedApp.pos()
        self.LoadedApp.show()
        self.App.exec()

    def mouseReleaseEvent(self, QMouseEvent):
        self.pressing = False

    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()

    def mouseMoveEvent(self, event):
        print("1")
        delta = QPoint (event.globalPos() - self.oldPos)
        #print(delta)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        self.oldPos = event.globalPos()

    def center(self):
        qr = self.LoadedApp.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.LoadedApp.move(qr.topLeft())

LoginWindow = LoginWindow()

The problem here is that the mouse move event seems to not be connecting to the pyqt5 mouse event.这里的问题是鼠标移动事件似乎没有连接到 pyqt5 鼠标事件。 I've read that it's supposed to connect when the function within the class has a specific function name or something like that?我读过它应该在 class 中的 function 具有特定的 function 名称或类似名称时连接? It's quite confusing and my login window seems to not budge.这很令人困惑,我的登录 window 似乎没有让步。 This mouse event thing is very confusing and I tried like two other methods but I ended up deleting the implementation since it made no sense to me.这个鼠标事件非常令人困惑,我尝试了其他两种方法,但我最终删除了实现,因为它对我来说毫无意义。

Here's the download link for the UI: https://cdn.discordapp.com/attachments/516367579598684184/634439342940487680/LoginWindow2.ui这是 UI 的下载链接: https://cdn.discordapp.com/attachments/516367579598684184/634439342940487680/LoginWindow2.ui

And here's what the UI looks like (With the hierarchy on the left focused on the bar QLabel widget): https://cdn.discordapp.com/attachments/516367579598684184/634439663557541918/unknown.png这是 UI 的样子(左侧的层次结构集中在栏 QLabel 小部件上): https://cdn.discordapp.com/attachments/516367579598684184/634439663557541918/unknown.png

I'm trying to get the top bar to be drag-able, not the whole window just the top bar only.我试图让顶部栏可以拖动,而不是整个 window 只是顶部栏。 Any help?有什么帮助吗? If you need additional information, or have questions to what I'm trying to do please ask me and I'll reply to them.如果您需要更多信息,或者对我正在尝试做的事情有疑问,请问我,我会回复他们。

... to get the top bar to be drag-able, not the whole window... ? Sorry, but I didn't understand what you want to drag and drop.抱歉,我不明白您要拖放的内容。 Try it:试试看:

from PyQt5 import QtWidgets, QtCore, uic
from PyQt5.QtCore import Qt, QObject, QThread, pyqtSignal, QCoreApplication, QPoint
from PyQt5.QtWidgets import (QApplication, QPushButton, QVBoxLayout, QWidget,
                             QFormLayout, QGridLayout, QLabel, QDialog, QHBoxLayout,
                             QDesktopWidget, QMainWindow)

class LoginWindow(QMainWindow):                                 # - (QWidget):
    def __init__(self):
        super(LoginWindow, self).__init__()                     # +
#        self.App = QtWidgets.QApplication([])
#        self.LoadedApp = uic.loadUi("LoginWindow2.ui")
        uic.loadUi("LoginWindow2.ui", self)

#        self.LoadedApp.setWindowFlags(Qt.FramelessWindowHint)
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)

        self.pressing = False
        self.start = QPoint(0, 0)
        self.center()
#        self.oldPos = self.LoadedApp.pos()
        self.oldPos = self.pos()              
#        self.LoadedApp.show()
#        self.App.exec()

    def mouseReleaseEvent(self, QMouseEvent):
        self.pressing = False

    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()

    def mouseMoveEvent(self, event):
        delta = QPoint (event.globalPos() - self.oldPos)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        self.oldPos = event.globalPos()

    def center(self):
#        qr = self.LoadedApp.frameGeometry()
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
#        self.LoadedApp.move(qr.topLeft())
        self.move(qr.topLeft())


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = LoginWindow()
    w.show()
    sys.exit(app.exec_())

在此处输入图像描述


Update更新

I still want to make it so the window can be dragged across the screen when you hold click with the mouse being in the QLabel area at the very top of the window which should act as a tab as a result.我仍然想这样做,因此Z05B8CBD96FBF2DE4C1A352702FBF4Z可以在屏幕上拖动,当您握住鼠标位于QLABEL区域的Qlabel区域时,Z05B8C74CBD96FBD96FBF2DE4C1A3527272702FBF4STAS AT AT AT AT AT AS AT ACTAS AT AT AT AT AS AT ACT AT AT ACT AS AT ACT AS AT ACT AS AT ACT AS AT ACT AS AT ACT AS AT ACT AS AT ACT鼠标。 That area where the x button lies. x 按钮所在的区域。 How would I go around doing that?我会怎么做呢?

from PyQt5 import QtWidgets, QtCore, uic
from PyQt5.QtCore import Qt, QObject, QThread, pyqtSignal, QCoreApplication, QPoint
from PyQt5.QtWidgets import (QApplication, QPushButton, QVBoxLayout, QWidget,
                             QFormLayout, QGridLayout, QLabel, QDialog, QHBoxLayout,
                             QDesktopWidget, QMainWindow)

class LoginWindow(QMainWindow):                                 
    def __init__(self):
        super(LoginWindow, self).__init__()                     
        uic.loadUi("LoginWindow2.ui", self)
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
        self.pressing = False
        self.start = QPoint(0, 0)
        self.center()
        self.oldPos = self.pos()              

    def mouseReleaseEvent(self, QMouseEvent):
        self.pressing = False

    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()

    def mouseMoveEvent(self, event):

        if event.pos().y() > 20:                                    # <---
            return                                                  # <---

        delta = QPoint (event.globalPos() - self.oldPos)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        self.oldPos = event.globalPos()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = LoginWindow()
    w.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM