Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a function that fetches data from MySQL database and stores it in a list.
import mysql.connector
import sys
from collections import defaultdict
from collections import namedtuple
from operator import attrgetter
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
Flag = Qt.ItemFlag


def fetchdata():
    proper = lambda x: x[0].upper() + x[1:]
    song = namedtuple('song', 'artist title album')
    conn = mysql.connector.connect(user='Estranger', password='Nillin10', host='127.0.0.1', port=3306, database='Music')
    cursor = conn.cursor()
    cursor.execute('select artist, title, album from songs')
    return sorted([song(*map(proper, i)) for i in cursor.fetchall()], key=attrgetter('artist', 'album'))


And another function to create the hierarchy needed for QTreeWidget:

def treefy(data):
    entries = defaultdict(lambda: defaultdict(list))
    for artist, title, album in data:
        entries[artist][album].append(title)
    return entries


They are used like this:

songs = fetchdata()
entries = treefy(songs)


Then the data is passed to QTreeWidget:

app = QApplication(sys.argv)
tree = QTreeWidget()
tree.resize(1280,720)
tree.setWindowTitle('tree')
frame = tree.frameGeometry()
center = tree.screen().availableGeometry().center()
frame.moveCenter(center)
tree.move(frame.topLeft())
tree.setColumnCount(4)
tree.setHeaderLabels(['Name', 'Artist', 'Album', 'Title'])
tree.setFont(QFont('Noto Mono', 10, 2))
for i in range(4): tree.setColumnWidth(i, 300)
tree.setAutoScroll(True)
tree.setIndentation(32)
tree.setAlternatingRowColors(True)

for artist, albums in entries.items():
    artist_node = QTreeWidgetItem([artist])
    for album, songs in albums.items():
        album_node = QTreeWidgetItem([album, artist, album])
        for song in songs:
            song_node = QTreeWidgetItem([song, artist, album, song])
            album_node.addChild(song_node)
        artist_node.addChild(album_node)
    tree.addTopLevelItem(artist_node)


I will write a filter function to select items from songs variable based on condition, songs will remain unchanged when this happens, and then the selected data will be retreefied and passed to QTreeWidget, but I don't know how to refresh QTreeWidget.

In short, I want to completely remove all items in QTreeWidget and then re-add filtered results from a list, and then redraw the QTreeWidget, all of this(refresh QTreeWidget) at a press of a button, how can this be done?

I am not adding or removing individual nodes, like I said, I am removing all nodes refetching the data, rebuilding the tree and redrawing it.

What I have tried:

I tried Googling for many days and haven't found a solution, and I seriously don't know what else can tell me about it.
Posted
Updated 2-Jul-21 0:25am

1 solution

Check the documentation: QTreeWidget Class | Qt Widgets 5.15.5[^]. Most classes like this will contain a clear or removeall method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900