Home · All Classes · Main Classes · Annotated · Grouped Classes · Functions

Chart Example

Files:

The Chart example shows how to create a custom view for the model/view framework.

[Missing image chart-example.png]

In this example, the items in a table model are represented as slices in a pie chart, relying on the flexibility of the model/view architecture to handle custom editing and selection features.

Note that you only need to create a new view class if your data requires a specialized representation. You should first consider using a standard QListView, QTableView, or QTreeView with a custom QItemDelegate subclass if you need to represent data in a special way.

PieView Class Definition

The PieView class is a subclass of QAbstractItemView. The base class provides much of the functionality required by view classes, so we only need to provide implementations for three public functions: visualRect(), scrollTo(), and indexAt(). However, the view needs to maintain strict control over its look and feel, so we also provide implementations for a number of other functions:

    class PieView : public QAbstractItemView
    {
        Q_OBJECT

    public:
        PieView(QWidget *parent = 0);

        QRect visualRect(const QModelIndex &index) const;
        void scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible);
        QModelIndex indexAt(const QPoint &point) const;

    protected slots:
        void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
        void rowsInserted(const QModelIndex &parent, int start, int end);
        void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);

    protected:
        bool edit(const QModelIndex &index, EditTrigger trigger, QEvent *event);
        QModelIndex moveCursor(QAbstractItemView::CursorAction cursorAction,
                               Qt::KeyboardModifiers modifiers);

        int horizontalOffset() const;
        int verticalOffset() const;

        bool isIndexHidden(const QModelIndex &index) const;

        void setSelection(const QRect&, QItemSelectionModel::SelectionFlags command);

        void mouseReleaseEvent(QMouseEvent *event);
        void paintEvent(QPaintEvent *event);
        void resizeEvent(QResizeEvent *event);
        void scrollContentsBy(int dx, int dy);

        QRegion visualRegionForSelection(const QItemSelection &selection) const;

    private:
        QRect itemRect(const QModelIndex &item) const;
        QRegion itemRegion(const QModelIndex &index) const;
        int rows(const QModelIndex &index = QModelIndex()) const;
        void updateGeometries();

        int margin;
        int totalSize;
        int pieSize;
        int validItems;
        double totalValue;
        QRect selectionRect;
    };

PieView Class Implementation

The paint event renders the data from the standard item model as a pie chart. We interpret the data in the following way:

The figure is always drawn with the chart on the left and the key on the right. This means that we must try and obtain an area that is wider than it is tall. We do this by imposing a particular aspect ratio on the chart and applying it to the available vertical space. This ensures that we always obtain the maximum horizontal space for the aspect ratio used. We also apply fixed size margin around the figure.

We use logical coordinates to draw the chart and key, and position them on the view using viewports.


Copyright © 2005 Trolltech Trademarks
Qt 4.0.0-rc1