Files
Feeding_control_system/view/widgets/conveyor_system_widget.py
2025-10-31 18:52:35 +08:00

215 lines
7.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sys
from PySide6.QtWidgets import (
QApplication,
QWidget,
QVBoxLayout,
QHBoxLayout,
QLabel,
QPushButton,
)
from PySide6.QtGui import QPixmap
from PySide6.QtCore import Qt
import resources.resources_rc
# 传送系统控件 (包括传送带 和 料斗)
class ConveyorSystemWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("料斗与传送带界面")
self.setFixedSize(443, 190)
self.init_ui()
self._bind()
def init_ui(self):
# 主布局
main_layout = QVBoxLayout(self)
main_layout.setSpacing(0) # 组件间间距
# main_layout.setContentsMargins(20, 20, 20, 20)
main_layout.setContentsMargins(10, 120, 6, 3)
# 添加料斗组件
self.hopper_widget = self.create_upper_hopper()
# main_layout.addWidget(self.hopper_widget)
self.hopper_widget.setParent(self) # 明确父对象为窗口,确保显示在窗口上
self.hopper_widget.move(190, 7) # 中间的过渡位置
self.hopper_widget.setHidden(True)
# 添加传送带组件
self.conveyor_widget = self.create_conveyor()
main_layout.addWidget(self.conveyor_widget, alignment=Qt.AlignLeft)
# 添加传送带控制按钮
self.btn_layout = self.create_conveyor_buttons()
main_layout.addLayout(self.btn_layout)
def create_upper_hopper(self):
"""创建简化版上位料斗(移除了按钮、文字标签等元素)"""
group = QWidget()
layout = QVBoxLayout(group)
layout.setSpacing(0)
layout.setContentsMargins(0, 0, 0, 0)
# 外框图片
outer_img = ":/icons/images/料斗1.png"
outer_pixmap = QPixmap(outer_img)
if outer_pixmap.isNull():
print(f"警告:图片 {outer_img} 加载失败,请检查路径!")
return group
group.setFixedSize(outer_pixmap.size()) # 设置尺寸, 大小和外框一样
# 背景容器
upper_bg_widget = QWidget()
upper_bg_widget.setFixedSize(outer_pixmap.width(), outer_pixmap.height())
upper_bg_widget.setStyleSheet(
f"background-image: url({outer_img}); "
"background-repeat: no-repeat; "
"background-position: center;"
)
layout.addWidget(upper_bg_widget, alignment=Qt.AlignCenter)
# 内框图片
inner_img = ":/icons/images/料斗2.png"
inner_pixmap = QPixmap(inner_img)
if not inner_pixmap.isNull():
upper_inner_label = QLabel(upper_bg_widget)
upper_inner_label.setPixmap(inner_pixmap)
upper_inner_label.setFixedSize(inner_pixmap.width(), inner_pixmap.height())
upper_inner_label.move(14, 9) # 保持原位置
return group
def create_conveyor(self):
"""创建传送带组件包含左右齿轮group容器背景为传送带图片"""
group = QWidget()
group.setObjectName("conveyorGroup")
# 1. 加载传送带图片设置group的尺寸和背景
conveyor_path = ":/icons/images/传送带.png" # 需要替换为实际的路径
conveyor_pix = QPixmap(conveyor_path)
if conveyor_pix.isNull():
print("警告:传送带图片加载失败!请检查图片路径是否正确!")
else:
# group的尺寸 = 传送带图片尺寸(保证背景图完整显示)
group.setFixedSize(conveyor_pix.size())
group.setStyleSheet(f"""
#conveyorGroup {{
background-image: url({conveyor_path});
background-repeat: no-repeat;
background-position: center;
}}
""")
# 2. 给group设置布局用于放置左右齿轮
layout = QHBoxLayout(group)
layout.setSpacing(0) # 齿轮与容器边缘无间距
# 内边距(根据实际图片调整)
layout.setContentsMargins(3, 3, 2, 4)
# 3. 左侧齿轮直接放在group的布局里层级在背景之上
left_gear = QLabel(group)
left_gear_pix = QPixmap(":/icons/images/传送带齿轮.png")
if left_gear_pix.isNull():
print("警告:左侧齿轮图片加载失败!")
else:
left_gear.setPixmap(left_gear_pix)
left_gear.setFixedSize(left_gear_pix.size())
# 左对齐,让齿轮靠在传送带背景的左端
layout.addWidget(left_gear, alignment=Qt.AlignLeft | Qt.AlignVCenter)
# 4. 右侧齿轮通过addStretch推到最右边
right_gear = QLabel(group)
right_gear_pix = QPixmap(":/icons/images/传送带齿轮.png")
if right_gear_pix.isNull():
print("警告:右侧齿轮图片加载失败!")
else:
right_gear.setPixmap(right_gear_pix)
right_gear.setFixedSize(right_gear_pix.size())
# 右对齐,让齿轮靠在传送带背景的右端
layout.addWidget(right_gear, alignment=Qt.AlignRight | Qt.AlignVCenter)
return group
def create_conveyor_buttons(self):
"""创建传送带控制按钮(左右箭头按钮)"""
layout = QHBoxLayout()
layout.setSpacing(0) # 两个按钮之间的间距
# 左侧按钮
self.left_btn = QPushButton()
self.left_btn.setFixedSize(25, 25)
self.left_btn.setStyleSheet(
"""
QPushButton {
background-image: url(:/icons/images/传送带箭头按钮左1.png);
background-repeat: no-repeat;
background-position: center;
border: none;
}
QPushButton:hover {
background-image: url(:/icons/images/传送带箭头按钮左2.png);
}
QPushButton:pressed {
background-image: url(:/icons/images/传送带箭头按钮左2.png);
}
"""
)
# 右侧按钮
self.right_btn = QPushButton()
self.right_btn.setFixedSize(25, 25)
self.right_btn.setStyleSheet(
"""
QPushButton {
background-image: url(:/icons/images/传送带箭头按钮右1.png);
background-repeat: no-repeat;
background-position: center;
border: none;
}
QPushButton:hover {
background-image: url(:/icons/images/传送带箭头按钮右2.png);
}
QPushButton:pressed {
background-image: url(:/icons/images/传送带箭头按钮右2.png);
}
"""
)
# addStretch调整按钮的位置
layout.addStretch(1)
layout.addWidget(self.left_btn, alignment=Qt.AlignLeft)
layout.addStretch(3)
layout.addWidget(self.right_btn, alignment=Qt.AlignLeft)
layout.addStretch(4)
return layout
def _bind(self):
# self.left_btn.clicked.connect(self.moveHopperBelowMixer)
# self.right_btn.clicked.connect(self.moveHopperToTransition)
pass
# 移动料斗到搅拌机下方 (传送带中间位置)
def moveHopperBelowMixer(self):
self.hopper_widget.move(34, 7) # 搅拌机下方坐标
# 移动料斗到中间过渡的位置
def moveHopperToTransition(self):
"""将料斗移动到中间过渡位置(用于位置切换过程)"""
self.hopper_widget.move(190, 7) # 中间过渡坐标
# 隐藏料斗 (用于传送带中 料斗向右移动完成之后)
def hideHopper(self):
self.hopper_widget.setHidden(True)
# 显示料斗 (用于传送带中 料斗开始向左移动时)
def showHopper(self):
self.hopper_widget.setHidden(False)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ConveyorSystemWidget()
window.show()
sys.exit(app.exec())