提交 03ad7b99 编写于 作者: 之一Yo's avatar 之一Yo

修复表格组件无法多选的问题

上级 93700c4f
......@@ -2,7 +2,7 @@
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QHeaderView, QTableWidgetItem, QWidget, QHBoxLayout
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QWidget, QHBoxLayout
from qfluentwidgets import TableWidget, setTheme, Theme, TableView
......@@ -58,13 +58,14 @@ class Demo(QWidget):
self.tableView.verticalHeader().hide()
self.tableView.setHorizontalHeaderLabels(['标题', '艺人', '专辑', '年份', '时长'])
self.tableView.resizeColumnsToContents()
# self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
# self.tableView.setSortingEnabled(True)
self.setStyleSheet("Demo{background: rgb(249, 249, 249)} ")
self.hBoxLayout.setContentsMargins(0, 0, 0, 0)
self.hBoxLayout.addWidget(self.tableView)
self.resize(800, 700)
self.resize(625, 700)
if __name__ == "__main__":
......
......@@ -12,7 +12,7 @@ Examples are available at https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/ma
:license: GPLv3, see LICENSE for more details.
"""
__version__ = "0.8.0"
__version__ = "0.8.2"
from .components import *
from .common import *
......
......@@ -3,6 +3,7 @@ QTableView {
outline: none;
border: none;
font: 13px 'Segoe UI', 'Microsoft YaHei';
selection-background-color: transparent;
}
QTableView::item {
......
......@@ -3,6 +3,7 @@ QTableView {
outline: none;
border: none;
font: 13px 'Segoe UI', 'Microsoft YaHei';
selection-background-color: transparent;
}
QTableView::item {
......
此差异已折叠。
# coding: utf-8
from PyQt5 import QtGui
from typing import List
from PyQt5.QtCore import Qt, QMargins, QModelIndex
from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtWidgets import (QStyledItemDelegate, QApplication, QStyleOptionViewItem,
......@@ -8,7 +9,6 @@ from PyQt5.QtWidgets import (QStyledItemDelegate, QApplication, QStyleOptionView
from ...common.smooth_scroll import SmoothScroll
from ...common.style_sheet import isDarkTheme, FluentStyleSheet, themeColor
from .line_edit import LineEdit
from .scroll_area import SmoothScrollBar
class TableItemDelegate(QStyledItemDelegate):
......@@ -18,7 +18,7 @@ class TableItemDelegate(QStyledItemDelegate):
self.margin = 2
self.hoverRow = -1
self.pressedRow = -1
self.currentRow = -1
self.selectedRows = set()
def setHoverRow(self, row: int):
self.hoverRow = row
......@@ -26,10 +26,12 @@ class TableItemDelegate(QStyledItemDelegate):
def setPressedRow(self, row: int):
self.pressedRow = row
def setCurrentRow(self, row: int):
self.currentRow = row
if row == self.pressedRow:
self.pressedRow = -1
def setSelectedRows(self, indexes: List[QModelIndex]):
self.selectedRows.clear()
for index in indexes:
self.selectedRows.add(index.row())
if index.row() == self.pressedRow:
self.pressedRow = -1
def sizeHint(self, option, index):
# increase original sizeHint to accommodate space needed for border
......@@ -89,7 +91,7 @@ class TableItemDelegate(QStyledItemDelegate):
c = 255 if isDark else 0
alpha = 0
if self.currentRow != index.row():
if index.row() not in self.selectedRows:
if isPressed:
alpha = 9 if isDark else 6
elif isHover:
......@@ -135,6 +137,7 @@ class TableBase:
self.setShowGrid(False)
self.setMouseTracking(True)
self.setItemDelegate(self.delegate)
self.setSelectionBehavior(TableWidget.SelectRows)
self.setVerticalScrollMode(QTableView.ScrollPerPixel)
self.setHorizontalScrollMode(QTableView.ScrollPerPixel)
......@@ -155,8 +158,8 @@ class TableBase:
self.delegate.setPressedRow(row)
self.viewport().update()
def setCurrentRow(self, row: int):
self.delegate.setCurrentRow(row)
def setSelectedRows(self, indexes: List[QModelIndex]):
self.delegate.setSelectedRows(indexes)
self.viewport().update()
def leaveEvent(self, e):
......@@ -178,11 +181,11 @@ class TableBase:
def mouseReleaseEvent(self, e):
row = self.indexAt(e.pos()).row()
if row >= 0 and e.button() != Qt.RightButton:
self.setCurrentRow(row)
self.setSelectedRows(self.selectedIndexes())
else:
self.setPressedRow(-1)
return TableView.mouseReleaseEvent(self, e)
return QTableView.mouseReleaseEvent(self, e)
class TableWidget(QTableWidget, TableBase):
......@@ -192,6 +195,7 @@ class TableWidget(QTableWidget, TableBase):
super().__init__(parent)
class TableView(QTableView, TableBase):
""" Table view """
......
......@@ -6,7 +6,7 @@ with open('README.md', encoding='utf-8') as f:
setuptools.setup(
name="PyQt-Fluent-Widgets",
version="0.8.0",
version="0.8.2",
keywords="pyqt fluent widgets",
author="zhiyiYo",
author_email="shokokawaii@outlook.com",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册