Qt QListView自定义树状导航控件

        大部分的软件都有多个页面,这时候就需要一个导航栏控件,通过在导航栏中选择某一栏,同时显示对应的页面。

        本文代码效果如下:

        本文的导航栏控件基于大佬 feiyangqingyun 的导航栏控件博客Qt/C++编写自定义控件46-树状导航栏_qt之实现自定义树状图控件-优快云博客做了美化,修复了一些会导致崩溃的bug。

        本文代码:https://download.youkuaiyun.com/download/Sakuya__/89420773?spm=1001.2014.3001.5501icon-default.png?t=O83Ahttps://download.youkuaiyun.com/download/Sakuya__/89420773?spm=1001.2014.3001.5501        也可以在这里下载大佬的代码学习:NavListView: Qt 自定义的树形导航控件icon-default.png?t=O83Ahttps://gitee.com/qt-open-source-collection/NavListView


代码之路

NavListView.h

#ifndef NAVLISTVIEW_H
#define NAVLISTVIEW_H


#include <QStyledItemDelegate>
#include <QAbstractListModel>
#include <QListView>
#include <vector>

class NavListView;

class NavDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    NavDelegate(QObject *parent);
    ~NavDelegate();

protected:
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const ;
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

private:
    NavListView *nav;
};


class NavModel : public QAbstractListModel
{
    Q_OBJECT
public:
    NavModel(QObject *parent);
    ~NavModel();

public:
    struct TreeNode {
        QString iconName;
        QString label;
        int level;
        bool collapse;
        bool theFirst;
        bool theLast;
        QString info;
        std::list<TreeNode *> children;
    };

    struct ListNode {
        QString label;
        TreeNode *treeNode;
    };

protected:
    int rowCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;

private:
    std::vector<TreeNode *> treeNode;
    std::vector<ListNode> listNode;

public slots:
    void readData(QString path);
    void setData(QStringList listItem);
    void collapse(const QModelIndex &index);

private:
    void refreshList();
};

class NavListView : public QListView
{
    Q_OBJECT
public:
    enum IcoStyle {IcoStyle_Cross = 0, IcoStyle_Triangle = 1};
    NavListView(QWidget *parent);
    ~NavListView();

    bool getInfoVisible() const {
        return infoVisible;
    }

    bool getLineVisible() const {
        return lineVisible;
    }

    bool getIcoColorBg() const {
        return icoColorBg;
    }

    IcoStyle getIcoStyle() const {
        return style;
    }

    QColor getColorLine() const {
        return colorLine;
    }
    /// ====== 获取背景颜色函数
    QColor getColorBgNormal() const{
        return colorBgNormal;
    }
    QColor getColorBgSelected() const{
        return colorBgSelected;
    }
    QColor getColorBgHover() const{
        return colorBgHover;
    }
    QColor getColorBgNormalLeval2() const{
        return colorBgNormalLeval2;
    }
    QColor getColorBgSelectedLeval2() const{
        return colorBgSelectedLeval2;
    }
    QColor getColorBgHoverLeval2() const{
        return colorBgHoverLeval2;
    }
    /// ====== 获取文字颜色函数
    QColor getColorTextNormal() const {
        return colorTextNormal;
    }
    QColor getColorTextSelected() const {
        return colorTextSelected;
    }
    QColor getColorTextHover() const {
        return colorTextHover;
    }
    QColor getColorTextNormalLeval2() const {
        return colorTextNormalLeval2;
    }
    QColor getColorTextSelectedLeval2() const {
        return colorTextSelectedLeval2;
    }
    QColor getColorTextHoverLeval2() const {
        return colorTextHoverLeval2;
    }

public slots:
    // 读取xml文件数据
    void readData(QString xmlPath);

    // 设置数据集合
    void setData(QStringList listItem);

    // 设置当前选中行
    void setCurrentRow(int row);

    // 设置是否显示提示信息
    void setInfoVisible(bool infoVisible);

    // 设置是否显示间隔线条
    void setLineVisible(bool lineVisible);

    // 设置伸缩图片是否采用背景色
    void setIcoColorBg(bool icoColorBg);

    // 设置伸缩图片样式
    void setIcoStyle(IcoStyle style);

    /// ====== 设置各种前景色背景色选中色
    void setColorLine(QColor colorLine);
    void setColorBg(QColor color
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值