JokerMartini
JokerMartini

Reputation: 6147

How can I improve speed of QTreeView when updating rows?

How can I improve the speed of this python tool. When a user types in the Description or Name inputs, it slows the program while it updates the list of items in the second column of the QTreeView. It doesn't seem like it should be that taxing of a process.

Why is it quicker for me to recreate the entire list of items vs just updating the text in the second column of each row?

I noticed it's quicker to do this:

# self.uiAssetName.textChanged.connect(self.generate_names)
# self.uiAssetDescription.textChanged.connect(self.generate_names)
self.uiAssetName.textChanged.connect(self.generate_assets)
self.uiAssetDescription.textChanged.connect(self.generate_assets)

enter image description here

# Simple rename tool using QTreeView, pass data object into treeview

from __future__ import print_function
# Modules
import sys
import random
from PySide import QtGui, QtCore


AssetClasses = {
    "Label A": "AA",
    "Label B": "BB",
    "Label C": "CC",
    "Label D": "DD",
    "Label E": "EE",
    "Label F": "FF",
    "Label G": "GG",
    "Label H": "HH"
}

class Asset(object):
    def __init__(self, name=''):
        self.name = name
        self.newname = ''


class PSRenamer(QtGui.QMainWindow):
    def __init__(self):
        super(PSRenamer, self).__init__()

        # ui
        self.setWindowTitle(self.tr("Renamer"))
        self.setMinimumWidth(450)
        self.setMinimumHeight(500)

        # create widgets
        lbAssetClass = QtGui.QLabel("Class")
        self.uiAssetClass = QtGui.QComboBox()

        lbAssetName = QtGui.QLabel("Name")
        self.uiAssetName = QtGui.QLineEdit()
        self.uiAssetName.setPlaceholderText('Name...')

        lbAssetDescription = QtGui.QLabel("Description")
        self.uiAssetDescription = QtGui.QLineEdit()
        self.uiAssetDescription.setPlaceholderText('Description...')

        # treeview
        self._model = QtGui.QStandardItemModel()
        self.ui_items = QtGui.QTreeView()
        self.ui_items.setAlternatingRowColors(True)
        self.ui_items = QtGui.QTreeView()
        self.ui_items.setAlternatingRowColors(True)
        self.ui_items.setModel(self._model)
        self.ui_items.setSortingEnabled(True)
        self.ui_items.sortByColumn(0, QtCore.Qt.AscendingOrder)
        self.ui_items.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.ui_items.setSelectionMode(QtGui.QAbstractItemView.NoSelection)
        self.ui_items.setFocusPolicy(QtCore.Qt.NoFocus)
        self.ui_items.header().setResizeMode(QtGui.QHeaderView.ResizeToContents)
        self.ui_items.header().setStretchLastSection(False)

        self.uiGenerateAssets = QtGui.QPushButton("Generate")
        self.uiRunRenamer = QtGui.QPushButton("Apply")

        # layout: controls
        gdl = QtGui.QGridLayout()
        gdl.setContentsMargins(10, 5, 10, 5)
        gdl.addWidget(lbAssetClass, 0, 0)
        gdl.addWidget(self.uiAssetClass, 0, 1)
        gdl.addWidget(lbAssetName, 1, 0)
        gdl.addWidget(self.uiAssetName, 1, 1)
        gdl.addWidget(lbAssetDescription, 2, 0)
        gdl.addWidget(self.uiAssetDescription, 2, 1)
        gdl.addWidget(self.ui_items, 3, 0, 1, 2)

        gdl.addWidget(self.uiGenerateAssets, 4, 0, 1, 1)
        gdl.addWidget(self.uiRunRenamer, 4, 1, 1, 1)

        # main layout
        main_widget = QtGui.QWidget()
        main_widget.setLayout(gdl)
        self.setCentralWidget(main_widget)


        for c in AssetClasses:
            self.uiAssetClass.addItem(c, AssetClasses[c])
            self.uiAssetClass.currentIndexChanged.connect(self.generate_names)
            self.uiAssetClass.setCurrentIndex(6)

        # actions
        self.uiAssetName.textChanged.connect(self.generate_names)
        self.uiAssetDescription.textChanged.connect(self.generate_names)
        self.uiRunRenamer.clicked.connect(self.setNewNames)
        self.uiGenerateAssets.clicked.connect(self.generateAssets)
        self.generateAssets()

    def generateAssets(self):
        print("generate new assets")
        items = [
            'Football', 'Steve', 'Basketball', 'Grag', 'Badminton', 'Boris', 'Red Lamp', 'Ann', 'PinSpot', 'Diana', 'Soccer', 'Jogn', 'Ferrari', 'Aziz', 'Train', 'Asef', 'Cat', 'Arnold', 'IceHockey', 'Dimitras', 'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'District of Columbia', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming', 'Afghanistan', 'Albania', 'Algeria', 'American Samoa', 'Andorra', 'Angola', 'Anguilla', 'Antarctica', 'Antigua and Barbuda', 'Argentina', 'Armenia', 'Aruba', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bermuda', 'Bhutan', 'Bolivia', 'Bosnia and Herzegovina', 'Botswana', 'Bouvet Island', 'Brazil', 'British Indian Ocean Territory', 'Brunei Darussalam', 'Bulgaria', 'Burkina Faso', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Cape Verde', 'Cayman Islands', 'Central African Republic', 'Chad', 'Chile', 'China', 'Christmas Island', 'Cocos (Keeling) Islands', 'Colombia', 'Comoros', 'Congo', 'Congo', 'The Democratic Republic of The', 'Cook Islands', 'Costa Rica', 'Cote Divoire', 'Croatia', 'Cuba', 'Cyprus', 'Czech Republic', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Ethiopia', 'Falkland Islands (Malvinas)', 'Faroe Islands', 'Fiji', 'Finland', 'France', 'French Guiana', 'French Polynesia', 'French Southern Territories', 'Gabon', 'Gambia', 'Georgia', 'Germany', 'Ghana', 'Gibraltar', 'Greece', 'Greenland', 'Grenada', 'Guadeloupe', 'Guam', 'Guatemala', 'Guinea', 'Guinea-bissau', 'Guyana', 'Haiti', 'Heard Island and Mcdonald Islands', 'Holy See (Vatican City State)', 'Honduras', 'Hong Kong', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Iran', 'Islamic Republic of', 'Iraq', 'Ireland', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jordan', 'Kazakhstan', 'Kenya', 'Kiribati', 'Korea', 'Democratic Peoples Republic of', 'Korea', 'Republic of', 'Kuwait', 'Kyrgyzstan', 'Lao Peoples Democratic Republic', 'Latvia', 'Lebanon', 'Lesotho', 'Liberia', 'Libyan Arab Jamahiriya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Macao', 'Macedonia', 'The Former Yugoslav Republic of', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Marshall Islands', 'Martinique', 'Mauritania', 'Mauritius', 'Mayotte', 'Mexico', 'Micronesia', 'Federated States of', 'Moldova', 'Republic of', 'Monaco', 'Mongolia', 'Montserrat', 'Morocco', 'Mozambique', 'Myanmar', 'Namibia', 'Nauru', 'Nepal', 'Netherlands', 'Netherlands Antilles', 'New Caledonia', 'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'Niue', 'Norfolk Island', 'Northern Mariana Islands', 'Norway', 'Oman', 'Pakistan', 'Palau', 'Palestinian Territory', 'Occupied', 'Panama', 'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines', 'Pitcairn', 'Poland', 'Portugal', 'Puerto Rico', 'Qatar', 'Reunion', 'Romania', 'Russian Federation', 'Rwanda', 'Saint Helena', 'Saint Kitts and Nevis', 'Saint Lucia', 'Saint Pierre and Miquelon', 'Saint Vincent and The Grenadines', 'Samoa', 'San Marino', 'Sao Tome and Principe', 'Saudi Arabia', 'Senegal', 'Serbia and Montenegro', 'Seychelles', 'Sierra Leone', 'Singapore', 'Slovakia', 'Slovenia', 'Solomon Islands', 'Somalia', 'South Africa', 'South Georgia and The South Sandwich Islands', 'Spain', 'Sri Lanka', 'Sudan', 'Suriname', 'Svalbard and Jan Mayen', 'Swaziland', 'Sweden', 'Switzerland', 'Syrian Arab Republic', 'Taiwan', 'Province of China', 'Tajikistan', 'Tanzania', 'United Republic of', 'Thailand', 'Timor-leste', 'Togo', 'Tokelau', 'Tonga', 'Trinidad and Tobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Turks and Caicos Islands', 'Tuvalu', 'Uganda', 'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States', 'United States Minor Outlying Islands', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Venezuela', 'Viet Nam', 'Virgin Islands', 'British', 'Virgin Islands', 'U.S.', 'Wallis and Futuna', 'Western Sahara', 'Yemen', 'Zambia', 'Zimbabwe'
        ]


        model = self.ui_items.model()
        model.clear()
        columns = ["Name","New Name"]
        model.setHorizontalHeaderLabels(columns)

        random.seed()

        for name in random.sample(items, random.randint(1, len(items))):

            obj = Asset(name)
            model.insertRow(0)
            model.setData(model.index(0, 0), obj.name)
            model.setData(model.index(0, 1), obj.newname)

            # Append peson data
            model.setData(model.index(0, 0), obj, role=QtCore.Qt.UserRole)

            model.setData(model.index(0, 1),
                          QtGui.QBrush(QtGui.QColor(50, 80, 190, 255)),
                          QtCore.Qt.ForegroundRole)

        self.generate_names()

    def setNewNames(self):
        print("apply new names")
        model = self.ui_items.model()
        for i in range(model.rowCount()):
            sidx = model.index(i, 1,
                               parent=QtCore.QModelIndex())
            didx = model.index(i, 0,
                               parent=QtCore.QModelIndex())
            if sidx.isValid() and didx.isValid():
                model.setData(didx, sidx.data())
                data = didx.data(role=QtCore.Qt.UserRole)
                if isinstance(data, Person):
                    print(i + 1, data)

    def generate_names(self):
        index = self.uiAssetClass.currentIndex()
        assetCode = str(self.uiAssetClass.itemData(index))

        items = [assetCode, self.uiAssetName.text(),
                 self.uiAssetDescription.text()]
        items = list(filter(lambda x: x, items))
        name_template = '_'.join(items)
        print('NT: {}'.format(name_template))
        self.set_new_names(name_template)

    def set_new_names(self, template):
        model = self.ui_items.model()
        names = {}
        for i in range(model.rowCount()):
            if template not in names:
                names[template] = []
            names[template].append(model.index(i, 1,
                                               parent=QtCore.QModelIndex()))
        # Set names
        for tmpl, items in names.items():
            for idx, item in enumerate(items):
                if item.isValid():
                    suffix = '_{:03d}'.format(idx+1)
                    if len(items) == 1:
                        suffix = ''
                    model.setData(item, '{}{}'.format(tmpl, suffix))

# Main
# -----------------------------------------------------------------------------
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = PSRenamer()
    window.show()
    app.exec_()

Upvotes: 2

Views: 880

Answers (1)

ekhumoro
ekhumoro

Reputation: 120598

You have column sorting and resizing switched on by default. This means the whole list will be re-sorted and re-sized every time you change one row. Obviously this will absolutely kill the performance if the list is fairly long.

The solution is to switch off default sorting/resizing and only apply it after re-populating the list:

class PSRenamer(QtGui.QMainWindow):
    def __init__(self):
        ...
        self.ui_items.setModel(self._model)
        # self.ui_items.setSortingEnabled(True)
        # self.ui_items.sortByColumn(0, QtCore.Qt.AscendingOrder)
        self.ui_items.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
        self.ui_items.setSelectionMode(QtGui.QAbstractItemView.NoSelection)
        self.ui_items.setFocusPolicy(QtCore.Qt.NoFocus)
        # self.ui_items.header().setResizeMode(QtGui.QHeaderView.ResizeToContents)
        self.ui_items.header().setStretchLastSection(False)

    def set_new_names(self, template):
        self.ui_items.setSortingEnabled(False)
        ...
        # Set names
        ...
        self.ui_items.setSortingEnabled(True)
        self.ui_items.sortByColumn(0, QtCore.Qt.AscendingOrder)
        self.ui_items.header().resizeSections(QtGui.QHeaderView.ResizeToContents)

Upvotes: 3

Related Questions