[software development] PyQt5中,同样一份代码,多出来了莫名奇妙的按钮焦点
Tofloor
poster avatar
Indedeve
deepin
Backbone of ecological co-construction group
2024-08-24 01:09
Author

同样一份代码,

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from default import *

class TitleBar(QWidget):
    def __init__(self, parent):
        super(TitleBar, self).__init__()
        self.win = parent
        self.InitializeWindow()

    def InitializeWindow(self):
        self.isPressed = False
        self.setFixedHeight(TITLE_BAR_HEIGHT)
        self.InitializeViews()
        pass

    def InitializeViews(self):
        self.iconLabel = QLabel(self)
        self.titleLabel = QLabel(self)

        #设置标题栏的字体或者样式
        # self.titleLabel.setProperty('class', 'title_style')  # 设置样式表类
        self.titleLabel.setObjectName('titleLabel')

        self.minButton = QPushButton(self)
        # self.minButton.setFocusPolicy(Qt.NoFocus)


        self.restoreButton = QPushButton(self)
        self.closeButton = QPushButton(self)

        self.minButton.setFixedSize(TITLE_BUTTON_SIZE, TITLE_BUTTON_SIZE);
        self.restoreButton.setFixedSize(TITLE_BUTTON_SIZE, TITLE_BUTTON_SIZE);
        self.closeButton.setFixedSize(TITLE_BUTTON_SIZE, TITLE_BUTTON_SIZE);

        #设置logo和标题尺寸,Fixed为固定
        self.iconLabel.setFixedSize(TITLE_LABEL_SIZE, TITLE_LABEL_SIZE);
        # self.titleLabel.setFixedHeight(TITLE_LABEL_SIZE);

        self.iconLabel.setAlignment(Qt.AlignCenter);
        self.titleLabel.setAlignment(Qt.AlignLeft|Qt.AlignVCenter);

        self.minButton.setIcon(QIcon(TITLE_MIN_ICON));
        self.restoreButton.setIcon(QIcon(TITLE_RESTORE_ICON));
        self.closeButton.setIcon(QIcon(TITLE_CLS_ICON));

        #信号槽连接
        self.minButton.clicked.connect(self.ShowMininizedWindow)
        self.restoreButton.clicked.connect(self.ShowRestoreWindow)
        self.closeButton.clicked.connect(self.CloseWindow)

        self.lay = QHBoxLayout(self)
        self.setLayout(self.lay)

        self.lay.setSpacing(0)
        self.lay.setContentsMargins(0, 0, 0, 0)

        # 创建一个占位控件,并设置其最小宽度为20px
        # spacer = QWidget(self)
        # spacer.setFixedWidth(20)  # 这将使得图标向右移动20px
        # spacer.setObjectName('Spacer') 


        # self.lay.addWidget(spacer) 

        self.lay.addWidget(self.iconLabel)
        self.lay.addWidget(self.titleLabel)
        self.lay.addWidget(self.minButton)
        self.lay.addWidget(self.restoreButton)
        self.lay.addWidget(self.closeButton)

        # default_style = self.minButton.styleSheet()
        default_style_name = QStyleFactory.keys()[0]  # 假设 QPushButton 使用第一个样式
  
        # # 打印按钮的默认样式表
        # print("打印样式表")
        # # print(str(default_style))
        # print(f'Default style name for QPushButton: {default_style_name}')
    def ShowMininizedWindow(self):
        self.win.showMinimized()

    def ShowMaximizedWindow(self):
        self.win.showMaximized()

    def ShowRestoreWindow(self):
        if self.win.isMaximized():
            self.win.showNormal()
        else:
            self.win.showMaximized()

    def CloseWindow(self):
        self.win.close()

    def SetTitle(self, str):
        self.titleLabel.setText(str)

    def SetIcon(self, pix):
        self.iconLabel.setPixmap(pix.scaled(self.iconLabel.size() - QSize(TITLE_ICON_MAG, TITLE_ICON_MAG)))

    def mouseDoubleClickEvent(self, event):
        self.ShowRestoreWindow()
        return QWidget().mouseDoubleClickEvent(event)

    def mousePressEvent(self, event):
        self.isPressed = True
        self.startPos = event.globalPos()
        return QWidget().mousePressEvent(event)

    def mouseReleaseEvent(self, event):
        self.isPressed = False
        return QWidget().mouseReleaseEvent(event)

    def mouseMoveEvent(self, event):
        if self.isPressed:
            if self.win.isMaximized:
                self.win.showNormal()

            movePos = event.globalPos() - self.startPos
            self.startPos = event.globalPos()
            self.win.move(self.win.pos() + movePos)

        return QWidget().mouseMoveEvent(event)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = TitleBar(None)
    win.show()
    sys.exit(app.exec_())
    pass

写着写着,发现运行效果有点怪,右边的按钮莫名其妙多出来一个蓝色的圆框,

image.png

找半天找不到是什么原因,qss也改过,我注释掉minButton,他这个蓝色框又会出现在中间的"还原"按钮上,如此类推,

当我把代码拷贝到别处运行,却是

image.png

完全没有这个蓝色框!

我排查了好久好久好久之后,发现,我把焦点注释了之后!

就是加上这样一个代码

self.minButton.setFocusPolicy(Qt.NoFocus)

他蓝框没了!没了!

image.png

不过跑到旁边去了,我把三个按钮全都加上之后

三个蓝框总算是消失了,
image.png

到底是为什么?为什么会这样???

还有顺便问一下,为什么我没设置圆角,按钮也变成圆角了?另外一份同样的代码完全没有这样的.

是因为默认有不同的样式吗? 是因为chameleon等等一些基础样式导致的吗?

但是我打印出来的基础样式只有两个啊

Windows
Fusion,

两个都不是这样的情况.


关键是只有在vscode里面F5运行调试的时候才会这样,打包成可执行程序完全没有这个框!

Reply Favorite View the author
All Replies
Indedeve
deepin
Backbone of ecological co-construction group
2024-08-24 01:26
#1

哪怕是dtk的按钮,默认也不应该带焦点才对啊???

Reply View the author
pgxppp
deepin
2024-08-24 10:53
#2

你自己都说了是vscode的问题

Reply View the author
Indedeve
deepin
Backbone of ecological co-construction group
2024-08-24 11:22
#3
pgxppp

你自己都说了是vscode的问题

我的意思是“只有在运行或者调试的时候才会这样,打包成可执行程序完全没有这个框!“

同样的vscode,在另外一份一模一样的代码完全不会这样有蓝框。

Reply View the author