zhiyiYo / PyQt-Fluent-Widgets

A fluent design widgets library based on C++ Qt/PyQt/PySide. Make Qt Great Again.
https://qfluentwidgets.com
GNU General Public License v3.0
5.61k stars 541 forks source link

TableItemDelegate size #335

Closed immiProgrammer closed 1 year ago

immiProgrammer commented 1 year ago

image image

immiProgrammer commented 1 year ago

i am using QAbstractTableModel with QSortFilterProxyModel

immiProgrammer commented 1 year ago

this is showing when i enable dpi scale

QApplication.setHighDpiScaleFactorRoundingPolicy(
    Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps) 

when i not enable dpi scale all TableItemDelegate same sized

zhiyiYo commented 1 year ago

Try tableView.resizeRowsToContents() when you update the data of table

immiProgrammer commented 1 year ago

when i use QTableView this bug is not showing:

image

immiProgrammer commented 1 year ago

i use this line of code: self.thread.finished.connect(lambda: [self.table_view.resizeRowToContents(row) for row in range(self.proxy_model.rowCount())])

the results is:

https://github.com/zhiyiYo/PyQt-Fluent-Widgets/assets/101139501/39b15b4c-45bb-4aa8-87f9-abfcc5d607ba

immiProgrammer commented 1 year ago

i use also this line of code: self.thread.finished.connect(lambda: self.table_view.resizeRowsToContents())

even then show same results thats show in above video

zhiyiYo commented 1 year ago

Could you provide the minimum code to reproduce this problem.

immiProgrammer commented 1 year ago

this is minimum code and full understanding code:

from PyQt5.QtCore import Qt, QSortFilterProxyModel, QAbstractTableModel
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget

import pandas as pd
from qfluentwidgets import TableView, LineEdit

QApplication.setHighDpiScaleFactorRoundingPolicy(
    Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)

#* Filtering Data
class CustomFilterProxyModel(QSortFilterProxyModel):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.filter_text = ""

    def setFilterText(self, text):
        self.filter_text = text
        self.invalidateFilter()

    def filterAcceptsRow(self, source_row, source_parent):

        index0 = self.sourceModel().index(source_row, self.filterKeyColumn(), source_parent)
        value0 = self.sourceModel().data(index0, Qt.DisplayRole)
        self.filter_text = self.filter_text.lower()

        if self.filter_text in value0.lower():
            return True
        return False

#* QAbstractTableModel For Pandas
class PandasTableModel(QAbstractTableModel):
    def __init__(self, path:str):
        super().__init__()
        self._data = pd.read_csv(path)
        self._data.drop_duplicates(subset="Name", keep='first', inplace=True)

    def rowCount(self, parent=None):
        return self._data.shape[0]

    def columnCount(self, parent=None):
        return self._data.shape[1]

    def data(self, index, role=Qt.DisplayRole):
        if index.isValid() and role in [Qt.DisplayRole, Qt.EditRole]:
            return str(self._data.iloc[index.row(), index.column()])

    def headerData(self, section, orientation, role=Qt.DisplayRole):
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return str(self._data.columns[section])
            elif orientation == Qt.Vertical:
                return str(self._data.index[section]+1)

        return super().headerData(section, orientation, role)

#* Manage TableView and LineEdit
class TableViewManager:
    def __init__(self, model) -> None:
        self.model = model
        self.proxy_model = CustomFilterProxyModel()
        self.proxy_model.setSourceModel(self.model)

        self.table_view = TableView()
        self.table_view.setModel(self.proxy_model)

    def setLineEdit(self, lineE:LineEdit):
        lineE.textChanged.connect(self.proxy_model.setFilterText)
        lineE.returnPressed.connect(lambda:self.proxy_model.setFilterText(self.lineedit.text()))

if __name__ == '__main__':

    app = QApplication([])
    app.setAttribute(Qt.AA_DontCreateNativeWidgetSiblings)

    tablemanager = TableViewManager(PandasTableModel(r"C:\Users\self\Desktop\products 1.csv"))
    tablemanager.proxy_model.setFilterKeyColumn(0)
    table_view = tablemanager.table_view

    #* Create Custom LineEdit and connect into table
    line_edit = LineEdit()
    tablemanager.setLineEdit(line_edit)

    # Create a layout for the main window
    layout = QVBoxLayout()

    layout.addWidget(line_edit)
    layout.addWidget(table_view)

    # Create a main window widget and set the layout
    window = QWidget()
    window.setLayout(layout)
    window.show()
    window.setMinimumSize(700, 500)
    app.exec()
immiProgrammer commented 1 year ago

when i comment showEvent Function in 'qfluentwidgets\components\widgets\table_view.py' all items has same size but same not changed

image

zhiyiYo commented 1 year ago

Please provide the CSV file

immiProgrammer commented 1 year ago

Please provide the CSV file products 1 - Copy.csv

immiProgrammer commented 1 year ago

Please provide the CSV file

Are u face this issue?

immiProgrammer commented 1 year ago

i am very glad to fix that issue using this line:

table_view.verticalHeader().setDefaultSectionSize(40) or table_view.verticalHeader().setMinimumSectionSize(40)

immiProgrammer commented 7 months ago

Good https://github.com/zhiyiYo/PyQt-Fluent-Widgets/commit/ecd056fac180b61b67212bb78e8347643c718b6b