def query(self, sql): self.table = QTableWidget() self.setCentralWidget(self.table) self.v_layout.addWidget(self.table) # 执行 SQL 查询语句 self.cursor.execute(sql) # 获取查询结果并显示在表格中 rows = self.cursor.fetchall() if rows: self.table.setRowCount(len(rows)) self.table.setColumnCount(len(rows[0])) for i, row in enumerate(rows): for j, col in enumerate(row): item = QTableWidgetItem(str(col)) self.table.setItem(i, j, item) self.table.setHorizontalHeaderLabels(["部门", "姓名", "性别",'职位','工号','状态']) self.table.horizontalHeader().setVisible(True)无法显示内容
时间: 2024-02-15 13:28:26 浏览: 105
这段代码看起来是将数据库查询结果显示在 PyQt5 的 QTableWidget 中。如果您在运行代码时无法在应用程序界面中看到表格的内容,请检查以下几点:
1. 是否正确连接了数据库,且 SQL 查询语句正确执行;
2. 是否正确设置了 QTableWidget 的大小和位置,以及它的父窗口;
3. 是否正确添加了表格的表头信息;
4. 是否正确添加了表格的每一个单元格的内容。
如果您仍然无法解决问题,请提供更多详细信息和完整代码,以便我可以帮助您解决问题。
相关问题
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setFixedSize(800, 600) main_layout = QVBoxLayout() central_widget = QWidget() central_widget.setLayout(main_layout) self.setCentralWidget(central_widget) button_layout = QVBoxLayout() button1 = QPushButton('当日员工工资') button1.setFixedSize(200, 50) button1.clicked.connect(self.show_query1_result) button_layout.addStretch() button_layout.addWidget(button1) button_layout.addStretch() layout = QHBoxLayout() layout.addStretch() layout.addLayout(button_layout) layout.addStretch() widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) main_layout.addLayout(button_layout) self.query1_window = QueryResultWindow() def show_query1_result(self): db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset') cursor = db.cursor() db_sql = """ """ cursor.execute(db_sql) result = cursor.fetchall() db.close() if len(result) == 0: QMessageBox.information(self, "提示", "今日无员工工资记录") return self.query1_window.table_widget.setRowCount(0) self.query1_window.table_widget.setColumnCount(len(result[0])) self.query1_window.table_widget.setHorizontalHeaderLabels( ["员工ID", "员工姓名", "日期", "领取鸡爪重量(KG)", "效率(每小时KG)", "出成率", "基础工资", "重量奖励", "当日总工资"]) for row_num, row_data in enumerate(result): self.query1_window.table_widget.insertRow(row_num) for col_num, col_data in enumerate(row_data): self.query1_window.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data))) self.query1_window.show() class QueryResultWindow(QWidget): def __init__(self): super().__init__() self.setFixedSize(800, 600) self.table_widget = QTableWidget() self.table_widget.setEditTriggers(QTableWidget.NoEditTriggers) self.table_widget.setSelectionBehavior(QTableWidget.SelectRows) layout = QVBoxLayout() layout.addWidget(self.table_widget) self.setLayout(layout) if __name__ == '__main__': app = QApplication(sys.argv) loginWindow = LoginWindow() loginWindow.show() sys.exit(app.exec_()))数据展示页面怎么设置筛选器按ID筛选结果并展示
你可以在数据展示页面添加一个QLineEdit和一个QPushButton,让用户输入员工ID并点击按钮进行筛选。
具体实现方法如下:
1. 在QueryResultWindow类的初始化函数中添加以下代码来创建QLineEdit和QPushButton:
```
self.id_line_edit = QLineEdit()
self.filter_button = QPushButton('筛选')
self.filter_button.clicked.connect(self.filter_result)
```
2. 在layout中添加这两个控件:
```
layout.addWidget(self.id_line_edit)
layout.addWidget(self.filter_button)
```
3. 添加filter_result函数来执行筛选操作:
```
def filter_result(self):
id = self.id_line_edit.text().strip()
if len(id) == 0:
QMessageBox.warning(self, '提示', '请输入员工ID')
return
db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset')
cursor = db.cursor()
db_sql = f"SELECT * FROM table_name WHERE id = '{id}'"
cursor.execute(db_sql)
result = cursor.fetchall()
db.close()
if len(result) == 0:
QMessageBox.information(self, "提示", "没有找到符合条件的记录")
return
self.table_widget.setRowCount(0)
for row_num, row_data in enumerate(result):
self.table_widget.insertRow(row_num)
for col_num, col_data in enumerate(row_data):
self.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data)))
```
注意替换db_sql中的table_name为你的表名。这个函数会从数据库中查询符合条件的记录并展示在表格中。
4. 修改show_query1_result函数,在最开始添加以下代码,用于展示所有记录:
```
db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset')
cursor = db.cursor()
db_sql = """SELECT * FROM table_name"""
cursor.execute(db_sql)
result = cursor.fetchall()
db.close()
if len(result) == 0:
QMessageBox.information(self, "提示", "今日无员工工资记录")
return
```
注意同样替换db_sql中的table_name为你的表名。这个代码块会在进入数据展示页面时展示所有记录。
通过以上修改,你就可以在数据展示页面添加筛选功能了。
class MainWindow(QMainWindow): def init(self): super().init() # 设置主窗口大小 self.setFixedSize(800, 600) # 创建主窗口布局 main_layout = QVBoxLayout() central_widget = QWidget() central_widget.setLayout(main_layout) self.setCentralWidget(central_widget) # 创建两个竖向按钮 button_layout = QVBoxLayout() button1 = QPushButton('当日员工工资') button1.setFixedSize(200, 50) button1.clicked.connect(self.show_query1_result) button2 = QPushButton('当日鸡爪领取记录') button2.setFixedSize(200, 50) button2.clicked.connect(self.show_query2_result) button3 = QPushButton('查询历史员工工资') button3.setFixedSize(200, 50) button3.clicked.connect(self.show_query3_result) button4 = QPushButton('查询历史鸡爪领取记录') button4.setFixedSize(200, 50) button4.clicked.connect(self.show_query4_result) button_layout.addStretch() button_layout.addWidget(button1) button_layout.addWidget(button2) button_layout.addWidget(button3) button_layout.addWidget(button4) button_layout.addStretch() layout = QHBoxLayout() layout.addStretch() layout.addLayout(button_layout) layout.addStretch() widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) # 将按钮布局添加到主窗口布局中 main_layout.addLayout(button_layout) # 创建两个窗口用于展示查询结果 self.query1_window = QueryResultWindow() self.query2_window = QueryResultWindow() self.query3_window = QueryResultWindow() self.query4_window = QueryResultWindow() def show_query1_result(self): # 查询数据 db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset') cursor = db.cursor() db_sql = """ """ cursor.execute(db_sql) result = cursor.fetchall() db.close() if len(result) == 0: QMessageBox.information(self, "提示", "今日无员工工资记录") return # 清空表格 self.query1_window.table_widget.setRowCount(0) self.query1_window.table_widget.setColumnCount(len(result[0])) self.query1_window.table_widget.setHorizontalHeaderLabels \ (["员工ID", "员工姓名", "日期", "领取鸡爪重量(KG)", "效率(每小时KG)", "出成率", "基础工资", "重量奖励", "当日总工资"]) # 添加数据到表格 for row_num, row_data in enumerate(result): self.query1_window.table_widget.insertRow(row_num) for col_num, col_data in enumerate(row_data): self.query1_window.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data))) # 显示窗口 self.query1_window.show()数据展示页面怎么设置筛选器按ID筛选结果并展示
你可以在数据展示页面添加一个 QLineEdit 和一个 QPushButton,让用户输入员工ID并点击按钮进行筛选。具体实现可以按照以下步骤进行:
1. 在数据展示页面的构造函数中创建 QLineEdit 和 QPushButton,并添加到布局中。
```python
self.filter_edit = QLineEdit()
self.filter_button = QPushButton('筛选')
self.filter_button.clicked.connect(self.filter_result)
button_layout.addWidget(self.filter_edit)
button_layout.addWidget(self.filter_button)
```
2. 创建一个方法 `filter_result` 用于筛选结果。该方法首先获取用户输入的员工ID,然后查询数据库获取符合条件的记录,最后将结果添加到表格中。
```python
def filter_result(self):
employee_id = self.filter_edit.text()
if not employee_id:
return
db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset')
cursor = db.cursor()
db_sql = f"SELECT * FROM employee_salary WHERE employee_id={employee_id}"
cursor.execute(db_sql)
result = cursor.fetchall()
db.close()
if len(result) == 0:
QMessageBox.information(self, "提示", "无符合条件的记录")
return
self.table_widget.setRowCount(0)
for row_num, row_data in enumerate(result):
self.table_widget.insertRow(row_num)
for col_num, col_data in enumerate(row_data):
self.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data)))
```
3. 将 QLineEdit 和 QPushButton 添加到布局中,并显示窗口即可。
完整代码如下:
```python
class QueryResultWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('查询结果')
self.setFixedSize(800, 600)
main_layout = QVBoxLayout()
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
table_widget = QTableWidget()
table_widget.setColumnCount(9)
table_widget.setHorizontalHeaderLabels(["员工ID", "员工姓名", "日期", "领取鸡爪重量(KG)", "效率(每小时KG)", "出成率", "基础工资", "重量奖励", "当日总工资"])
main_layout.addWidget(table_widget)
button_layout = QHBoxLayout()
self.filter_edit = QLineEdit()
self.filter_button = QPushButton('筛选')
self.filter_button.clicked.connect(self.filter_result)
button_layout.addWidget(self.filter_edit)
button_layout.addWidget(self.filter_button)
button_layout.addStretch()
main_layout.addLayout(button_layout)
self.table_widget = table_widget
def filter_result(self):
employee_id = self.filter_edit.text()
if not employee_id:
return
db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset')
cursor = db.cursor()
db_sql = f"SELECT * FROM employee_salary WHERE employee_id={employee_id}"
cursor.execute(db_sql)
result = cursor.fetchall()
db.close()
if len(result) == 0:
QMessageBox.information(self, "提示", "无符合条件的记录")
return
self.table_widget.setRowCount(0)
for row_num, row_data in enumerate(result):
self.table_widget.insertRow(row_num)
for col_num, col_data in enumerate(row_data):
self.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data)))
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
阅读全文