Files
Feeding_control_system/view/widgets/system_nav_bar.py

120 lines
4.9 KiB
Python
Raw Normal View History

2025-10-31 18:52:31 +08:00
from PySide6.QtWidgets import QWidget, QLabel, QHBoxLayout, QSpacerItem, QSizePolicy
from PySide6.QtGui import QPixmap, QFont, QPainter
from PySide6.QtCore import Qt, QTimer, QDateTime
import resources.resources_rc
2025-11-01 16:13:14 +08:00
from utils.image_paths import ImagePaths
2025-10-31 18:52:31 +08:00
"""主界面最上方的导航栏"""
2025-10-31 18:52:31 +08:00
# 自定义消息容器, 显示系统消息
class MsgContainer(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(770, 24)
# 加载消息区域背景图
2025-11-01 16:13:14 +08:00
self.bg_pixmap = QPixmap(ImagePaths.SYSTEM_MSG_BACKGROUND) # 替换为实际路径
2025-10-31 18:52:31 +08:00
if self.bg_pixmap.isNull():
print("警告:系统消息背景.png 加载失败")
# 消息区域内部布局(喇叭+文本)
msg_layout = QHBoxLayout(self)
msg_layout.setContentsMargins(0, 0, 0, 0) # 调整内边距,避免内容贴边
msg_layout.setSpacing(3) # 喇叭和文本的间距
# 消息喇叭图标
self.msg_icon = QLabel()
self.msg_icon.setFixedSize(13, 18)
# self.msg_icon.setStyleSheet("background-color:red;")
2025-11-01 16:13:14 +08:00
self.msg_icon.setPixmap(QPixmap(ImagePaths.SYSTEM_MSG_HORN)) # 替换为实际路径
2025-10-31 18:52:31 +08:00
msg_layout.addWidget(self.msg_icon, alignment=Qt.AlignVCenter | Qt.AlignLeft)
# 消息文本
current_time = QDateTime.currentDateTime().toString("hh:mm:ss")
self.msg_text = QLabel(f"{current_time} 开始启动智能浇筑系统")
self.msg_text.setFixedWidth(740)
# self.msg_text.setStyleSheet("color: white; font-size: 14px;background-color:red;") # 文本样式
self.msg_text.setStyleSheet("color: white; font-size: 16px;font-weight:Bold;")
self.msg_text.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
msg_layout.addWidget(self.msg_text)
def paintEvent(self, event):
# 绘制消息区域背景图
super().paintEvent(event) # 确保子控件正常绘制
if self.bg_pixmap.isNull():
return # 图片加载失败则不绘制
painter = QPainter(self)
painter.setRenderHint(QPainter.SmoothPixmapTransform) # 缩放平滑
painter.drawPixmap(self.rect(), self.bg_pixmap)
class SystemNavBar(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# 设置尺寸
self.setFixedSize(1280, 80)
# 1. 加载背景图
2025-11-01 16:13:14 +08:00
self.bg_pixmap = QPixmap(ImagePaths.SYSTEM_MAIN_NAV) # 替换为实际图片路径
2025-10-31 18:52:31 +08:00
if self.bg_pixmap.isNull():
print("警告:背景图加载失败,请检查路径!")
main_layout = QHBoxLayout(self)
main_layout.setContentsMargins(9, 9, 9, 19)
main_layout.setSpacing(100) # 注意左侧的logo+系统标题的容器 和 系统消息的间隔
# 左侧区域logo + 系统标题
left_container = QWidget()
left_container.setFixedSize(400, 53)
left_layout = QHBoxLayout(left_container) # 容器内部的水平布局
left_layout.setContentsMargins(0, 0, 0, 0) # 容器内边距
left_layout.setSpacing(6) # 设置logo和标题之间的间隙为6px
# 系统logo
self.logo = QLabel()
self.logo.setFixedSize(53, 53)
2025-11-01 16:13:14 +08:00
self.logo.setPixmap(QPixmap(ImagePaths.SYSTEM_LOGO))
2025-10-31 18:52:31 +08:00
left_layout.addWidget(self.logo, alignment=Qt.AlignTop)
# 系统总标题
self.title = QLabel()
2025-11-01 16:13:14 +08:00
self.title.setPixmap(QPixmap(ImagePaths.SYSTEM_TOTAL_TITLE))
2025-10-31 18:52:31 +08:00
left_layout.addWidget(self.title, alignment=Qt.AlignCenter)
main_layout.addWidget(left_container, alignment=Qt.AlignTop)
# 中间区域:系统消息(喇叭+文本+背景)
self.msg_container = MsgContainer()
main_layout.addWidget(self.msg_container, alignment=Qt.AlignBottom | Qt.AlignRight)
# 右侧区域:实时时间
self.time_label = QLabel()
self.time_label.setStyleSheet("color: white; font-size: 16px;font-weight:Bold;")
main_layout.addWidget(self.time_label, alignment= Qt.AlignTop | Qt.AlignRight)
# 启动时间更新定时器
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_time)
self.timer.start(1000) # 每秒更新一次
def paintEvent(self, event):
# 2. 调用父类paintEvent
super().paintEvent(event)
# 3. 创建画家对象,绘制背景图
painter = QPainter(self)
painter.setRenderHint(QPainter.SmoothPixmapTransform) # 缩放时平滑
painter.drawPixmap(0, 0, self.bg_pixmap)
def update_time(self):
current_time = QDateTime.currentDateTime().toString("yyyy/MM/dd hh:mm:ss")
self.time_label.setText(current_time)
# 测试代码
if __name__ == "__main__":
import sys
from PySide6.QtWidgets import QApplication, QMainWindow
app = QApplication(sys.argv)
nav_bar = SystemNavBar()
nav_bar.show()
sys.exit(app.exec())