QTableWidget 设置列宽模式
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive)
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Fixed)
self.tableWidget.horizontalHeader().setStretchLastSection(True)
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.tableWidget.horizontalHeader().setSectionResizeMode(0, QHeaderView.Interactive)
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.tableWidget.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeToContents)
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.tableWidget.horizontalHeader().setSectionResizeMode(0, QHeaderView.Fixed)
self.tableWidget.setColumnWidth(0, 80)
QTableWidget 设置表格编辑状态
self.tableWidget.setEditTriggers(QAbstractItemView.DoubleClicked)
self.tableWidget.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
self.tableWidget.setCurrentItem(None)
self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tableWidget.setSelectionMode(QAbstractItemView.NoSelection)
self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
QTableWidget 获取选中行索引
row_idx = self.tableWidget.selectedItems()[0].row()
self.tableWidget.removeRow(row_idx)
row_idx = self.tableWidget.selectionModel().selectedRows()[0].row()
self.tableWidget.removeRow(row_idx)
QTableWidget设置某列不可选中、不可编辑
row_count = self.tableWidget.rowCount()
for i in range(row_count):
item = QTableWidgetItem(str(i + 1))
item.setTextAlignment(Qt.AlignCenter)
item.setFlags(QtCore.Qt.ItemIsEnabled)
self.tableWidget.setItem(i, 0, item)
QTableWidget导出内容为CSV文件
import pandas as pd
def save_to_csv(table: QTableWidget, file: str):
"""保存测点信息至CSV文件"""
col_headers = []
for j in range(table.columnCount()):
col_headers.append(table.horizontalHeaderItem(j).text())
df = pd.DataFrame(columns=col_headers)
for row in range(table.rowCount()):
for col in range(table.columnCount()):
item = table.item(row, col)
df.at[row, col_headers[col]] = item.text() if item is not None else ""
df.to_csv(file, index=False)
QTableWidget填充CSV文件内容
import pandas as pd
def read_device_message_from_csv(table: QTableWidget, file: str):
"""从CSV文件读取测点信息"""
table.clear()
df = pd.read_csv(file)
row = df.shape[0]
col = df.shape[1]
table_header = list(df.columns)
table.setColumnCount(col)
table.setRowCount(row)
table.setHorizontalHeaderLabels(table_header)
for i in range(row):
for j in range(col):
table_item = str(df.iloc[i, j])
new_item = QTableWidgetItem(table_item)
new_item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
table.setItem(i, j, new_item)
pass
QTableWidget单元格添加QPushButton等控件
def get_button_widget(slot, text=""):
"""生成包含QLineEdit,QPushButton的QWidget组件"""
widget = QtWidgets.QWidget()
line_edit = QtWidgets.QLineEdit(text)
line_edit.setStyleSheet("QLineEdit{\n"
"margin: 2px 2px 2px 2px;\n"
"background-color:rgba(167, 243, 228, 100);\n"
"border-radius: 5px;\n"
"}\n")
btn = QtWidgets.QPushButton('获取')
btn.setStyleSheet(".QPushButton {\n"
"margin: 2px 2px 2px 2px;\n"
"font-size:18px;\n"
"min-width:90px;\n"
"padding:5px;\n"
"border-radius: 5px;\n"
"background-color: rgba(156, 178, 206, 150);\n"
"}\n"
"\n"
".QPushButton:hover{\n"
"background-color: rgba(109, 125, 144, 200);\n"
"}\n"
"\n"
".QPushButton:pressed{\n"
"background-color: rgb(109, 125, 144);\n"
"}\n")
hbox_layout = QtWidgets.QHBoxLayout()
hbox_layout.addWidget(line_edit)
hbox_layout.addWidget(btn)
hbox_layout.setContentsMargins(4, 0, 4, 0)
widget.setLayout(hbox_layout)
btn.clicked.connect(slot)
return widget
表格某列添加QPushButton等控件
def add_cellwidget(table, col)
for i in range(table.rowCount()):
table.setCellWidget(i, col, get_button_widget(slot))
item = table.item(i, col)
if item is None:
item = QTableWidgetItem('')
table.setItem(i, col, item)
item.setFlags(QtCore.Qt.ItemIsEnabled)
QTableWidget 样式
/*表格的一种美化方式*/
QHeaderView{
/*font-size:18px;*/
background:transparent;
}
QTableCornerButton::section{
background:rgb(126, 145, 176);
}
QHeaderView::section{
color:
background:rgb(126, 145, 176);
border:none;
border-left: 1px solid
text-align:center;
padding-left:2px;
padding-right:2px;
}
QTableWidget{
background:
border:none;
outline:none;
/*font-size:18px;*/
color:
}
QTableWidget::item{
border-left:1px solid
border-bottom:1px solid
}
QTableWidget::item::selected{
color:rgb(0, 0, 0);
background:
}
/* QScrollBar Vertical Style */
QScrollBar::vertical{
background:transparent;
width:12px;
border:none;
}
QScrollBar::add-line:vertical{
background:none;
border:none;
}
QScrollBar::sub-line:vertical{
background:none;
border:none;
}
QScrollBar::add-page:vertical{
background:transparent;
border:none;
}
QScrollBar::sub-page:vertical{
background:transparent;
border:none;
}
QScrollBar::handle:vertical{
background: rgba(111, 134, 147, 100);
border-radius:4px;
}
QScrollBar::handle:vertical:hover{
background: rgba(111, 134, 147, 180);
}
QScrollBar::handle:vertical:pressed{
background: rgba(111, 134, 147, 255);
}
/* QScrollBar Horizontal Style */
QScrollBar::horizontal{
background:transparent;
height:12px;
border:none;
}
QScrollBar::add-line:horizontal{
background:none;
border:none;
}
QScrollBar::sub-line:horizontal{
background:none;
border:none;
}
QScrollBar::add-page:horizontal{
background:transparent;
border:none;
}
QScrollBar::sub-page:horizontal{
background:transparent;
border:none;
}
QScrollBar::handle:horizontal{
background: rgba(111, 134, 147, 100);
border-radius:4px;
}
QScrollBar::handle:horizontal:hover{
background: rgba(111, 134, 147, 180);
}
QScrollBar::handle:horizontal:pressed{
background: rgba(111, 134, 147, 255);
}
未完待续······