6.select_top_查询表顶部多少记录

SQL TOP与DISTINCT用法
本文详细介绍了SQL中TOP和DISTINCT关键字的使用方法,包括如何查询表中前几条记录、按百分比选择记录及结合DISTINCT去除重复记录等技巧。
USE SQLSERVER;
SELECT * FROM EMP;

--筛选顶部的记录或列的记录
--top关键字

--备注:目前不会讲分页,因为子句还没学

--查询表中前5个条数据
    select top 5 * from emp;

--查询数据库中35%前的记录数
    select top 35 percent * from emp;

    select top 15 percent * from emp;

--查询comm不重复数据的前三条记录
    select distinct top 3 comm from emp;

--查询部门,comm不重复数据的前三条记录
    select distinct top 3 deptno, comm from emp;


--错误的搭配
--select distinct top 10 deptno from emp;
--select top 10 distinctdeptno from emp;
--distinct修饰的是整个select查找的结果集

--SELECT语法
SELECT [ ALL | DISTINCT ] [ topSubclause ] aliasedExpr 
       [{ , aliasedExpr }] FROM fromClause [ WHERE whereClause ] [ GROUP BY groupByClause [ HAVING havingClause ] ] [ ORDER BY orderByClause ]


/*
    注意:
        percent:                    是百分比 和 前面值合起来 取上限
        top和distinct一起使用时:    distinct修饰的是结果集,而top修饰结果集的前几条记录
    
    语法:
        select top VALUE (percent) * from table;

    语意:
        select top VALUE (percent) * from table;        
        来自 table 中 顶部记录VALUE条    数据, 进行显示

        例如:
            select distinct top 30 percent comm, deptno from emp;
        含义:
            查询 唯一 来自 emp 中 comm字段, 最后将 顶部的字段 30%(取上限) 进行输出
            
            来自 emp 表 查询 不重复comm, deptno字段组合 前 30%(取上限) 
    
    顺序的问题:
        select 
                3.    distinct(字段组合 F1, F2... )
                4.    top NUMBER percent F1, F2... 
                1.    from TABLE
                2.    where 条件....
    
*/        
                

 

转载于:https://www.cnblogs.com/MrTarget/p/5640244.html

# 顶部操作区域 top_control_layout = QHBoxLayout() top_control_layout.setSpacing(10) # 设置控件间距为5 top_control_layout.setContentsMargins(0, 0, 0, 0) # 设置边距为0 # 名称输入框 self.name_input = QLineEdit() self.name_input.setStyleSheet(self.input_style()) self.name_input.setObjectName("name_input") self.name_input.setPlaceholderText("请输入名称") self.name_input.setMaximumWidth(100) # 部门下拉框 self.department_combo = QComboBox() self.department_combo.setStyleSheet(self.select_btn_style()) self.department_combo.setObjectName("department_combo") self.department_combo.addItems(["全部部门", "技术部", "市场部", "财务部", "人力资源部", "行政部"]) self.department_combo.setMaximumWidth(100) # 导师下拉框 self.mentor_combo = QComboBox() self.mentor_combo.setStyleSheet(self.select_btn_style()) self.mentor_combo.setObjectName("mentor_combo") self.mentor_combo.addItems(["全部导师", "导师A", "导师B", "导师C"]) self.mentor_combo.setMaximumWidth(100) # 状态下拉框 self.status_combo = QComboBox() self.status_combo.setStyleSheet(self.select_btn_style()) self.status_combo.setObjectName("status_combo") self.status_combo.addItems(["全部状态", "进行中", "已完成", "已过期"]) self.status_combo.setMaximumWidth(100) # 查询按钮 self.search_button = QPushButton("查询") self.search_button.setStyleSheet(self.query_btn_style()) self.search_button.setObjectName("search_button") self.search_button.setFixedWidth(80) # 批量提醒按钮(保持原有顺序) self.batch_remind_button = QPushButton("批量提醒") self.batch_remind_button.setStyleSheet(self.notice_btn_style()) self.batch_remind_button.setObjectName("action-btn") self.batch_remind_button.setMaximumWidth(100) top_control_layout.addWidget(self.name_input) top_control_layout.addWidget(self.department_combo) top_control_layout.addWidget(self.mentor_combo) top_control_layout.addWidget(self.status_combo) top_control_layout.addWidget(self.search_button) # 插入水平弹性空间,将后续控件推至右侧 # 弹性空间推至右侧 top_control_layout.addSpacerItem(QSpacerItem( 20, 20, QSizePolicy.Policy.Expanding, # 水平扩展 QSizePolicy.Policy.Minimum # 垂直保持最小 )) # 添加右对齐按钮 top_control_layout.addWidget(self.batch_remind_button) 单独提成一个独立的组件
08-20
import sys from PyQt6.QtCore import Qt, QRect, pyqtSignal from PyQt6.QtGui import QBrush, QColor from PyQt6.QtWidgets import QApplication, QMainWindow from PyQt6.QtWidgets import QHBoxLayout, QLineEdit, QComboBox, QSpacerItem, QSizePolicy from PyQt6.QtWidgets import QTableWidget, QTableWidgetItem, QCheckBox, QPushButton, QHeaderView, QStyleOptionButton, \ QStyle from PyQt6.QtWidgets import QWidget, QVBoxLayout class TopControlBar(QWidget): """顶部控制栏组件""" search_requested = pyqtSignal(str, str, str, str) # 查询信号: name, dept, mentor, status batch_remind_requested = pyqtSignal() # 批量提醒信号 def __init__(self, parent=None): super().__init__(parent) self.init_ui() self.init_connections() def init_ui(self): layout = QHBoxLayout(self) layout.setSpacing(10) layout.setContentsMargins(0, 0, 0, 0) # 名称输入框 self.name_input = QLineEdit() self.name_input.setPlaceholderText("请输入名称") self.name_input.setMaximumWidth(100) # 部门下拉框 self.department_combo = QComboBox() self.department_combo.addItems(["全部部门", "技术部", "市场部", "财务部", "人力资源部", "行政部"]) self.department_combo.setMaximumWidth(100) # 导师下拉框 self.mentor_combo = QComboBox() self.mentor_combo.addItems(["全部导师", "导师A", "导师B", "导师C"]) self.mentor_combo.setMaximumWidth(100) # 状态下拉框 self.status_combo = QComboBox() self.status_combo.addItems(["全部状态", "进行中", "已完成", "已过期"]) self.status_combo.setMaximumWidth(100) # 查询按钮 self.search_button = QPushButton("查询") self.search_button.setFixedWidth(80) # 批量提醒按钮 self.batch_remind_button = QPushButton("批量提醒") self.batch_remind_button.setMaximumWidth(100) # 添加控件到布局 layout.addWidget(self.name_input) layout.addWidget(self.department_combo) layout.addWidget(self.mentor_combo) layout.addWidget(self.status_combo) layout.addWidget(self.search_button) # 添加弹性空间 layout.addSpacerItem(QSpacerItem( 20, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum )) layout.addWidget(self.batch_remind_button) def init_connections(self): """初始化信号连接""" self.search_button.clicked.connect(self.emit_search_signal) self.batch_remind_button.clicked.connect(self.batch_remind_requested.emit) def emit_search_signal(self): """发射查询信号""" name = self.name_input.text() dept = self.department_combo.currentText() mentor = self.mentor_combo.currentText() status = self.status_combo.currentText() self.search_requested.emit(name, dept, mentor, status) def apply_styles(self, input_style, select_style, query_style, notice_style): """应用样式表""" self.name_input.setStyleSheet(input_style) self.department_combo.setStyleSheet(select_style) self.mentor_combo.setStyleSheet(select_style) self.status_combo.setStyleSheet(select_style) self.search_button.setStyleSheet(query_style) self.batch_remind_button.setStyleSheet(notice_style) class CheckBoxHeader(QHeaderView): """自定义表头复选框""" allChecked = pyqtSignal(bool) # 全选信号 def __init__(self, parent=None): super().__init__(Qt.Orientation.Horizontal, parent) self.isChecked = False def paintSection(self, painter, rect, logicalIndex): if logicalIndex == 0: option = QStyleOptionButton() option.rect = QRect(rect.x() + 10, rect.y() + 10, 20, 20) option.state = QStyle.StateFlag.State_Enabled if self.isChecked: option.state |= QStyle.StateFlag.State_On else: option.state |= QStyle.StateFlag.State_Off self.style().drawControl(QStyle.ControlElement.CE_CheckBox, option, painter) else: super().paintSection(painter, rect, logicalIndex) def mousePressEvent(self, event): if event.pos().x() < 50 and event.pos().y() < 50: self.isChecked = not self.isChecked self.updateSection(0) self.allChecked.emit(self.isChecked) # 发射全选信号 super().mousePressEvent(event) class DataTable(QTableWidget): """数据表格组件""" row_selected = pyqtSignal(int, bool) # 行选择信号: row_index, is_checked detail_requested = pyqtSignal(int) # 详情请求信号: row_index def __init__(self, parent=None): super().__init__(parent) self.checkboxes = [] self.init_ui() def init_ui(self): self.setColumnCount(9) self.setHorizontalHeader(CheckBoxHeader(self)) self.setHorizontalHeaderLabels( ["", "姓名", "导师姓名", "部门", "导师邮箱", "第一天", "第一周", "第一月", "操作"]) # 连接表头全选信号 self.horizontalHeader().allChecked.connect(self.update_all_checkboxes) def update_all_checkboxes(self, checked): """更新所有行复选框""" for i, checkbox in enumerate(self.checkboxes): checkbox.setChecked(checked) self.row_selected.emit(i, checked) def populate_data(self, data): """填充表格数据""" self.setRowCount(len(data)) self.checkboxes = [] for row, emp in enumerate(data): self.setRowHeight(row, 50) # 复选框 checkbox = QCheckBox() checkbox.setFixedSize(15, 15) checkbox.stateChanged.connect(lambda state, r=row: self.row_selected.emit(r, bool(state))) self.setCellWidget(row, 0, checkbox) self.checkboxes.append(checkbox) # 填充数据 self.setItem(row, 1, QTableWidgetItem(emp["name"])) self.setItem(row, 2, QTableWidgetItem(emp["account"])) self.setItem(row, 3, QTableWidgetItem(emp["department"])) self.setItem(row, 4, QTableWidgetItem(emp["email"])) # 日期项 item = QTableWidgetItem("07/30") item.setForeground(QBrush(QColor("#4CAF50"))) self.setItem(row, 5, item) item = QTableWidgetItem("08/06") item.setForeground(QBrush(QColor("#F44336"))) self.setItem(row, 6, item) item = QTableWidgetItem("08/30") item.setForeground(QBrush(QColor("#9E9E9E"))) self.setItem(row, 7, item) # 操作按钮 detail_button = QPushButton("查看详情") detail_button.setMaximumWidth(80) detail_button.clicked.connect(lambda _, r=row: self.detail_requested.emit(r)) self.setCellWidget(row, 8, detail_button) def apply_styles(self, table_style): """应用表格样式""" self.setStyleSheet(table_style) self.horizontalHeader().setStretchLastSection(True) class MainTableWidget(QWidget): """主表格界面组件""" def __init__(self, parent=None): super().__init__(parent) self.top_bar = TopControlBar() self.data_table = DataTable() self.init_ui() self.init_connections() self.populate_sample_data() def init_ui(self): layout = QVBoxLayout(self) layout.setContentsMargins(0, 0, 0, 0) # 顶部控制栏 layout.addWidget(self.top_bar) # 数据表格 layout.addWidget(self.data_table) def init_connections(self): """初始化信号连接""" # 顶部控制栏信号 self.top_bar.search_requested.connect(self.handle_search) self.top_bar.batch_remind_requested.connect(self.handle_batch_remind) # 表格信号 self.data_table.row_selected.connect(self.handle_row_selection) self.data_table.detail_requested.connect(self.handle_detail_request) def populate_sample_data(self): """填充示例数据""" employees = [ {"name": "张三", "account": "zhangsan", "department": "技术部", "email": "zhangsan@example.com"}, {"name": "李四", "account": "lisi", "department": "市场部", "email": "lisi@example.com"}, {"name": "王五", "account": "wangwu", "department": "财务部", "email": "wangwu@example.com"}, {"name": "赵六", "account": "zhaoliu", "department": "人力资源部", "email": "zhaoliu@example.com"}, {"name": "孙七", "account": "sunqi", "department": "行政部", "email": "sunqi@example.com"}, ] self.data_table.populate_data(employees) def handle_search(self, name, dept, mentor, status): """处理查询请求""" print(f"查询条件: 姓名={name}, 部门={dept}, 导师={mentor}, 状态={status}") # 实现实际的过滤逻辑 def handle_batch_remind(self): """处理批量提醒请求""" print("执行批量提醒操作") # 实现批量提醒逻辑 def handle_row_selection(self, row, selected): """处理行选择事件""" print(f"行 {row} {'选中' if selected else '消选中'}") def handle_detail_request(self, row): """处理详情查看请求""" print(f"查看第 {row} 行详情") def apply_styles(self): """应用所有样式""" # 定义样式 input_style = """ padding: 8px 12px; border: 1px solid #e6e6e6; border-radius: 5px; font-size: 14px; """ select_style = """ QComboBox { padding: 5px 10px; border: 1px solid #e6e6e6; border-radius: 5px; background-color: white; min-height: 24px; font-size: 14px; } QComboBox:hover { border-color: #4CAF50; } QComboBox::drop-down { border: none; background: transparent; padding-right: 5px; } QComboBox QAbstractItemView { outline: 0px; border: 1px solid #e6e6e6; background-color: white; font-size: 14px; } QComboBox QAbstractItemView::item { padding: 4px 10px; } QComboBox QAbstractItemView::item:selected { background-color: #f0f0f0; color: black; } QComboBox QAbstractItemView::item:hover { background-color: #e0e0e0; } """ query_style = """ background-color: #4564a8; color: white; border: none; padding: 10px; border-radius: 5px; font-size: 14px; """ notice_style = """ background-color: #ffa300; color: white; border: none; padding: 10px; border-radius: 5px; font-size: 14px; """ table_style = """ QTableWidget { border: 1px solid #e6e6e6; border-radius: 2px; background-color: white; font-size: 14px; color: #333; } QHeaderView::section { background-color: #f5f7fa; color: #333; padding: 8px; border: none; font-weight: bold; } QTableWidget::item { padding: 10px; border-bottom: 1px solid #f0f0f0; } QTableWidget::item:selected { background-color: #e5ebee; color: #000; } QTableWidget::item:hover { background-color: #f0f5f9; } """ # 应用样式 self.top_bar.apply_styles(input_style, select_style, query_style, notice_style) self.data_table.apply_styles(table_style) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("模块化表格应用") self.resize(1000, 600) # 创建主表格组件 self.table_widget = MainTableWidget() self.table_widget.apply_styles() # 应用样式 # 设置为主窗口中心部件 self.setCentralWidget(self.table_widget) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) 初始默认 下来框为空 点击批量提醒 则自动将现在下来数据添加下拉框中
最新发布
08-20
import sys from PyQt6.QtGui import QBrush, QColor from PyQt6.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QTableWidget, QTableWidgetItem, QCheckBox, QHeaderView, QStyleOptionButton, QStyle, QLineEdit, QComboBox, QSpacerItem, QSizePolicy) from PyQt6.QtCore import Qt, QRect, pyqtSignal class CheckBoxHeader(QHeaderView): allChecked = pyqtSignal(bool) # 添加全选信号 def __init__(self, parent=None): super().__init__(Qt.Orientation.Horizontal, parent) self.isChecked = False def paintSection(self, painter, rect, logicalIndex): if logicalIndex == 0: option = QStyleOptionButton() option.rect = QRect(rect.x() + 10, rect.y() + 10, 20, 20) option.state = QStyle.StateFlag.State_Enabled if self.isChecked: option.state |= QStyle.StateFlag.State_On else: option.state |= QStyle.StateFlag.State_Off self.style().drawControl(QStyle.ControlElement.CE_CheckBox, option, painter) else: super().paintSection(painter, rect, logicalIndex) def mousePressEvent(self, event): if event.pos().x() < 70 and event.pos().y() < 70: self.isChecked = not self.isChecked self.updateSection(0) self.allChecked.emit(self.isChecked) # 发射全选信号 super().mousePressEvent(event) class TableWidget(QWidget): """独立的表格组件""" def __init__(self): super().__init__() self.checkboxes = [] self.init_ui() self.init_connections() # 添加连接初始化 def init_connections(self): # 连接全选信号 self.table.horizontalHeader().allChecked.connect(self.update_all_checkboxes) # 连接查询按钮 self.search_button.clicked.connect(self.handle_search) def handle_search(self): # 获查询条件 name = self.name_input.text() department = self.department_combo.currentText() mentor = self.mentor_combo.currentText() status = self.status_combo.currentText() # 示例过滤逻辑(需要根据实际数据结构调整) for row in range(self.table.rowCount()): item_name = self.table.item(row, 1).text() item_dept = self.table.item(row, 3).text() item_mentor = self.table.item(row, 2).text() # 这里需要实现具体的过滤逻辑 # 例如:根据各条件组合筛选显示行 # 可以通过设置行的隐藏状态实现过滤 # self.table.setRowHidden(row, 是否隐藏) def update_all_checkboxes(self, checked): """更新所有行复选框""" for checkbox in self.checkboxes: checkbox.setChecked(checked) def checkbox_changed(self): """处理复选框状态变化""" # 计算选中数量 checked_count = sum(1 for cb in self.checkboxes if cb.isChecked()) # 更新表头复选框状态 header = self.table.horizontalHeader() if checked_count == len(self.checkboxes): header.isChecked = True elif checked_count == 0: header.isChecked = False else: # 可以添加中间状态处理(可选) pass header.updateSection(0) def init_ui(self): # 主布局 main_layout = QVBoxLayout(self) main_layout.setContentsMargins(0, 0, 0, 0) # 顶部操作区域 top_control_layout = QHBoxLayout() top_control_layout.setSpacing(10) # 设置控件间距为5 top_control_layout.setContentsMargins(0, 0, 0, 0) # 设置边距为0 # 名称输入框 self.name_input = QLineEdit() self.name_input.setStyleSheet(self.input_style()) self.name_input.setObjectName("name_input") self.name_input.setPlaceholderText("请输入名称") self.name_input.setMaximumWidth(100) # 部门下拉框 self.department_combo = QComboBox() self.department_combo.setStyleSheet(self.select_btn_style()) self.department_combo.setObjectName("department_combo") self.department_combo.addItems(["全部部门", "技术部", "市场部", "财务部", "人力资源部", "行政部"]) self.department_combo.setMaximumWidth(100) # 导师下拉框 self.mentor_combo = QComboBox() self.mentor_combo.setStyleSheet(self.select_btn_style()) self.mentor_combo.setObjectName("mentor_combo") self.mentor_combo.addItems(["全部导师", "导师A", "导师B", "导师C"]) self.mentor_combo.setMaximumWidth(100) # 状态下拉框 self.status_combo = QComboBox() self.status_combo.setStyleSheet(self.select_btn_style()) self.status_combo.setObjectName("status_combo") self.status_combo.addItems(["全部状态", "进行中", "已完成", "已过期"]) self.status_combo.setMaximumWidth(100) # 查询按钮 self.search_button = QPushButton("查询") self.search_button.setStyleSheet(self.query_btn_style()) self.search_button.setObjectName("search_button") self.search_button.setFixedWidth(80) # 批量提醒按钮(保持原有顺序) self.batch_remind_button = QPushButton("批量提醒") self.batch_remind_button.setStyleSheet(self.notice_btn_style()) self.batch_remind_button.setObjectName("action-btn") self.batch_remind_button.setMaximumWidth(100) top_control_layout.addWidget(self.name_input) top_control_layout.addWidget(self.department_combo) top_control_layout.addWidget(self.mentor_combo) top_control_layout.addWidget(self.status_combo) top_control_layout.addWidget(self.search_button) # 插入水平弹性空间,将后续控件推至右侧 # 弹性空间推至右侧 top_control_layout.addSpacerItem(QSpacerItem( 20, 20, QSizePolicy.Policy.Expanding, # 水平扩展 QSizePolicy.Policy.Minimum # 垂直保持最小 )) # 添加右对齐按钮 top_control_layout.addWidget(self.batch_remind_button) # 将布局添加到主布局中 main_layout.addLayout(top_control_layout) # 创建表格 self.table = QTableWidget() self.table.setColumnCount(9) self.table.setHorizontalHeader(CheckBoxHeader(self.table)) self.table.setHorizontalHeaderLabels( ["", "姓名", "导师姓名", "部门", "导师邮箱", "第一天", "第一周", "第一月", "操作"]) # 设置表格行数 self.table.setRowCount(5) # 示例数据 employees = [ {"name": "张三", "account": "zhangsan", "department": "技术部", "email": "zhangsan@example.com"}, {"name": "李四", "account": "lisi", "department": "市场部", "email": "lisi@example.com"}, {"name": "王五", "account": "wangwu", "department": "财务部", "email": "wangwu@example.com"}, {"name": "赵六", "account": "zhaoliu", "department": "人力资源部", "email": "zhaoliu@example.com"}, {"name": "孙七", "account": "sunqi", "department": "行政部", "email": "sunqi@example.com"}, ] for row, emp in enumerate(employees): self.table.setRowHeight(row, 50) # 插入复选框 checkbox = QCheckBox() checkbox.setFixedSize(15, 15) self.checkboxes.append(checkbox) self.table.setCellWidget(row, 0, checkbox) # 连接状态变化信号 checkbox.stateChanged.connect(self.checkbox_changed) # 填充其他列数据 self.table.setItem(row, 1, QTableWidgetItem(emp["name"])) self.table.setItem(row, 2, QTableWidgetItem(emp["account"])) self.table.setItem(row, 3, QTableWidgetItem(emp["department"])) self.table.setItem(row, 4, QTableWidgetItem(emp["email"])) item = QTableWidgetItem("07/30") item.setForeground(QBrush(QColor("#4CAF50"))) self.table.setItem(row, 5, item) item = QTableWidgetItem("08/06") item.setForeground(QBrush(QColor("#F44336"))) self.table.setItem(row, 6, item) item = QTableWidgetItem("08/30") item.setForeground(QBrush(QColor("#9E9E9E"))) self.table.setItem(row, 7, item) # 操作按钮 detail_button = QPushButton("查看详情") detail_button.setObjectName("action-btn") detail_button.setMaximumWidth(80) self.table.setCellWidget(row, 8, detail_button) # 表格样式设置 self.table.horizontalHeader().setStretchLastSection(True) self.table.setStyleSheet(self.table_style()) # 添加到布局 main_layout.addWidget(self.table) @staticmethod def table_style(): return """ QTableWidget { border: 1px solid #e6e6e6; border-radius: 2px; background-color: white; font-size: 14px; color: #333; } QHeaderView::section { background-color: #f5f7fa; color: #333; padding: 8px; border: none; font-weight: bold; } QTableWidget::item { padding: 10px; border-bottom: 1px solid #f0f0f0; } QTableWidget::item:selected { background-color: #e5ebee; color: #000; } QTableWidget::item:hover { background-color: #f0f5f9; } """ @staticmethod def input_style(): return """ padding: 8px 12px; border: 1px solid #e6e6e6; border-radius: 5px; font-size: 14px; """ @staticmethod def query_btn_style(): return """ background-color: #4564a8; color: white; border: none; padding: 10px; border-radius: 5px; font-size: 14px; """ def notice_btn_style(self): return """ background-color: #ffa300; color: white; border: none; padding: 10px; border-radius: 5px; font-size: 14px; """ def select_btn_style(self): return """ QComboBox { padding: 5px 10px; border: 1px solid #e6e6e6; border-radius: 5px; background-color: white; min-height: 24px; font-size: 14px; } QComboBox:hover { border-color: #4CAF50; } QComboBox::drop-down { border: none; background: transparent; padding-right: 5px; } QComboBox QAbstractItemView { outline: 0px; border: 1px solid #e6e6e6; background-color: white; font-size: 14px; } QComboBox QAbstractItemView::item { padding: 4px 10px; } QComboBox QAbstractItemView::item:selected { background-color: #f0f0f0; color: black; } QComboBox QAbstractItemView::item:hover { background-color: #e0e0e0; } """ class MainWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle("表格模块化示例") self.resize(1000, 600) # 主布局 main_layout = QVBoxLayout(self) # 添加独立的表格组件 self.table_widget = TableWidget() main_layout.addWidget(self.table_widget) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) 分别将 top_control_layout 和 QTableWidget 分别抽出成为两个单独的组件
08-20
import sys from PyQt6.QtGui import QBrush, QColor from PyQt6.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QTableWidget, QTableWidgetItem, QCheckBox, QHeaderView, QStyleOptionButton, QStyle, QLineEdit, QComboBox) from PyQt6.QtCore import Qt, QRect, pyqtSignal class CheckBoxHeader(QHeaderView): allChecked = pyqtSignal(bool) # 添加全选信号 def __init__(self, parent=None): super().__init__(Qt.Orientation.Horizontal, parent) self.isChecked = False def paintSection(self, painter, rect, logicalIndex): if logicalIndex == 0: option = QStyleOptionButton() option.rect = QRect(rect.x() + 10, rect.y() + 10, 20, 20) option.state = QStyle.StateFlag.State_Enabled if self.isChecked: option.state |= QStyle.StateFlag.State_On else: option.state |= QStyle.StateFlag.State_Off self.style().drawControl(QStyle.ControlElement.CE_CheckBox, option, painter) else: super().paintSection(painter, rect, logicalIndex) def mousePressEvent(self, event): if event.pos().x() < 70 and event.pos().y() < 70: self.isChecked = not self.isChecked self.updateSection(0) self.allChecked.emit(self.isChecked) # 发射全选信号 super().mousePressEvent(event) class TableWidget(QWidget): """独立的表格组件""" def __init__(self): super().__init__() self.checkboxes = [] self.init_ui() self.init_connections() # 添加连接初始化 def init_connections(self): # 连接全选信号 self.table.horizontalHeader().allChecked.connect(self.update_all_checkboxes) # 连接查询按钮 self.search_button.clicked.connect(self.handle_search) def handle_search(self): # 获查询条件 name = self.name_input.text() department = self.department_combo.currentText() mentor = self.mentor_combo.currentText() status = self.status_combo.currentText() # 示例过滤逻辑(需要根据实际数据结构调整) for row in range(self.table.rowCount()): item_name = self.table.item(row, 1).text() item_dept = self.table.item(row, 3).text() item_mentor = self.table.item(row, 2).text() # 这里需要实现具体的过滤逻辑 # 例如:根据各条件组合筛选显示行 # 可以通过设置行的隐藏状态实现过滤 # self.table.setRowHidden(row, 是否隐藏) def update_all_checkboxes(self, checked): """更新所有行复选框""" for checkbox in self.checkboxes: checkbox.setChecked(checked) def checkbox_changed(self): """处理复选框状态变化""" # 计算选中数量 checked_count = sum(1 for cb in self.checkboxes if cb.isChecked()) # 更新表头复选框状态 header = self.table.horizontalHeader() if checked_count == len(self.checkboxes): header.isChecked = True elif checked_count == 0: header.isChecked = False else: # 可以添加中间状态处理(可选) pass header.updateSection(0) def init_ui(self): # 主布局 main_layout = QVBoxLayout(self) main_layout.setContentsMargins(0, 0, 0, 0) # 顶部操作区域 top_control_layout = QHBoxLayout() top_control_layout.setSpacing(0) # 设置控件间距 # 名称输入框 self.name_input = QLineEdit() self.name_input.setStyleSheet(self.input_style()) self.name_input.setObjectName("name_input") self.name_input.setPlaceholderText("请输入名称") self.name_input.setMaximumWidth(120) top_control_layout.addWidget(self.name_input) # 部门下拉框 self.department_combo = QComboBox() self.department_combo.setStyleSheet(self.select_btn_style()) self.department_combo.setObjectName("department_combo") self.department_combo.addItems(["全部部门", "技术部", "市场部", "财务部", "人力资源部", "行政部"]) self.department_combo.setMaximumWidth(120) top_control_layout.addWidget(self.department_combo) # 导师下拉框 self.mentor_combo = QComboBox() self.mentor_combo.setStyleSheet(self.select_btn_style()) self.mentor_combo.setObjectName("mentor_combo") self.mentor_combo.addItems(["全部导师", "导师A", "导师B", "导师C"]) self.mentor_combo.setMaximumWidth(120) top_control_layout.addWidget(self.mentor_combo) # 状态下拉框 self.status_combo = QComboBox() self.status_combo.setStyleSheet(self.select_btn_style()) self.status_combo.setObjectName("status_combo") self.status_combo.addItems(["全部状态", "进行中", "已完成", "已过期"]) self.status_combo.setMaximumWidth(120) top_control_layout.addWidget(self.status_combo) # 查询按钮 self.search_button = QPushButton("查询") self.search_button.setStyleSheet(self.query_btn_style()) self.search_button.setObjectName("search_button") self.search_button.setMaximumWidth(80) top_control_layout.addWidget(self.search_button) # 批量提醒按钮(保持原有顺序) self.batch_remind_button = QPushButton("批量提醒") self.batch_remind_button.setStyleSheet(self.notice_btn_style()) self.batch_remind_button.setObjectName("action-btn") self.batch_remind_button.setMaximumWidth(100) top_control_layout.addWidget(self.batch_remind_button) # 创建表格 self.table = QTableWidget() self.table.setColumnCount(9) self.table.setHorizontalHeader(CheckBoxHeader(self.table)) self.table.setHorizontalHeaderLabels( ["", "姓名", "导师姓名", "部门", "导师邮箱", "第一天", "第一周", "第一月", "操作"]) # 设置表格行数 self.table.setRowCount(5) # 示例数据 employees = [ {"name": "张三", "account": "zhangsan", "department": "技术部", "email": "zhangsan@example.com"}, {"name": "李四", "account": "lisi", "department": "市场部", "email": "lisi@example.com"}, {"name": "王五", "account": "wangwu", "department": "财务部", "email": "wangwu@example.com"}, {"name": "赵六", "account": "zhaoliu", "department": "人力资源部", "email": "zhaoliu@example.com"}, {"name": "孙七", "account": "sunqi", "department": "行政部", "email": "sunqi@example.com"}, ] for row, emp in enumerate(employees): self.table.setRowHeight(row, 50) # 插入复选框 checkbox = QCheckBox() checkbox.setFixedSize(15, 15) self.checkboxes.append(checkbox) self.table.setCellWidget(row, 0, checkbox) # 连接状态变化信号 checkbox.stateChanged.connect(self.checkbox_changed) # 填充其他列数据 self.table.setItem(row, 1, QTableWidgetItem(emp["name"])) self.table.setItem(row, 2, QTableWidgetItem(emp["account"])) self.table.setItem(row, 3, QTableWidgetItem(emp["department"])) self.table.setItem(row, 4, QTableWidgetItem(emp["email"])) item = QTableWidgetItem("07/30") item.setForeground(QBrush(QColor("#4CAF50"))) self.table.setItem(row, 5, item) item = QTableWidgetItem("08/06") item.setForeground(QBrush(QColor("#F44336"))) self.table.setItem(row, 6, item) item = QTableWidgetItem("08/30") item.setForeground(QBrush(QColor("#9E9E9E"))) self.table.setItem(row, 7, item) # 操作按钮 detail_button = QPushButton("查看详情") detail_button.setObjectName("action-btn") detail_button.setMaximumWidth(80) self.table.setCellWidget(row, 8, detail_button) # 表格样式设置 self.table.horizontalHeader().setStretchLastSection(True) self.table.setStyleSheet(self.table_style()) # 添加到布局 main_layout.addLayout(top_control_layout) main_layout.addWidget(self.table) @staticmethod def table_style(): return """ QTableWidget { border: 1px solid #e6e6e6; border-radius: 2px; background-color: white; font-size: 14px; color: #333; } QHeaderView::section { background-color: #f5f7fa; color: #333; padding: 8px; border: none; font-weight: bold; } QTableWidget::item { padding: 10px; border-bottom: 1px solid #f0f0f0; } QTableWidget::item:selected { background-color: #e5ebee; color: #000; } QTableWidget::item:hover { background-color: #f0f5f9; } """ @staticmethod def input_style(): return """ padding: 8px 12px; border: 1px solid #e6e6e6; border-radius: 5px; font-size: 14px; """ @staticmethod def query_btn_style(): return """ background-color: #4564a8; color: white; border: none; padding: 10px; border-radius: 5px; font-size: 14px; """ def notice_btn_style(self): return """ background-color: #ffa300; color: white; border: none; padding: 10px; border-radius: 5px; font-size: 14px; """ def select_btn_style(self): return """ QComboBox { padding: 5px 10px; border: 1px solid #e6e6e6; border-radius: 5px; background-color: white; min-height: 24px; font-size: 14px; } QComboBox:hover { border-color: #4CAF50; } QComboBox::drop-down { border: none; background: transparent; padding-right: 5px; } QComboBox QAbstractItemView { outline: 0px; border: 1px solid #e6e6e6; background-color: white; font-size: 14px; } QComboBox QAbstractItemView::item { padding: 4px 10px; } QComboBox QAbstractItemView::item:selected { background-color: #f0f0f0; color: black; } QComboBox QAbstractItemView::item:hover { background-color: #e0e0e0; } """ class MainWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle("表格模块化示例") self.resize(1000, 600) # 主布局 main_layout = QVBoxLayout(self) # 添加独立的表格组件 self.table_widget = TableWidget() main_layout.addWidget(self.table_widget) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) 将查询控件 左对齐 并且控件间距为5 最后一个按钮右对齐
08-20
import sys from PyQt6.QtGui import QBrush, QColor from PyQt6.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QTableWidget, QTableWidgetItem, QCheckBox, QHeaderView, QStyleOptionButton, QStyle, QLineEdit, QComboBox, QSpacerItem, QSizePolicy) from PyQt6.QtCore import Qt, QRect, pyqtSignal class CheckBoxHeader(QHeaderView): allChecked = pyqtSignal(bool) # 添加全选信号 def __init__(self, parent=None): super().__init__(Qt.Orientation.Horizontal, parent) self.isChecked = False def paintSection(self, painter, rect, logicalIndex): if logicalIndex == 0: option = QStyleOptionButton() option.rect = QRect(rect.x() + 10, rect.y() + 10, 20, 20) option.state = QStyle.StateFlag.State_Enabled if self.isChecked: option.state |= QStyle.StateFlag.State_On else: option.state |= QStyle.StateFlag.State_Off self.style().drawControl(QStyle.ControlElement.CE_CheckBox, option, painter) else: super().paintSection(painter, rect, logicalIndex) def mousePressEvent(self, event): if event.pos().x() < 70 and event.pos().y() < 70: self.isChecked = not self.isChecked self.updateSection(0) self.allChecked.emit(self.isChecked) # 发射全选信号 super().mousePressEvent(event) class TableWidget(QWidget): """独立的表格组件""" def __init__(self): super().__init__() self.checkboxes = [] self.init_ui() self.init_connections() # 添加连接初始化 def init_connections(self): # 连接全选信号 self.table.horizontalHeader().allChecked.connect(self.update_all_checkboxes) # 连接查询按钮 self.search_button.clicked.connect(self.handle_search) def handle_search(self): # 获查询条件 name = self.name_input.text() department = self.department_combo.currentText() mentor = self.mentor_combo.currentText() status = self.status_combo.currentText() # 示例过滤逻辑(需要根据实际数据结构调整) for row in range(self.table.rowCount()): item_name = self.table.item(row, 1).text() item_dept = self.table.item(row, 3).text() item_mentor = self.table.item(row, 2).text() # 这里需要实现具体的过滤逻辑 # 例如:根据各条件组合筛选显示行 # 可以通过设置行的隐藏状态实现过滤 # self.table.setRowHidden(row, 是否隐藏) def update_all_checkboxes(self, checked): """更新所有行复选框""" for checkbox in self.checkboxes: checkbox.setChecked(checked) def checkbox_changed(self): """处理复选框状态变化""" # 计算选中数量 checked_count = sum(1 for cb in self.checkboxes if cb.isChecked()) # 更新表头复选框状态 header = self.table.horizontalHeader() if checked_count == len(self.checkboxes): header.isChecked = True elif checked_count == 0: header.isChecked = False else: # 可以添加中间状态处理(可选) pass header.updateSection(0) def init_ui(self): # 主布局 main_layout = QVBoxLayout(self) main_layout.setContentsMargins(0, 0, 0, 0) # 顶部操作区域 top_control_layout = QHBoxLayout() top_control_layout.setSpacing(5) # 设置控件间距为5 top_control_layout.setContentsMargins(0, 0, 0, 0) # 设置边距为0 # 名称输入框 self.name_input = QLineEdit() self.name_input.setStyleSheet(self.input_style()) self.name_input.setObjectName("name_input") self.name_input.setPlaceholderText("请输入名称") self.name_input.setMaximumWidth(120) top_control_layout.addWidget(self.name_input) # 部门下拉框 self.department_combo = QComboBox() self.department_combo.setStyleSheet(self.select_btn_style()) self.department_combo.setObjectName("department_combo") self.department_combo.addItems(["全部部门", "技术部", "市场部", "财务部", "人力资源部", "行政部"]) self.department_combo.setMaximumWidth(120) top_control_layout.addWidget(self.department_combo) # 导师下拉框 self.mentor_combo = QComboBox() self.mentor_combo.setStyleSheet(self.select_btn_style()) self.mentor_combo.setObjectName("mentor_combo") self.mentor_combo.addItems(["全部导师", "导师A", "导师B", "导师C"]) self.mentor_combo.setMaximumWidth(120) top_control_layout.addWidget(self.mentor_combo) # 状态下拉框 self.status_combo = QComboBox() self.status_combo.setStyleSheet(self.select_btn_style()) self.status_combo.setObjectName("status_combo") self.status_combo.addItems(["全部状态", "进行中", "已完成", "已过期"]) self.status_combo.setMaximumWidth(120) top_control_layout.addWidget(self.status_combo) # 查询按钮 self.search_button = QPushButton("查询") self.search_button.setStyleSheet(self.query_btn_style()) self.search_button.setObjectName("search_button") self.search_button.setMaximumWidth(120) top_control_layout.addWidget(self.search_button) # 批量提醒按钮(保持原有顺序) self.batch_remind_button = QPushButton("批量提醒") self.batch_remind_button.setStyleSheet(self.notice_btn_style()) self.batch_remind_button.setObjectName("action-btn") self.batch_remind_button.setMaximumWidth(100) # 插入水平弹性空间,将后续控件推至右侧 top_control_layout.addSpacerItem(QSpacerItem(20, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)) # 添加右对齐按钮 top_control_layout.addWidget(self.batch_remind_button) # 将布局添加到主布局中 main_layout.addLayout(top_control_layout) # 创建表格 self.table = QTableWidget() self.table.setColumnCount(9) self.table.setHorizontalHeader(CheckBoxHeader(self.table)) self.table.setHorizontalHeaderLabels( ["", "姓名", "导师姓名", "部门", "导师邮箱", "第一天", "第一周", "第一月", "操作"]) # 设置表格行数 self.table.setRowCount(5) # 示例数据 employees = [ {"name": "张三", "account": "zhangsan", "department": "技术部", "email": "zhangsan@example.com"}, {"name": "李四", "account": "lisi", "department": "市场部", "email": "lisi@example.com"}, {"name": "王五", "account": "wangwu", "department": "财务部", "email": "wangwu@example.com"}, {"name": "赵六", "account": "zhaoliu", "department": "人力资源部", "email": "zhaoliu@example.com"}, {"name": "孙七", "account": "sunqi", "department": "行政部", "email": "sunqi@example.com"}, ] for row, emp in enumerate(employees): self.table.setRowHeight(row, 50) # 插入复选框 checkbox = QCheckBox() checkbox.setFixedSize(15, 15) self.checkboxes.append(checkbox) self.table.setCellWidget(row, 0, checkbox) # 连接状态变化信号 checkbox.stateChanged.connect(self.checkbox_changed) # 填充其他列数据 self.table.setItem(row, 1, QTableWidgetItem(emp["name"])) self.table.setItem(row, 2, QTableWidgetItem(emp["account"])) self.table.setItem(row, 3, QTableWidgetItem(emp["department"])) self.table.setItem(row, 4, QTableWidgetItem(emp["email"])) item = QTableWidgetItem("07/30") item.setForeground(QBrush(QColor("#4CAF50"))) self.table.setItem(row, 5, item) item = QTableWidgetItem("08/06") item.setForeground(QBrush(QColor("#F44336"))) self.table.setItem(row, 6, item) item = QTableWidgetItem("08/30") item.setForeground(QBrush(QColor("#9E9E9E"))) self.table.setItem(row, 7, item) # 操作按钮 detail_button = QPushButton("查看详情") detail_button.setObjectName("action-btn") detail_button.setMaximumWidth(80) self.table.setCellWidget(row, 8, detail_button) # 表格样式设置 self.table.horizontalHeader().setStretchLastSection(True) self.table.setStyleSheet(self.table_style()) # 添加到布局 main_layout.addLayout(top_control_layout) main_layout.addWidget(self.table) @staticmethod def table_style(): return """ QTableWidget { border: 1px solid #e6e6e6; border-radius: 2px; background-color: white; font-size: 14px; color: #333; } QHeaderView::section { background-color: #f5f7fa; color: #333; padding: 8px; border: none; font-weight: bold; } QTableWidget::item { padding: 10px; border-bottom: 1px solid #f0f0f0; } QTableWidget::item:selected { background-color: #e5ebee; color: #000; } QTableWidget::item:hover { background-color: #f0f5f9; } """ @staticmethod def input_style(): return """ padding: 8px 12px; border: 1px solid #e6e6e6; border-radius: 5px; font-size: 14px; """ @staticmethod def query_btn_style(): return """ background-color: #4564a8; color: white; border: none; padding: 10px; border-radius: 5px; font-size: 14px; """ def notice_btn_style(self): return """ background-color: #ffa300; color: white; border: none; padding: 10px; border-radius: 5px; font-size: 14px; """ def select_btn_style(self): return """ QComboBox { padding: 5px 10px; border: 1px solid #e6e6e6; border-radius: 5px; background-color: white; min-height: 24px; font-size: 14px; } QComboBox:hover { border-color: #4CAF50; } QComboBox::drop-down { border: none; background: transparent; padding-right: 5px; } QComboBox QAbstractItemView { outline: 0px; border: 1px solid #e6e6e6; background-color: white; font-size: 14px; } QComboBox QAbstractItemView::item { padding: 4px 10px; } QComboBox QAbstractItemView::item:selected { background-color: #f0f0f0; color: black; } QComboBox QAbstractItemView::item:hover { background-color: #e0e0e0; } """ class MainWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle("表格模块化示例") self.resize(1000, 600) # 主布局 main_layout = QVBoxLayout(self) # 添加独立的表格组件 self.table_widget = TableWidget() main_layout.addWidget(self.table_widget) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) 查询按钮 设置宽度不起作用
08-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值