python/LaserMethaneProductionTool/tempfile_1738894164834.py
2025-03-14 16:15:51 +08:00

27 lines
1.2 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.

from PyQt5.QtWidgets import QTableWidgetItem
class YourClassName:
def add_item_ser_channel(self, port_name):
row_count = self.tableWidgetSerChannel.rowCount() # 获取表格的行数
# 检查 port_name 是否已经存在于表格中
for i in range(row_count):
item = self.tableWidgetSerChannel.item(i, 2) # 假设 port_name 在第三列索引为2
if item and item.text() == port_name:
print(f"{port_name} 已经存在于表格中")
return
# 找到第一个空白行
for i in range(row_count):
item = self.tableWidgetSerChannel.item(i, 2) # 假设 port_name 在第三列索引为2
if not item or not item.text():
# 在第一个空白行插入 port_name
self.tableWidgetSerChannel.setItem(i, 2, QTableWidgetItem(port_name))
print(f"{port_name} 已添加到表格中")
return
# 如果没有空白行,则在表格末尾添加一行并插入 port_name
self.tableWidgetSerChannel.insertRow(row_count)
self.tableWidgetSerChannel.setItem(row_count, 2, QTableWidgetItem(port_name))
print(f"{port_name} 已添加到表格末尾")