提交 5362613c 编写于 作者: 之一Yo's avatar 之一Yo

修复列表和表格组件的选中问题

上级 0cc60069
......@@ -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.6"
__version__ = "0.8.7"
from .components import *
from .common import *
......
......@@ -115,3 +115,12 @@ QHeaderView::up-arrow {
margin-right: 6px;
image: url(:/qfluentwidgets/images/table_view/Up_white.svg);
}
QTableCornerButton::section {
background-color: transparent;
border: 1px solid rgba(0, 0, 0, 57);
}
QTableCornerButton::section:pressed {
background-color: rgba(255, 255, 255, 16);
}
\ No newline at end of file
......@@ -116,3 +116,11 @@ QHeaderView::up-arrow {
margin-right: 6px;
image: url(:/qfluentwidgets/images/table_view/Up_black.svg);
}
QTableCornerButton::section {
background-color: transparent;
border: 1px solid rgba(0, 0, 0, 19);
}
QTableCornerButton::section:pressed {
background-color: rgba(0, 0, 0, 12);
}
\ No newline at end of file
此差异已折叠。
# coding:utf-8
from typing import List
from typing import List, Union
from PyQt5.QtCore import Qt, pyqtSignal, QModelIndex
from PyQt5.QtCore import Qt, pyqtSignal, QModelIndex, QItemSelectionModel
from PyQt5.QtGui import QPainter
from PyQt5.QtWidgets import QStyleOptionViewItem, QListView, QWidget, QListView, QListWidget
from PyQt5.QtWidgets import QStyleOptionViewItem, QListView, QListWidgetItem, QListView, QListWidget
from .scroll_bar import SmoothScrollDelegate
from .table_view import TableItemDelegate
......@@ -37,26 +37,26 @@ class ListBase:
self.setItemDelegate(self.delegate)
self.setMouseTracking(True)
self.entered.connect(lambda i: self.setHoverRow(i.row()))
self.pressed.connect(lambda i: self.setPressedRow(i.row()))
self.entered.connect(lambda i: self._setHoverRow(i.row()))
self.pressed.connect(lambda i: self._setPressedRow(i.row()))
def setHoverRow(self, row: int):
def _setHoverRow(self, row: int):
""" set hovered row """
self.delegate.setHoverRow(row)
self.viewport().update()
def setPressedRow(self, row: int):
def _setPressedRow(self, row: int):
""" set pressed row """
self.delegate.setPressedRow(row)
self.viewport().update()
def setSelectedRows(self, indexes: List[QModelIndex]):
def _setSelectedRows(self, indexes: List[QModelIndex]):
self.delegate.setSelectedRows(indexes)
self.viewport().update()
def leaveEvent(self, e):
QListView.leaveEvent(self, e)
self.setHoverRow(-1)
self._setHoverRow(-1)
def resizeEvent(self, e):
QListView.resizeEvent(self, e)
......@@ -64,27 +64,42 @@ class ListBase:
def keyPressEvent(self, e):
QListView.keyPressEvent(self, e)
self.setSelectedRows(self.selectedIndexes())
self._updateSelectedRows()
def mousePressEvent(self, e):
if e.button() == Qt.LeftButton:
QListView.mousePressEvent(self, e)
else:
self.setPressedRow(self.indexAt(e.pos()).row())
self._setPressedRow(self.indexAt(e.pos()).row())
def mouseReleaseEvent(self, e):
QListView.mouseReleaseEvent(self, e)
row = self.indexAt(e.pos()).row()
if row >= 0 and e.button() != Qt.RightButton:
self.setSelectedRows(self.selectedIndexes())
self._updateSelectedRows()
else:
self.setPressedRow(-1)
self._setPressedRow(-1)
def setItemDelegate(self, delegate: ListItemDelegate):
self.delegate = delegate
super().setItemDelegate(delegate)
def setSelection(self, rect, command):
QListView.setSelection(self, rect, command)
self._updateSelectedRows()
def clearSelection(self):
QListView.clearSelection(self)
self._updateSelectedRows()
def setCurrentIndex(self, index: QModelIndex):
QListView.setCurrentIndex(self, index)
self._updateSelectedRows()
def _updateSelectedRows(self):
self._setSelectedRows(self.selectedIndexes())
class ListWidget(ListBase, QListWidget):
""" List widget """
......@@ -92,6 +107,17 @@ class ListWidget(ListBase, QListWidget):
def __init__(self, parent=None):
super().__init__(parent)
def setCurrentItem(self, item: QListWidgetItem, command: Union[QItemSelectionModel.SelectionFlag, QItemSelectionModel.SelectionFlags] = None):
self.setCurrentRow(self.row(item), command)
def setCurrentRow(self, row: int, command: Union[QItemSelectionModel.SelectionFlag, QItemSelectionModel.SelectionFlags] = None):
if not command:
super().setCurrentRow(row)
else:
super().setCurrentRow(row, command)
self._updateSelectedRows()
class ListView(ListBase, QListView):
""" List view """
......
# coding: utf-8
from typing import List
from typing import List, Union
from PyQt5.QtCore import Qt, QMargins, QModelIndex
from PyQt5.QtCore import Qt, QMargins, QModelIndex, QItemSelectionModel
from PyQt5.QtGui import QPainter, QColor, QKeyEvent, QPalette
from PyQt5.QtWidgets import (QStyledItemDelegate, QApplication, QStyleOptionViewItem,
QTableView, QTableWidget, QWidget)
QTableView, QTableWidget, QWidget, QTableWidgetItem)
from ...common.style_sheet import isDarkTheme, FluentStyleSheet, themeColor
from .line_edit import LineEdit
......@@ -151,30 +151,30 @@ class TableBase:
self.setItemDelegate(self.delegate)
self.setSelectionBehavior(TableWidget.SelectRows)
self.entered.connect(lambda i: self.setHoverRow(i.row()))
self.pressed.connect(lambda i: self.setPressedRow(i.row()))
self.entered.connect(lambda i: self._setHoverRow(i.row()))
self.pressed.connect(lambda i: self._setPressedRow(i.row()))
def showEvent(self, e):
QTableView.showEvent(self, e)
self.resizeRowsToContents()
def setHoverRow(self, row: int):
def _setHoverRow(self, row: int):
""" set hovered row """
self.delegate.setHoverRow(row)
self.viewport().update()
def setPressedRow(self, row: int):
def _setPressedRow(self, row: int):
""" set pressed row """
self.delegate.setPressedRow(row)
self.viewport().update()
def setSelectedRows(self, indexes: List[QModelIndex]):
def _setSelectedRows(self, indexes: List[QModelIndex]):
self.delegate.setSelectedRows(indexes)
self.viewport().update()
def leaveEvent(self, e):
QTableView.leaveEvent(self, e)
self.setHoverRow(-1)
self._setHoverRow(-1)
def resizeEvent(self, e):
QTableView.resizeEvent(self, e)
......@@ -182,27 +182,50 @@ class TableBase:
def keyPressEvent(self, e: QKeyEvent):
QTableView.keyPressEvent(self, e)
self.setSelectedRows(self.selectedIndexes())
self._updateSelectedRows()
def mousePressEvent(self, e: QKeyEvent):
if e.button() == Qt.LeftButton:
QTableView.mousePressEvent(self, e)
else:
self.setPressedRow(self.indexAt(e.pos()).row())
self._setPressedRow(self.indexAt(e.pos()).row())
def mouseReleaseEvent(self, e):
QTableView.mouseReleaseEvent(self, e)
row = self.indexAt(e.pos()).row()
if row >= 0 and e.button() != Qt.RightButton:
self.setSelectedRows(self.selectedIndexes())
self._updateSelectedRows()
else:
self.setPressedRow(-1)
self._setPressedRow(-1)
def setItemDelegate(self, delegate: TableItemDelegate):
self.delegate = delegate
super().setItemDelegate(delegate)
def selectAll(self):
QTableView.selectAll(self)
self._updateSelectedRows()
def selectRow(self, row: int):
QTableView.selectRow(self, row)
self._updateSelectedRows()
def setSelection(self, rect, command):
QTableView.setSelection(self, rect, command)
self._updateSelectedRows()
def clearSelection(self):
QTableView.clearSelection(self)
self._updateSelectedRows()
def setCurrentIndex(self, index: QModelIndex):
QTableView.setCurrentIndex(self, index)
self._updateSelectedRows()
def _updateSelectedRows(self):
self._setSelectedRows(self.selectedIndexes())
class TableWidget(TableBase, QTableWidget):
""" Table widget """
......@@ -210,6 +233,17 @@ class TableWidget(TableBase, QTableWidget):
def __init__(self, parent=None):
super().__init__(parent)
def setCurrentCell(self, row: int, column: int, command: Union[QItemSelectionModel.SelectionFlag, QItemSelectionModel.SelectionFlags] = None):
self.setCurrentItem(self.item(row, column), command)
def setCurrentItem(self, item: QTableWidgetItem, command: Union[QItemSelectionModel.SelectionFlag, QItemSelectionModel.SelectionFlags] = None):
if not command:
super().setCurrentItem(item)
else:
super().setCurrentItem(item, command)
self._updateSelectedRows()
class TableView(TableBase, QTableView):
""" Table view """
......
......@@ -6,7 +6,7 @@ with open('README.md', encoding='utf-8') as f:
setuptools.setup(
name="PyQt-Fluent-Widgets",
version="0.8.6",
version="0.8.7",
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.
先完成此消息的编辑!
想要评论请 注册