PyQt - Column View



The QColumnView class in PyQt is used to display hierarchical data in a cascading list format. A seperate column is present in each hierarchy level which helos users to move from one level to another easily.This approach is often referred to as a cascading list, providing a structured and intuitive way to explore complex data sets.

The QcolumnView uses the Model/View(MV) architecture which seperates data presentation from logic. It leads to more maintainable and flexible UI's.

Understanding MV Architecture

In the MV architecture, the QAbstractItemModel class act as the model and it encapsulates the underlying data structure. The QAbstractItemView and QColumnView is responsible for displaying the model data and serve as view.

The seperation of Model and view helps in the following features −

  • Data Reusability − The same model can be used for different display purpose.
  • Increased Flexibility − If one model is modified the changes automatically occurs in all the views which reduces code duplication.
  • Enhanced Maintainability − Changes to how data is displayed only require adjustments to the view, keeping the model independent.

Inheritance and structure

QColumnView is inherited from QAbstractitemView. It implements the interfaces defined by QAbstractItemView to interact with models derived from QAbstractItemModel.

Key Methods and Properties

Sr.No. Methods & Description
1

columnWidths()

Retrieves a list of current column widths.

2

setColumnWidths(list)

Sets the widths of all columns or the remaining columns if the list has fewer elements than the number of columns.

3

initializeColumn(column)

Copies the column view's properties (e.g., iconSize(), textElideMode(), alternatingRowColors()) to the specified column view.

4

previewWidget()

Gets the preview widget, used to display additional information for selected items (can be None).

5

setPreviewWidget(widget)

Sets the preview widget to display details about selected items.

6

resizeGripsVisible()

Determines whether resize grips are visible for column resizing (True by default).

7

updatePreviewWidget(index))

Signals the need to update the preview widget based on the provided index.

Example 1: Basic QColumnView Implementation

In the below example, we will create a simple PyQt application which shows a QcolumnView to display a hierarchical data model. We will use the default settings for column widths and appearance.

We initialize a QFileSystemModel to represent the file system hierarchy. The QColumnView is set to use this model, and the application is launched with app.exec_() method.

import sys
from PyQt5.QtWidgets import QApplication, QColumnView, QFileSystemModel

if __name__ == "__main__":
   app = QApplication(sys.argv)

   model = QFileSystemModel()
   model.setRootPath("")

   column_view = QColumnView()
   column_view.setModel(model)
   column_view.setWindowTitle("Basic QColumnView Example")
   column_view.show()

   sys.exit(app.exec_())

Output

pyqt qcolumnview example 1

Example 2: Customizing Column Widths

In this example we will use the setColumnWidths() method to set column widths for each column by passing a lsit of integer values to the fucntion parameter.

import sys
from PyQt5.QtWidgets import QApplication, QColumnView, QFileSystemModel

if __name__ == "__main__":
   app = QApplication(sys.argv)

   model = QFileSystemModel()
   model.setRootPath("")

   column_view = QColumnView()
   column_view.setModel(model)

   # Customize column widths
   widths = [200, 150, 250]  # Set widths for each column
   column_view.setColumnWidths(widths)

   column_view.setWindowTitle("Customized Column Widths")
   column_view.show()

   sys.exit(app.exec_())

Output

On execution of above code, we get the following result −

pyqt qcolumnview example 2
Advertisements