QSizeGrip 根本不调整窗口大小,我该如何解决这个问题?

问题描述 投票:0回答:1

我正在尝试创建一个无框窗口以允许自定义标题栏。但是,我遇到了 QSizeGrip 未按预期调整窗口大小的问题。我尝试过将其放置在代码的不同部分,但不幸的是,它并没有被证明有效。我还遇到了一个建议,我需要为小部件创建一个 resizeEvent。我尝试实现这个,但似乎也没有解决问题。有人可以帮助我吗?谢谢。这是代码:

import sys
from PyQt5.QtCore import QPoint, Qt
from PyQt5.QtWidgets import QPushButton, QHBoxLayout, QVBoxLayout, QWidget, QLabel, QDialog, QSizeGrip, QMenuBar, QApplication
class TitleBar(QWidget):
    height = 35
    def __init__(self, parent):
        super(TitleBar, self).__init__()
        self.parent = parent
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)

        self.menu_bar = QMenuBar()
        self.menu_bar.setStyleSheet("""
            color: #fff;
            background-color: #23272A;
            font-size: 14px;
            padding: 4px; 
        """)
        self.menu_file = self.menu_bar.addMenu('File')
        self.menu_file_quit = self.menu_file.addAction('Exit')
        self.menu_file_quit.triggered.connect(self.parent.close)
        self.menu_help = self.menu_bar.addMenu('Help')

        self.layout.addWidget(self.menu_bar)

        self.title = QLabel("Test")
        self.title.setFixedHeight(self.height)
        self.layout.addWidget(self.title, alignment=Qt.AlignCenter)

        title_style = """
                            QLabel {
                                color: #ffffff; /* Text color (white) */
                                background-color: #23272a; /* Background color (dark grey) */
                                font-size: 18px; /* Font size */
                                font-weight: bold; /* Bold font */
                                padding: 8px; /* Padding around the text */
                                text-align: center; /* Horizontally center the text */
                            }
                        """
        self.title.setStyleSheet(title_style)

        self.closeButton = QPushButton(' ')
        self.closeButton.clicked.connect(self.on_click_close)
        self.closeButton.setStyleSheet("""
            background-color: #DC143C;
            border-radius: 10px;
            height: {};
            width: {};
            margin-right: 3px;
            font-weight: bold;
            color: #000;
            font-family: "Webdings";
            qproperty-text: "r";
        """.format(self.height / 1.8, self.height / 1.8))

        self.maxButton = QPushButton(' ')
        self.maxButton.clicked.connect(self.on_click_maximize)
        self.maxButton.setStyleSheet("""
            background-color: #32CD32;
            border-radius: 10px;
            height: {};
            width: {};
            margin-right: 3px;
            font-weight: bold;
            color: #000;
            font-family: "Webdings";
            qproperty-text: "1";
        """.format(self.height / 1.8, self.height / 1.8))

        self.hideButton = QPushButton(' ')
        self.hideButton.clicked.connect(self.on_click_hide)
        self.hideButton.setStyleSheet("""
            background-color: #FFFF00;
            border-radius: 10px;
            height: {};
            width: {};
            margin-right: 3px;
            font-weight: bold;
            color: #000;
            font-family: "Webdings";
            qproperty-text: "0";
        """.format(self.height / 1.8, self.height / 1.8))

        self.layout.addWidget(self.hideButton)
        self.layout.addWidget(self.maxButton)
        self.layout.addWidget(self.closeButton)
        self.setLayout(self.layout)

        self.start = QPoint(0, 0)
        self.pressing = False
        self.maximaze = False

    def resizeEvent(self, QResizeEvent):
        super(TitleBar, self).resizeEvent(QResizeEvent)
        self.title.setFixedWidth(self.parent.width())

    def mousePressEvent(self, event):
        self.start = self.mapToGlobal(event.pos())
        self.pressing = True

    def mouseMoveEvent(self, event):
        if self.pressing:
            self.end = self.mapToGlobal(event.pos())
            self.movement = self.end - self.start
            self.parent.move(self.mapToGlobal(self.movement))
            self.start = self.end

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

    def on_click_close(self):
        self.parent.close()

    def on_click_maximize(self):
        self.maximaze = not self.maximaze
        if self.maximaze:    self.parent.setWindowState(Qt.WindowNoState)
        if not self.maximaze:
            self.parent.setWindowState(Qt.WindowMaximized)

    def on_click_hide(self):
        self.parent.showMinimized()

class StatusBar(QWidget):
    def __init__(self, parent):
        super(StatusBar, self).__init__()
        self.initUI()
        self.showMessage("***Test***")

    def initUI(self):
        self.label = QLabel("Status bar...")
        self.label.setFixedHeight(24)
        self.label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
        self.label.setStyleSheet("""
            background-color: #23272a;
            font-size: 12px;
            padding-left: 5px;
            color: white;
        """)
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.layout.addWidget(self.label)
        self.setLayout(self.layout)

    def showMessage(self, text):
        self.label.setText(text)

class MainWindow(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.setFixedSize(400, 200)
        self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)
        self.setStyleSheet("background-color: #2c2f33;")
        self.setWindowTitle('Test')

        self.title_bar = TitleBar(self)
        self.status_bar = StatusBar(self)
        self.grip = QSizeGrip(self)

        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.layout.addWidget(self.title_bar)
        self.layout.addStretch(1)
        self.layout.addWidget(self.grip, 0, Qt.AlignBottom | Qt.AlignRight)
        self.layout.addWidget(self.status_bar)
        self.layout.setSpacing(0)
        self.setLayout(self.layout)

if __name__=='__main__':
    app = QApplication(sys.argv)
    myWindow = MainWindow()
    myWindow.show()
    sys.exit(app.exec_())
python-3.x pyqt pyqt5
1个回答
0
投票

我想用什么

self.setFixedSize(400, 200)

这太过分了。

尝试一下。 关注

SizeGrip()
课程。


main.py

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *


class TitleBar(QWidget):
    height = 35
    
    def __init__(self, parent):
        super(TitleBar, self).__init__()
        self.parent = parent
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)

        self.menu_bar = QMenuBar()
        self.menu_bar.setStyleSheet("""
            color: #fff;
            background-color: #23272A;
            font-size: 14px;
            padding: 4px; 
        """)
        self.menu_file = self.menu_bar.addMenu('File')
        self.menu_file_quit = self.menu_file.addAction('Exit')
        self.menu_file_quit.triggered.connect(self.parent.close)
        self.menu_help = self.menu_bar.addMenu('Help')
        self.layout.addWidget(self.menu_bar, alignment=Qt.AlignLeft)

        self.title = QLabel("Test", alignment=Qt.AlignCenter)
        self.title.setFixedHeight(self.height)
        self.layout.addWidget(self.title, 1)
        title_style = """
            QLabel {
                color: #ffffff; /* Text color (white) */
                background-color: #23272a; /* Background color (dark grey) */
                font-size: 18px; /* Font size */
                font-weight: bold; /* Bold font */
                padding: 8px; /* Padding around the text */
                text-align: center; /* Horizontally center the text */
            }
        """
        self.title.setStyleSheet(title_style)

        self.closeButton = QPushButton(' ')
        self.closeButton.clicked.connect(self.on_click_close)
        self.closeButton.setStyleSheet("""
            background-color: #DC143C;
            border-radius: 10px;
            height: {};
            width: {};
            margin-right: 3px;
            font-weight: bold;
            color: #000;
            font-family: "Webdings";
            qproperty-text: "r";
        """.format(self.height / 1.8, self.height / 1.8))

        self.maxButton = QPushButton(' ')
        self.maxButton.clicked.connect(self.on_click_maximize)
        self.maxButton.setStyleSheet("""
            background-color: #32CD32;
            border-radius: 10px;
            height: {};
            width: {};
            margin-right: 3px;
            font-weight: bold;
            color: #000;
            font-family: "Webdings";
            qproperty-text: "1";
        """.format(self.height / 1.8, self.height / 1.8))

        self.hideButton = QPushButton(' ')
        self.hideButton.clicked.connect(self.on_click_hide)
        self.hideButton.setStyleSheet("""
            background-color: #FFFF00;
            border-radius: 10px;
            height: {};
            width: {};
            margin-right: 3px;
            font-weight: bold;
            color: #000;
            font-family: "Webdings";
            qproperty-text: "0";
        """.format(self.height / 1.8, self.height / 1.8))

        self.layout.addWidget(self.hideButton)
        self.layout.addWidget(self.maxButton)
        self.layout.addWidget(self.closeButton)
        self.setLayout(self.layout)

        self.start = QPoint(0, 0)
        self.pressing = False
        self.maximaze = False
       
       
# ?    def resizeEvent(self, QResizeEvent):
# ?        super(TitleBar, self).resizeEvent(QResizeEvent)
# ?        self.title.setFixedWidth(self.parent.width())
    
    def mousePressEvent(self, event):
        self.start = self.mapToGlobal(event.pos())
        self.pressing = True

    def mouseMoveEvent(self, event):
        if self.pressing:
            self.end = self.mapToGlobal(event.pos())
            self.movement = self.end - self.start
            self.parent.move(self.mapToGlobal(self.movement))
            self.start = self.end

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

    def on_click_close(self):
        self.parent.close()

    def on_click_maximize(self):
        self.maximaze = not self.maximaze
        if self.maximaze:    
            self.parent.setWindowState(Qt.WindowNoState)
        if not self.maximaze:
            self.parent.setWindowState(Qt.WindowMaximized)

    def on_click_hide(self):
        self.parent.showMinimized()


class StatusBar(QWidget):
    def __init__(self, parent):
        super(StatusBar, self).__init__()
        self.initUI()
        self.showMessage("***Test***")

    def initUI(self):
        self.label = QLabel("Status bar...")
        self.label.setFixedHeight(24)
        self.label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
        self.label.setStyleSheet("""
            background-color: #23272a;
            font-size: 12px;
            padding-left: 5px;
            color: white;
        """)
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.layout.addWidget(self.label)
        self.setLayout(self.layout)

    def showMessage(self, text):
        self.label.setText(text)


class SizeGrip(QSizeGrip):                                                # +++
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent                            
        
        parent.installEventFilter(self)

        self.setFixedSize(25, 25)
        self.polygon = QtGui.QPolygon([
            QPoint(10, 20), 
            QPoint(20, 10), 
            QPoint(20, 20), 
        ])

    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.Resize:
            geo = self.rect()
            geo.moveBottomRight(source.rect().bottomRight())
            self.setGeometry(geo)
        return super().eventFilter(source, event)
        
    def paintEvent(self, event):
        qp = QPainter(self)
        qp.setPen(Qt.white)
        qp.setPen(Qt.white)
        qp.drawPolygon(self.polygon)
    

class MainWindow(QDialog):     
    def __init__(self):
        super().__init__()
        
# ???   self.setFixedSize(400, 200)
        
        self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)
        
        self.setStyleSheet("background-color: #2c2f33;")
        self.setWindowTitle('Test')

        self.title_bar = TitleBar(self)
        self.status_bar = StatusBar(self)
        
        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.layout.addWidget(self.title_bar)
        self.layout.addStretch(1)
#        self.layout.addWidget(self.grip, 0, Qt.AlignBottom | Qt.AlignRight)
        self.layout.addWidget(self.status_bar)
        self.layout.setSpacing(0)
        self.setLayout(self.layout)

        self.grip = SizeGrip(self)                                        # +++ 


if __name__=='__main__':
    app = QApplication(sys.argv)
    myWindow = MainWindow()
    myWindow.resize(400, 200)

    myWindow.show()
    sys.exit(app.exec_())

© www.soinside.com 2019 - 2024. All rights reserved.