简体   繁体   中英

Pyqt5 mouse event not working for my custom tab bar

So I've been having problems with making a custom tab bar with 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. I've read that it's supposed to connect when the function within the class has a specific function name or something like that? It's quite confusing and my login window seems to not budge. 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

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

I'm trying to get the top bar to be drag-able, not the whole window just the top bar only. 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. That area where the x button lies. 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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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