Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Multi-Column Hierarchical Views with QColumnView

Tech 3

QColumnView is a Qt widget that displays hierarchical data acrosss multiple columns. Each column can contain multiple items, providing a visual representation of parenet-child relationships. This control inherits from QAbstractItemView and supports features like item display, drag-and-drop operations, and inline editing.

When running the example code, you'll initially see a "Root Node." Clicking on it reveals "Child Node 1" in the next collumn, and further navigation continues to subsequent columns.

Note: This widget is considered deprecaetd. For new development, consider using QTreeView or QTableView as alternatives.

Example Implementation

Python Example

import sys
from PySide6.QtWidgets import QApplication, QColumnView
from PySide6.QtCore import Qt
from PySide6.QtGui import QStandardItemModel, QStandardItem

class ColumnViewDemo(QColumnView):
    def __init__(self):
        super().__init__()
        
        data_model = QStandardItemModel()
        
        main_item = QStandardItem("Primary Item")
        sub_item_a = QStandardItem("Sub-item A")
        sub_item_b = QStandardItem("Sub-item B")
        
        main_item.appendRow(sub_item_a)
        main_item.appendRow(sub_item_b)
        
        data_model.appendRow(main_item)
        self.setModel(data_model)

if __name__ == "__main__":
    qt_app = QApplication(sys.argv)
    view_window = ColumnViewDemo()
    view_window.show()
    sys.exit(qt_app.exec())

C++ Example

#include <QApplication>
#include <QColumnView>
#include <QStandardItemModel>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    
    QColumnView column_display;
    QStandardItemModel data_model;
    
    QStandardItem* primary_item = new QStandardItem("Main Entry");
    QStandardItem* first_child = new QStandardItem("First Child");
    QStandardItem* second_child = new QStandardItem("Second Child");
    
    primary_item->appendRow(first_child);
    primary_item->appendRow(second_child);
    data_model.appendRow(primary_item);
    
    column_display.setModel(&data_model);
    column_display.show();
    
    return app.exec();
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.