大部分的软件都有多个页面,这时候就需要一个导航栏控件,通过在导航栏中选择某一栏,同时显示对应的页面。
本文代码效果如下:
本文的导航栏控件基于大佬 feiyangqingyun 的导航栏控件博客Qt/C++编写自定义控件46-树状导航栏_qt之实现自定义树状图控件-优快云博客做了美化,修复了一些会导致崩溃的bug。
本文代码:https://download.youkuaiyun.com/download/Sakuya__/89420773?spm=1001.2014.3001.5501https://download.youkuaiyun.com/download/Sakuya__/89420773?spm=1001.2014.3001.5501 也可以在这里下载大佬的代码学习:NavListView: Qt 自定义的树形导航控件
https://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