栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Qt5 自定义标题栏

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Qt5 自定义标题栏

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_())
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/856388.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号