27 lines
1.2 KiB
Python
27 lines
1.2 KiB
Python
![]() |
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} 已添加到表格末尾")
|