Qt5 自定义标题栏

Qt5 自定义标题栏,第1张

C++实现

C++实现方式为直接在Qtcreator界面项目中的C++头文件添加如下代码即可:

this->setWindowFlags(Qt::FramelessWindowHint);
python实现

python实现方式为新增一个隐藏标题栏函数,然后在创建界面时调用这个函数:

调用前:

import sys
import img_rc
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(768, 528)
        Form.setStyleSheet("background-color: rgb(227, 227, 227);")
        self.toolButton = QtWidgets.QToolButton(Form)
        self.toolButton.setGeometry(QtCore.QRect(723, 0, 45, 45))
        self.toolButton.setStyleSheet("background-color: rgb(227, 227, 227);")
        self.toolButton.setText("")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/png/叉叉.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.toolButton.setIcon(icon)
        self.toolButton.setIconSize(QtCore.QSize(45, 45))
        self.toolButton.setObjectName("toolButton")
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(230, 110, 311, 71))
        self.label.setStyleSheet("font: italic 28pt \"Cambria\";")
        self.label.setObjectName("label")

        self.retranslateUi(Form)
        self.toolButton.clicked.connect(Form.close) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "Welcome to Qt"))
        
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    widget = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(widget)
    widget.show()
    sys.exit(app.exec_())

调用后:

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


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(768, 528)
        Form.setStyleSheet("background-color: rgb(227, 227, 227);")
        self.toolButton = QtWidgets.QToolButton(Form)
        self.toolButton.setGeometry(QtCore.QRect(723, 0, 45, 45))
        self.toolButton.setStyleSheet("background-color: rgb(227, 227, 227);")
        self.toolButton.setText("")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/png/叉叉.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.toolButton.setIcon(icon)
        self.toolButton.setIconSize(QtCore.QSize(45, 45))
        self.toolButton.setObjectName("toolButton")
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(230, 110, 311, 71))
        self.label.setStyleSheet("font: italic 28pt \"Cambria\";")
        self.label.setObjectName("label")

        self.retranslateUi(Form)
        self.toolButton.clicked.connect(Form.close) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "Welcome to Qt"))

    def setNoTitle(self, Form):
        Form.setWindowFlags(Qt.FramelessWindowHint)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    widget = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setNoTitle(widget)
    ui.setupUi(widget)
    widget.show()
    sys.exit(app.exec_())

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/793323.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存