天天看點

04_HUD_Qt_for_Python開發之路2

04_HUD_Qt_for_Python開發之路2

歡迎來到我的部落格,希望這篇文章對你有所幫助,如果覺得不錯,請點贊搜藏哈。

文章目錄

HUD_Qt_for_Python開發之路2

1 設定程式名稱

2 隐藏視窗标題欄

3 設定視窗透明裁剪

4 修改下視窗大小,重新加載

5 搞定網絡子產品

5.1 包含網絡子產品

5.2 初始化UDP Socket

本片我們将正式開始我們HUD儀表的開發工作。這一篇首先要給我們的視窗重新命名為HUD使用代碼如下:

widget.setWindowTitle("HUD"),代碼位置如下圖所示。

04_HUD_Qt_for_Python開發之路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_())

      
04_HUD_Qt_for_Python開發之路2
04_HUD_Qt_for_Python開發之路2
04_HUD_Qt_for_Python開發之路2
04_HUD_Qt_for_Python開發之路2
04_HUD_Qt_for_Python開發之路2
04_HUD_Qt_for_Python開發之路2
# 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_())