欢迎来到我的博客,希望这篇文章对你有所帮助,如果觉得不错,请点赞搜藏哈。
文章目录
HUD_Qt_for_Python开发之路2
1 设置程序名称
2 隐藏窗口标题栏
3 设置窗口透明裁剪
4 修改下窗口大小,重新加载
5 搞定网络模块
5.1 包含网络模块
5.2 初始化UDP Socket
HUD_Qt_for_Python开发之路2
1 设置程序名称
本片我们将正式开始我们HUD仪表的开发工作。这一篇首先要给我们的窗口重新命名为HUD使用代码如下:
widget.setWindowTitle("HUD"),代码位置如下图所示。
2 隐藏窗口标题栏
Python在Qt的中API基本还是保持了Qt原有的样子,好多东西还是可以参照的,就比如这个已隐藏窗口的标题栏,在传统C++中,我们使用setWindowFlag(Qt::FramelessWindowHint);在Python中使用setWindowFlag(QtCore.Qt.FramelessWindowHint,True),是不是很相似,现在看下我们整个main.py程序的全貌。
————————————————
版权声明:本文为CSDN博主「DreamLife.」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/z609932088/article/details/119914698
# This Python file uses the following encoding: utf-8 import sys import os from PySide6 import QtCore from PySide6.QtWidgets import QApplication, QWidget from PySide6.QtCore import QFile from PySide6.QtUiTools import QUiLoader class HUD(QWidget): def __init__(self): super(HUD, self).__init__() self.load_ui() def load_ui(self): loader = QUiLoader() path = os.path.join(os.path.dirname(__file__), "hud.ui") ui_file = QFile(path) ui_file.open(QFile.ReadOnly) loader.load(ui_file, self) ui_file.close() if __name__ == "__main__": app = QApplication([]) widget = HUD() widget.setWindowTitle("HUD") #设置标题名称 widget.setWindowFlag(QtCore.Qt.FramelessWindowHint,True) #设置程序隐藏标题栏 widget.show() with open("images.qss","r") as f: _style = f.read() app.setStyleSheet(_style) sys.exit(app.exec_())
# This Python file uses the following encoding: utf-8 import sys import os from PySide6 import QtCore from PySide6 import QtNetwork from PySide6.QtNetwork import QUdpSocket from PySide6.QtWidgets import QApplication, QWidget from PySide6.QtCore import QFile,QObject from PySide6.QtUiTools import QUiLoader class HUD(QWidget): def __init__(self): super(HUD, self).__init__() self.load_ui() self.initSocket() def load_ui(self): loader = QUiLoader() path = os.path.join(os.path.dirname(__file__), "hud.ui") ui_file = QFile(path) ui_file.open(QFile.ReadOnly) loader.load(ui_file, self) ui_file.close() def initSocket(self): udpSocket = QUdpSocket(self) #初始化 udpSocket.bind(6876) #绑定端口 # self.connect(udpSocket,SIGNAL('readyRead()'),self,SLOT('readPendingDatagrams')) udpSocket.readyRead.connect(self.readPendingDatagrams) #新的信号槽编写方式 def readPendingDatagrams(self): while udpSocket.hasPendingDatagrams: datagram = QByteArray() datagram.resize(udpSocket.pendingDatagramSize()) (sender,senderPort) = udpSocket.readDatagram(datagram.data(), datagram.size()) processTheDatagram(datagram) print(datagram) if __name__ == "__main__": app = QApplication([]) widget = HUD() widget.setWindowTitle("HUD") #设置标题名称 widget.setWindowFlag(QtCore.Qt.FramelessWindowHint,True) #设置程序隐藏标题栏 widget.setAttribute(QtCore.Qt.WA_TranslucentBackground,True) #设置窗口透明 widget.show() with open("images.qss","r") as f: _style = f.read() app.setStyleSheet(_style) sys.exit(app.exec_())