• Qt QListView自定义树状导航控件


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

            本文代码效果如下:

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

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


    代码之路

    NavListView.h

    1. #ifndef NAVLISTVIEW_H
    2. #define NAVLISTVIEW_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. class NavListView;
    8. class NavDelegate : public QStyledItemDelegate
    9. {
    10. Q_OBJECT
    11. public:
    12. NavDelegate(QObject *parent);
    13. ~NavDelegate();
    14. protected:
    15. QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const ;
    16. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    17. private:
    18. NavListView *nav;
    19. };
    20. class NavModel : public QAbstractListModel
    21. {
    22. Q_OBJECT
    23. public:
    24. NavModel(QObject *parent);
    25. ~NavModel();
    26. public:
    27. struct TreeNode {
    28. QString iconName;
    29. QString label;
    30. int level;
    31. bool collapse;
    32. bool theFirst;
    33. bool theLast;
    34. QString info;
    35. std::list children;
    36. };
    37. struct ListNode {
    38. QString label;
    39. TreeNode *treeNode;
    40. };
    41. protected:
    42. int rowCount(const QModelIndex &parent) const;
    43. QVariant data(const QModelIndex &index, int role) const;
    44. private:
    45. std::vector treeNode;
    46. std::vector listNode;
    47. public slots:
    48. void readData(QString path);
    49. void setData(QStringList listItem);
    50. void collapse(const QModelIndex &index);
    51. private:
    52. void refreshList();
    53. };
    54. class NavListView : public QListView
    55. {
    56. Q_OBJECT
    57. public:
    58. enum IcoStyle {IcoStyle_Cross = 0, IcoStyle_Triangle = 1};
    59. NavListView(QWidget *parent);
    60. ~NavListView();
    61. bool getInfoVisible() const {
    62. return infoVisible;
    63. }
    64. bool getLineVisible() const {
    65. return lineVisible;
    66. }
    67. bool getIcoColorBg() const {
    68. return icoColorBg;
    69. }
    70. IcoStyle getIcoStyle() const {
    71. return style;
    72. }
    73. QColor getColorLine() const {
    74. return colorLine;
    75. }
    76. /// ====== 获取背景颜色函数
    77. QColor getColorBgNormal() const{
    78. return colorBgNormal;
    79. }
    80. QColor getColorBgSelected() const{
    81. return colorBgSelected;
    82. }
    83. QColor getColorBgHover() const{
    84. return colorBgHover;
    85. }
    86. QColor getColorBgNormalLeval2() const{
    87. return colorBgNormalLeval2;
    88. }
    89. QColor getColorBgSelectedLeval2() const{
    90. return colorBgSelectedLeval2;
    91. }
    92. QColor getColorBgHoverLeval2() const{
    93. return colorBgHoverLeval2;
    94. }
    95. /// ====== 获取文字颜色函数
    96. QColor getColorTextNormal() const {
    97. return colorTextNormal;
    98. }
    99. QColor getColorTextSelected() const {
    100. return colorTextSelected;
    101. }
    102. QColor getColorTextHover() const {
    103. return colorTextHover;
    104. }
    105. QColor getColorTextNormalLeval2() const {
    106. return colorTextNormalLeval2;
    107. }
    108. QColor getColorTextSelectedLeval2() const {
    109. return colorTextSelectedLeval2;
    110. }
    111. QColor getColorTextHoverLeval2() const {
    112. return colorTextHoverLeval2;
    113. }
    114. public slots:
    115. // 读取xml文件数据
    116. void readData(QString xmlPath);
    117. // 设置数据集合
    118. void setData(QStringList listItem);
    119. // 设置当前选中行
    120. void setCurrentRow(int row);
    121. // 设置是否显示提示信息
    122. void setInfoVisible(bool infoVisible);
    123. // 设置是否显示间隔线条
    124. void setLineVisible(bool lineVisible);
    125. // 设置伸缩图片是否采用背景色
    126. void setIcoColorBg(bool icoColorBg);
    127. // 设置伸缩图片样式
    128. void setIcoStyle(IcoStyle style);
    129. /// ====== 设置各种前景色背景色选中色
    130. void setColorLine(QColor colorLine);
    131. void setColorBg(QColor colorBgNormal, QColor colorBgSelected, QColor colorBgHover);
    132. void setColorText(QColor colorTextNormal, QColor colorTextSelected, QColor colorTextHover);
    133. void setColorBgLeval2(QColor colorBgNormal, QColor colorBgSelected, QColor colorBgHover);
    134. void setColorTextLeval2(QColor colorTextNormal, QColor colorTextSelected, QColor colorTextHover);
    135. private:
    136. NavModel *model;
    137. NavDelegate *delegate;
    138. bool infoVisible; // 是否显示提示信息
    139. bool lineVisible; // 是否显示分割线条
    140. bool icoColorBg; // 伸缩图片是否使用颜色
    141. IcoStyle style; // 图标样式
    142. QColor colorLine; // 线条颜色
    143. /// ====== leval为1时的效果
    144. QColor colorBgNormal; // 正常背景色
    145. QColor colorBgSelected; // 选中背景色
    146. QColor colorBgHover; // 悬停背景色
    147. QColor colorTextNormal; // 正常文字颜色
    148. QColor colorTextSelected; // 选中文字颜色
    149. QColor colorTextHover; // 悬停文字颜色
    150. /// ====== leval为2时的效果
    151. QColor colorBgNormalLeval2; // 正常背景颜色
    152. QColor colorBgSelectedLeval2; //
    153. QColor colorBgHoverLeval2; //
    154. QColor colorTextNormalLeval2; // 正常文字颜色
    155. QColor colorTextSelectedLeval2; //
    156. QColor colorTextHoverLeval2; //
    157. };
    158. #endif // NAVLISTVIEW_H

    NavListView.cpp

    1. #include "NavListView.h"
    2. #include
    3. #include
    4. #include
    5. #include
    6. NavDelegate::NavDelegate(QObject *parent) : QStyledItemDelegate(parent)
    7. {
    8. nav = (NavListView *)parent;
    9. }
    10. NavDelegate::~NavDelegate()
    11. {
    12. }
    13. QSize NavDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    14. {
    15. NavModel::TreeNode *node = (NavModel::TreeNode *)index.data(Qt::UserRole).toULongLong();
    16. if (node->level == 1)
    17. {
    18. return QSize(192, 71);
    19. }
    20. else
    21. {
    22. return QSize(182, 48);
    23. }
    24. }
    25. void NavDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    26. {
    27. painter->setRenderHint(QPainter::Antialiasing);
    28. NavModel::TreeNode *node = (NavModel::TreeNode *)index.data(Qt::UserRole).toULongLong();
    29. QColor colorBg;
    30. QColor colorText;
    31. QFont fontText;
    32. int iconSize = 0, leftMargin = 0, topMargin = 0;
    33. if(1 == node->level)
    34. {
    35. if (option.state & QStyle::State_Selected)
    36. {
    37. colorBg = nav->getColorBgSelected();
    38. colorText = nav->getColorTextSelected();
    39. }
    40. else if (option.state & QStyle::State_MouseOver)
    41. {
    42. colorBg = nav->getColorBgHover();
    43. colorText = nav->getColorTextHover();
    44. }
    45. else
    46. {
    47. colorBg = nav->getColorBgNormal();
    48. colorText = nav->getColorTextNormal();
    49. }
    50. iconSize = 32;
    51. leftMargin = 32;
    52. topMargin = 20;
    53. fontText.setPixelSize(20);
    54. painter->setBrush(QBrush(nav->getColorBgNormal()));
    55. painter->setPen(Qt::transparent);
    56. painter->drawRoundedRect(option.rect, 8, 20, Qt::RelativeSize);
    57. }
    58. else if(2 == node->level)
    59. {
    60. if (option.state & QStyle::State_Selected)
    61. {
    62. colorBg = nav->getColorBgSelectedLeval2();
    63. colorText = nav->getColorTextSelectedLeval2();
    64. }
    65. else if (option.state & QStyle::State_MouseOver)
    66. {
    67. colorBg = nav->getColorBgHoverLeval2();
    68. colorText = nav->getColorTextHoverLeval2();
    69. }
    70. else
    71. {
    72. colorBg = nav->getColorBgNormalLeval2();
    73. colorText = nav->getColorTextNormalLeval2();
    74. }
    75. iconSize = 24;
    76. leftMargin = 25;
    77. topMargin = 13;
    78. fontText.setPixelSize(18);
    79. QRect rectLevel2 = option.rect;
    80. rectLevel2.setX(option.rect.x() + 12);
    81. if (node->theFirst)
    82. {
    83. rectLevel2.setHeight(option.rect.height() + 4);
    84. rectLevel2.setWidth(option.rect.width() + 8);
    85. painter->setBrush(QBrush(nav->getColorBgNormalLeval2()));
    86. painter->setPen(Qt::transparent);
    87. painter->drawRoundedRect(rectLevel2, 8, 20, Qt::RelativeSize);
    88. }
    89. else if (node->theLast)
    90. {
    91. rectLevel2.setY(option.rect.y() - 4);
    92. rectLevel2.setWidth(option.rect.width() + 8);
    93. painter->setBrush(QBrush(nav->getColorBgNormalLeval2()));
    94. painter->setPen(Qt::transparent);
    95. painter->drawRoundedRect(rectLevel2, 8, 20, Qt::RelativeSize);
    96. }
    97. else
    98. {
    99. painter->fillRect(rectLevel2, nav->getColorBgNormalLeval2());
    100. }
    101. }
    102. /// ====== 菜单选项背景颜色
    103. if (1 == node->level && option.state & QStyle::State_Selected)
    104. {
    105. QRect rectMenu = option.rect;
    106. rectMenu.setWidth(option.rect.width() - 20);
    107. rectMenu.setHeight(option.rect.height()- 10);
    108. rectMenu.setX(option.rect.x() + 10);
    109. rectMenu.setY(option.rect.y() + 10);
    110. painter->setBrush(QBrush(colorBg));
    111. painter->setPen(Qt::transparent);
    112. painter->drawRoundedRect(rectMenu, 8, 20, Qt::RelativeSize);
    113. }
    114. /// ====== 绘制图标
    115. QPixmap pixMap;
    116. pixMap.load(node->iconName);
    117. QRect rectIcon = option.rect;
    118. rectIcon.setX(option.rect.x()+leftMargin);
    119. rectIcon.setY(option.rect.y()+topMargin);
    120. rectIcon.setWidth(iconSize);
    121. rectIcon.setHeight(iconSize);
    122. painter->drawPixmap(rectIcon, pixMap);
    123. /// ====== 绘制条目文字
    124. if(option.state & QStyle::State_Selected)
    125. {
    126. painter->setOpacity(1);
    127. }
    128. else
    129. {
    130. painter->setOpacity(0.5);
    131. }
    132. painter->setPen(QPen(colorText));
    133. int margin = 72;
    134. if (node->level == 2)
    135. {
    136. margin = 84;
    137. }
    138. QRect rect = option.rect;
    139. rect.setX(rect.x() + margin);
    140. painter->setFont(fontText);
    141. painter->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, index.data(Qt::DisplayRole).toString());
    142. painter->setOpacity(1);
    143. /// ====== 绘制分割线
    144. QRect rectLine = option.rect;
    145. rectLine.setX(option.rect.x()+16);
    146. rectLine.setY(option.rect.y()-1);
    147. rectLine.setWidth(168);
    148. rectLine.setHeight(1);
    149. QPixmap pixMapLine;
    150. pixMapLine.load(":/Images/Line.png");
    151. painter->drawPixmap(rectLine, pixMapLine);
    152. }
    153. NavModel::NavModel(QObject *parent) : QAbstractListModel(parent)
    154. {
    155. }
    156. NavModel::~NavModel()
    157. {
    158. for (std::vector::iterator it = treeNode.begin(); it != treeNode.end();) {
    159. for (std::list::iterator child = (*it)->children.begin(); child != (*it)->children.end();) {
    160. delete(*child);
    161. child = (*it)->children.erase(child);
    162. }
    163. delete(*it);
    164. it = treeNode.erase(it);
    165. }
    166. }
    167. void NavModel::readData(QString path)
    168. {
    169. QFile xml(path);
    170. if (!xml.open(QIODevice::ReadOnly | QIODevice::Text)) {
    171. return;
    172. }
    173. QDomDocument doc;
    174. if (!doc.setContent(&xml, false))
    175. {
    176. return;
    177. }
    178. treeNode.clear();
    179. listNode.clear();
    180. QDomNode root = doc.documentElement().firstChildElement("layout");
    181. QDomNodeList children = root.childNodes();
    182. for (int i = 0; i != children.count(); ++i)
    183. {
    184. QDomElement nodeInfo = children.at(i).toElement();
    185. TreeNode *node = new TreeNode;
    186. node->label = nodeInfo.attribute("label");
    187. node->collapse = nodeInfo.attribute("collapse").toInt();
    188. node->info = nodeInfo.attribute("info");
    189. node->level = 1;
    190. QDomNodeList secondLevel = nodeInfo.childNodes();
    191. for (int j = 0; j != secondLevel.count(); ++j)
    192. {
    193. QDomElement secNodeInfo = secondLevel.at(j).toElement();
    194. TreeNode *secNode = new TreeNode;
    195. secNode->label = secNodeInfo.attribute("label");
    196. secNode->info = secNodeInfo.attribute("info");
    197. secNode->collapse = false;
    198. secNode->level = 2;
    199. secNode->theLast = (j == secondLevel.count() - 1 && i != children.count() - 1);
    200. node->children.push_back(secNode);
    201. }
    202. treeNode.push_back(node);
    203. }
    204. refreshList();
    205. beginResetModel();
    206. endResetModel();
    207. }
    208. void NavModel::setData(QStringList listItem)
    209. {
    210. int count = listItem.count();
    211. if (count == 0) {
    212. return;
    213. }
    214. treeNode.clear();
    215. listNode.clear();
    216. // listItem格式: 标题|父节点标题(父节点为空)|是否展开|提示信息
    217. for (int i = 0; i < count; i++)
    218. {
    219. QString item = listItem.at(i);
    220. QStringList list = item.split("|");
    221. if (list.count() < 4)
    222. {
    223. continue;
    224. }
    225. // 首先先将父节点即父节点标题为空的元素加载完毕
    226. QString title = list.at(0);
    227. QString fatherTitle = list.at(1);
    228. QString collapse = list.at(2);
    229. QString info = list.at(3);
    230. QString iconFile = list.at(4);
    231. if (fatherTitle.isEmpty())
    232. {
    233. TreeNode *node = new TreeNode;
    234. node->label = title;
    235. node->collapse = collapse.toInt();
    236. node->info = info;
    237. node->level = 1;
    238. node->iconName = iconFile;
    239. // 先计算该父节点有多少个子节点
    240. int secCount = 0;
    241. for (int j = 0; j < count; j++)
    242. {
    243. QString secItem = listItem.at(j);
    244. QStringList secList = secItem.split("|");
    245. if (secList.count() < 4)
    246. {
    247. continue;
    248. }
    249. QString secFatherTitle = secList.at(1);
    250. if (secFatherTitle == title)
    251. {
    252. secCount++;
    253. }
    254. }
    255. // 查找该父节点是否有对应子节点,有则加载
    256. int currentCount = 0;
    257. for (int j = 0; j < count; j++)
    258. {
    259. QString secItem = listItem.at(j);
    260. QStringList secList = secItem.split("|");
    261. if (secList.count() < 4)
    262. {
    263. continue;
    264. }
    265. QString secTitle = secList.at(0);
    266. QString secFatherTitle = secList.at(1);
    267. QString secInfo = secList.at(3);
    268. QString secIconName = secList.at(4);
    269. if (secFatherTitle == title)
    270. {
    271. currentCount++;
    272. TreeNode *secNode = new TreeNode;
    273. secNode->label = secTitle;
    274. secNode->info = secInfo;
    275. secNode->collapse = false;
    276. secNode->level = 2;
    277. secNode->theFirst = (currentCount == 1);
    278. secNode->theLast = (currentCount == secCount);
    279. secNode->iconName = secIconName;
    280. node->children.push_back(secNode);
    281. }
    282. }
    283. treeNode.push_back(node);
    284. }
    285. }
    286. refreshList();
    287. beginResetModel();
    288. endResetModel();
    289. }
    290. int NavModel::rowCount(const QModelIndex &parent) const
    291. {
    292. return listNode.size();
    293. }
    294. QVariant NavModel::data(const QModelIndex &index, int role) const
    295. {
    296. if (!index.isValid()) {
    297. return QVariant();
    298. }
    299. if (index.row() >= listNode.size() || index.row() < 0) {
    300. return QVariant();
    301. }
    302. if (role == Qt::DisplayRole) {
    303. return listNode[index.row()].label;
    304. } else if (role == Qt::UserRole) {
    305. return reinterpret_cast(listNode[index.row()].treeNode);
    306. }
    307. return QVariant();
    308. }
    309. void NavModel::refreshList()
    310. {
    311. listNode.clear();
    312. for (std::vector::iterator it = treeNode.begin(); it != treeNode.end(); ++it) {
    313. ListNode node;
    314. node.label = (*it)->label;
    315. node.treeNode = *it;
    316. listNode.push_back(node);
    317. if ((*it)->collapse) {
    318. continue;
    319. }
    320. for (std::list::iterator child = (*it)->children.begin(); child != (*it)->children.end(); ++child) {
    321. ListNode node;
    322. node.label = (*child)->label;
    323. node.treeNode = *child;
    324. node.treeNode->theLast = false;
    325. listNode.push_back(node);
    326. }
    327. if (!listNode.empty()) {
    328. listNode.back().treeNode->theLast = true;
    329. }
    330. }
    331. }
    332. void NavModel::collapse(const QModelIndex &index)
    333. {
    334. TreeNode *node = listNode[index.row()].treeNode;
    335. if (node->children.size() == 0) {
    336. return;
    337. }
    338. node->collapse = !node->collapse;
    339. if (!node->collapse) {
    340. beginInsertRows(QModelIndex(), index.row() + 1, index.row() + node->children.size());
    341. endInsertRows();
    342. } else {
    343. beginRemoveRows(QModelIndex(), index.row() + 1, index.row() + node->children.size());
    344. endRemoveRows();
    345. }
    346. // 刷新放在删除行之后,放在删除行之前可能导致rowCount返回数据错误
    347. refreshList();
    348. }
    349. NavListView::NavListView(QWidget *parent) : QListView(parent)
    350. {
    351. infoVisible = true;
    352. lineVisible = true;
    353. icoColorBg = false;
    354. style = NavListView::IcoStyle_Cross;
    355. colorLine = QColor(214, 216, 224);
    356. colorBgNormal = QColor(239, 241, 250);
    357. colorBgSelected = QColor(133, 153, 216);
    358. colorBgHover = QColor(209, 216, 240);
    359. colorTextNormal = QColor(58, 58, 58);
    360. colorTextSelected = QColor(255, 255, 255);
    361. colorTextHover = QColor(59, 59, 59);
    362. this->setMouseTracking(true);
    363. model = new NavModel(this);
    364. delegate = new NavDelegate(this);
    365. connect(this, SIGNAL(clicked(QModelIndex)), model, SLOT(collapse(QModelIndex)));
    366. }
    367. NavListView::~NavListView()
    368. {
    369. delete model;
    370. delete delegate;
    371. }
    372. void NavListView::readData(QString xmlPath)
    373. {
    374. model->readData(xmlPath);
    375. this->setModel(model);
    376. this->setItemDelegate(delegate);
    377. }
    378. void NavListView::setData(QStringList listItem)
    379. {
    380. model->setData(listItem);
    381. this->setModel(model);
    382. this->setItemDelegate(delegate);
    383. }
    384. void NavListView::setCurrentRow(int row)
    385. {
    386. QModelIndex index = model->index(row, 0);
    387. setCurrentIndex(index);
    388. }
    389. void NavListView::setInfoVisible(bool infoVisible)
    390. {
    391. this->infoVisible = infoVisible;
    392. }
    393. void NavListView::setLineVisible(bool lineVisible)
    394. {
    395. this->lineVisible = lineVisible;
    396. }
    397. void NavListView::setIcoColorBg(bool icoColorBg)
    398. {
    399. this->icoColorBg = icoColorBg;
    400. }
    401. void NavListView::setIcoStyle(NavListView::IcoStyle style)
    402. {
    403. this->style = style;
    404. }
    405. void NavListView::setColorLine(QColor colorLine)
    406. {
    407. this->colorLine = colorLine;
    408. }
    409. void NavListView::setColorBg(QColor colorBgNormal, ///< 正常背景颜色
    410. QColor colorBgSelected,///< 选中背景颜色
    411. QColor colorBgHover) ///< 鼠标悬停背景颜色
    412. {
    413. this->colorBgNormal = colorBgNormal;
    414. this->colorBgSelected = colorBgSelected;
    415. this->colorBgHover = colorBgHover;
    416. }
    417. void NavListView::setColorText(QColor colorTextNormal, ///< 正常字体颜色
    418. QColor colorTextSelected,///< 选中字体颜色
    419. QColor colorTextHover) ///< 鼠标悬停字体颜色
    420. {
    421. this->colorTextNormal = colorTextNormal;
    422. this->colorTextSelected = colorTextSelected;
    423. this->colorTextHover = colorTextHover;
    424. }
    425. void NavListView::setColorBgLeval2(QColor _colorBgNormalLeval2,
    426. QColor _colorBgSelectedLeval2,
    427. QColor _colorBgHoverLeval2)
    428. {
    429. this->colorBgNormalLeval2 = _colorBgNormalLeval2;
    430. this->colorBgSelectedLeval2 = _colorBgSelectedLeval2;
    431. this->colorBgHoverLeval2 = _colorBgHoverLeval2;
    432. }
    433. void NavListView::setColorTextLeval2(QColor _colorTextNormalLeval2,
    434. QColor _colorTextSelectedLeval2,
    435. QColor _colorTextHoverLeval2)
    436. {
    437. this->colorTextNormalLeval2 = _colorTextNormalLeval2;
    438. this->colorTextSelectedLeval2 = _colorTextSelectedLeval2;
    439. this->colorTextHoverLeval2 = _colorTextHoverLeval2;
    440. }

    NavigationList.h

    1. #ifndef NAVIGATIONLIST_H
    2. #define NAVIGATIONLIST_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. QT_BEGIN_NAMESPACE
    8. namespace Ui { class NavigationList; }
    9. QT_END_NAMESPACE
    10. class NavigationList : public QWidget
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit NavigationList(QWidget *parent = nullptr);
    15. ~NavigationList();
    16. void initTreeView();
    17. protected:
    18. void paintEvent(QPaintEvent* _event) override;
    19. public slots:
    20. void slotListViewPressed(const QModelIndex &);
    21. signals:
    22. void signalPageSwitch(QString page); // 页面切换信号
    23. private:
    24. Ui::NavigationList *ui;
    25. bool m_isHideAdditional = true;
    26. };
    27. #endif // NAVIGATIONLIST_H

    NavigationList.cpp

    1. #include "NavigationList.h"
    2. #include "ui_NavigationList.h"
    3. NavigationList::NavigationList(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::NavigationList)
    6. {
    7. ui->setupUi(this);
    8. initTreeView();
    9. connect(ui->listViewNavigation, &NavListView::pressed, this, &NavigationList::slotListViewPressed);
    10. ui->listViewNavigation->setCurrentRow(0);
    11. }
    12. NavigationList::~NavigationList()
    13. {
    14. delete ui;
    15. }
    16. void NavigationList::paintEvent(QPaintEvent* _event)
    17. {
    18. Q_UNUSED(_event)
    19. QStyleOption n_styleOption;
    20. n_styleOption.init(this);
    21. QPainter painter(this);
    22. style()->drawPrimitive(QStyle::PE_Widget, &n_styleOption, &painter, this);
    23. }
    24. void NavigationList::initTreeView()
    25. {
    26. ui->listViewNavigation->setIcoColorBg(false);
    27. ui->listViewNavigation->setColorLine(QColor("#FFFFFF"));
    28. ui->listViewNavigation->setColorBg(QColor("#016BFF"),
    29. QColor("#2A83FF"),
    30. QColor("#2A83FF"));
    31. ui->listViewNavigation->setColorText(QColor("#FFFFFF"),
    32. QColor("#FFFFFF"),
    33. QColor(0, 0, 0));
    34. ui->listViewNavigation->setColorBgLeval2(QColor("#EBF1FF"),QColor("#EBF1FF"),QColor("#EBF1FF"));
    35. ui->listViewNavigation->setColorTextLeval2(QColor("#000000"),
    36. QColor("#000000"),
    37. QColor("#6D6D6D"));
    38. // 设置数据方式
    39. QStringList listItem;
    40. listItem.append(QString::fromLocal8Bit("Tab1||0||:/Images/1.png|"));
    41. listItem.append(QString::fromLocal8Bit("Tab2||0||:/Images/2.png|"));
    42. listItem.append(QString::fromLocal8Bit("Tab3||1||:/Images/3.png|"));
    43. listItem.append(QString::fromLocal8Bit("Tab4|Tab3|||:/Images/4.png|"));
    44. listItem.append(QString::fromLocal8Bit("Tab5|Tab3|||:/Images/5.png|"));
    45. listItem.append(QString::fromLocal8Bit("Tab6|Tab3|||:/Images/6.png|"));
    46. listItem.append(QString::fromLocal8Bit("Tab7|Tab3|||:/Images/7.png|"));
    47. listItem.append(QString::fromLocal8Bit("Tab8||0||:/Images/8.png|"));
    48. listItem.append(QString::fromLocal8Bit("Tab9||0||:/Images/9.png|"));
    49. ui->listViewNavigation->setData(listItem);
    50. }
    51. void NavigationList::slotListViewPressed(const QModelIndex &)
    52. {
    53. // 获取到点击的某一行,再根据点击显示对应的界面
    54. QModelIndex index = ui->listViewNavigation->currentIndex();
    55. QString text = index.data().toString();
    56. emit signalPageSwitch(text);
    57. }

    NavigationList.ui

            只有一个QListView控件,被提升成了上面的NavListView类

            其中listViewNavigation控件添加了如下的样式表:

    1. NavDelegate
    2. {
    3. background-color:"#016BFF";
    4. }
    5. QListView#listViewNavigation
    6. {
    7. border-top-left-radius: 8px;
    8. border-bottom-left-radius: 8px;
    9. background-color:"#016BFF";
    10. }

     

  • 相关阅读:
    二维区间最值差
    Unity-镜头移动的相关逻辑
    Python之property装饰器,上下文管理器与生成器
    windows配置skywalking集群
    5.Docker数据管理
    MacOS 文件系统种类及介绍
    Flutter - 波浪动画和lottie动画的使用
    Logback从添加依赖,到配置给中打印级别,archive相关信息配置,在项目中的常见的用法,一个完整的过程
    第六章 数学(三)
    【精华】多目标跟踪MOT
  • 原文地址:https://blog.csdn.net/Sakuya__/article/details/139594145