Как законнектить кнопки в кастомном titlebar
так, чтобы они работали и не закрывали мой кастомный TitleBar
?
Кнопка закрыть должна выполнить функцию закрытия окна (пример: self.close()
)
Кнопка сворачивания: self.showMinimized()
А полного экрана например:
if self.isMaximized():
self.showMinimalized()
else:
self.showMaximized()
Мой код:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.Qt import *
import pyautogui as p
class Ui_tbWidget(object):
def setupUi(self, tbWidget):
tbWidget.setObjectName("tbWidget")
tbWidget.resize(600, 40)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(tbWidget.sizePolicy().hasHeightForWidth())
tbWidget.setSizePolicy(sizePolicy)
tbWidget.setMinimumSize(QtCore.QSize(0, 40))
tbWidget.setMaximumSize(QtCore.QSize(16777215, 40))
self.verticalLayout = QtWidgets.QVBoxLayout(tbWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setSpacing(0)
self.verticalLayout.setObjectName("verticalLayout")
self.tbWidget_2 = QtWidgets.QWidget(tbWidget)
self.tbWidget_2.setMinimumSize(QtCore.QSize(0, 40))
self.tbWidget_2.setMaximumSize(QtCore.QSize(16777215, 40))
self.tbWidget_2.setStyleSheet("QWidget#tbWidget_2{\n"
" background-color: #009688;\n"
"}")
self.tbWidget_2.setObjectName("tbWidget_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.tbWidget_2)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setSpacing(0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.tbWidget_5 = QtWidgets.QWidget(self.tbWidget_2)
self.tbWidget_5.setMinimumSize(QtCore.QSize(0, 40))
self.tbWidget_5.setMaximumSize(QtCore.QSize(16777215, 40))
self.tbWidget_5.setObjectName("tbWidget_5")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.tbWidget_5)
self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_3.setSpacing(0)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.tbLabel_6 = QtWidgets.QLabel('Objection.lol', self.tbWidget_5,
alignment=QtCore.Qt.AlignCenter)
self.tbLabel_6.setObjectName("tbLabel_6")
self.tbLabel_6.setStyleSheet(
"#tbLabel_6 {color: #FFFFFF; font-size: 22px;}")
self.verticalLayout_3.addWidget(self.tbLabel_6)
self.horizontalLayout_2.addWidget(self.tbWidget_5)
self.tbWidget_3 = QtWidgets.QWidget(self.tbWidget_2)
self.tbWidget_3.setMinimumSize(QtCore.QSize(90, 40))
self.tbWidget_3.setMaximumSize(QtCore.QSize(90, 40))
self.tbWidget_3.setStyleSheet("QPushButton{\n"
" background-color:rgba(0, 0, 0, 0);\n"
" color:rgb(255, 255, 255);\n"
" border-radius:1px;\n"
" font-size:18px;\n"
" font-family:dripicons-v2;\n"
"}\n"
"QPushButton:hover{\n"
" background-color:rgb(49, 48, 53);\n"
"}\n"
"QPushButton#tbPushButton_3:hover{\n"
" background-color:rgb(232, 17, 35);\n"
"}\n"
"QPushButton:pressed{\n"
" padding-top:5px;\n"
" padding-left:5px;\n"
"}")
self.tbWidget_3.setObjectName("tbWidget_3")
self.gridLayout = QtWidgets.QGridLayout(self.tbWidget_3)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setSpacing(0)
self.gridLayout.setObjectName("gridLayout")
self.tbPushButton_3 = QtWidgets.QPushButton(self.tbWidget_3)
self.tbPushButton_3.setMinimumSize(QtCore.QSize(30, 30))
self.tbPushButton_3.setMaximumSize(QtCore.QSize(30, 30))
self.tbPushButton_3.setObjectName("tbPushButton_3")
self.gridLayout.addWidget(self.tbPushButton_3, 0, 2, 1, 1)
self.tbPushButton_2 = QtWidgets.QPushButton(self.tbWidget_3)
self.tbPushButton_2.setMinimumSize(QtCore.QSize(30, 30))
self.tbPushButton_2.setMaximumSize(QtCore.QSize(30, 30))
self.tbPushButton_2.setStyleSheet("font-size:13px;")
self.tbPushButton_2.setCheckable(True)
self.tbPushButton_2.setObjectName("tbPushButton_2")
self.gridLayout.addWidget(self.tbPushButton_2, 0, 1, 1, 1)
self.tbPushButton = QtWidgets.QPushButton(self.tbWidget_3)
self.tbPushButton.setMinimumSize(QtCore.QSize(30, 30))
self.tbPushButton.setMaximumSize(QtCore.QSize(30, 30))
self.tbPushButton.setObjectName("tbPushButton")
self.gridLayout.addWidget(self.tbPushButton, 0, 0, 1, 1)
self.horizontalLayout_2.addWidget(self.tbWidget_3)
self.verticalLayout.addWidget(self.tbWidget_2)
self.retranslateUi(tbWidget)
QtCore.QMetaObject.connectSlotsByName(tbWidget)
def retranslateUi(self, tbWidget):
_translate = QtCore.QCoreApplication.translate
tbWidget.setWindowTitle(_translate("tbWidget", "Form"))
self.tbPushButton.setText(_translate("tbWidget", ""))
self.tbPushButton_3.setText(_translate("tbWidget", "9"))
self.tbPushButton_2.setText(_translate("tbWidget", ""))
class TitleBar(QtWidgets.QWidget, Ui_tbWidget):
def __init__(self):
super().__init__()
self.setupUi(self)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.centralwidget = QtWidgets.QWidget()
self.centralwidget.setObjectName("centralwidget")
self.setCentralWidget(self.centralwidget)
self.setGeometry(300, 250, 1440, 900)
def _downloadRequested(item): # QWebEngineDownloadItem
p.alert(f'Download your file at {item.path()}', 'Request to download file', button='OK')
item.accept()
self.browser = QWebEngineView()
self.browser.page().profile().downloadRequested.connect(_downloadRequested)
self.browser.setUrl(QUrl("https://objection.lol"))
self.browser.loadFinished.connect(self.update_title)
#self.height = self.screenRect.height()
#self.width = self.screenRect.width()
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.FramelessWindowHint)
self.setWindowIcon(QIcon('objection.png'))
self.titleBar = TitleBar()
layout = QtWidgets.QVBoxLayout(self.centralwidget)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
layout.addWidget(self.titleBar)
layout.addWidget(self.browser)
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self.oldPos = self.pos()
def mousePressEvent(self, event):
self.oldPos = event.globalPos()
def mouseMoveEvent(self, event):
if event.pos().y() > 40:
return
delta = QPoint (event.globalPos() - self.oldPos)
self.move(self.x() + delta.x(), self.y() + delta.y())
self.oldPos = event.globalPos()
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
def update_title(self):
title = self.browser.page().title()
if title != "Ace Attorney Objection Maker":
self.browser.setUrl(QUrl("https://objection.lol"))
elif title == "Ace Attorney Objection Maker":
self.setWindowTitle(title)
def history(self, event):
if event.key() == QtCore.Qt.Key_F10:
self.url_start = self.browser.history().backItem()
print(self.url_start)
print(self.url_start.url())
def keyPressEvent(self, event):
# если нажата клавиша F11
if event.key() == QtCore.Qt.Key_F11:
# если в полный экран
if self.isFullScreen():
# вернуть прежнее состояние
self.showNormal()
else:
# иначе во весь экран
self.showFullScreen()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())