当前位置:   article > 正文

Qt做导航页面左侧_qt左侧导航栏

qt左侧导航栏

解决的问题:

实现一个左侧导航界面,单独封装,按下对应的按钮,弹出对应的界面在中间区域

Axure设计图:

需求:按钮,中间区域跳转


 

如何实现:

跳转界面当然用Qt自带的 QStackedWidget类咯

QStackedWidget类提供了一个小部件堆栈,其中一次只有一个小部件可见。

QStackedWidget可用于创建类似于QTabWidget提供的用户界面。它是建立在QStackedLayout类之上的一个方便的布局小部件。

左侧导航页面也是相当简单的:qt designer

咱们有啥拖啥,QPushButton,拖起来


 

 

 

代码块解析:

.ui文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>DebugLeftWidget</class>
  4. <widget class="QWidget" name="DebugLeftWidget">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>157</width>
  10. <height>354</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>Form</string>
  15. </property>
  16. <property name="styleSheet">
  17. <string notr="true">#DebugLeftWidget
  18. {
  19. background-color:#383F47;
  20. border-width:1px;
  21. }
  22. #pushButton_0 {
  23. qproperty-icon: url(:/resource/image/systemPage.png);
  24. background-repeat: no-repeat;
  25. background-origin: padding;
  26. background-position: left center;
  27. background-color: rgb(56, 63, 71);
  28. min-height: 30px;
  29. color:white;
  30. text-align: left center;
  31. padding-left: 30px;
  32. padding-top: 1px;
  33. border-style: solid;
  34. border-width: 2px;
  35. border-color: rgb(121, 121, 121);
  36. border-radius:5px;
  37. }
  38. #pushButton_1 {
  39. qproperty-icon: url(:/resource/image/testPage.png);
  40. background-repeat: no-repeat;
  41. background-origin: padding;
  42. background-position: left center;
  43. background-color: rgb(56, 63, 71);
  44. min-height: 30px;
  45. color:white;
  46. text-align: left center;
  47. padding-left: 30px;
  48. padding-top: 1px;
  49. border-style: solid;
  50. border-width: 2px;
  51. border-color: rgb(121, 121, 121);
  52. border-radius:5px;
  53. }
  54. #pushButton_2 {
  55. qproperty-icon: url(:/resource/image/tablePage.png);
  56. background-repeat: no-repeat;
  57. background-origin: padding;
  58. background-position: left center;
  59. background-color: rgb(56, 63, 71);
  60. min-height: 30px;
  61. color:white;
  62. text-align: left center;
  63. padding-left: 30px;
  64. padding-top: 1px;
  65. border-style: solid;
  66. border-width: 2px;
  67. border-color: rgb(121, 121, 121);
  68. border-radius:5px;
  69. }
  70. #pushButton_3 {
  71. qproperty-icon: url(:/resource/image/shrinkage2.png);
  72. background-repeat: no-repeat;
  73. background-origin: padding;
  74. background-position: left center;
  75. background-color: rgb(56, 63, 71);
  76. min-height: 30px;
  77. color:white;
  78. text-align: left center;
  79. padding-left: 30px;
  80. padding-top: 1px;
  81. border-style: solid;
  82. border-width: 2px;
  83. border-color: rgb(121, 121, 121);
  84. border-radius:5px;
  85. }
  86. #shrinkBtn{
  87. qproperty-icon: url(:/resource/image/shrinkage2.png);
  88. background-repeat: no-repeat;
  89. background-origin: padding;
  90. background-position: left center;
  91. background-color: rgb(56, 63, 71);
  92. }
  93. #unfoldBtn{
  94. qproperty-icon: url(:/resource/image/unfold2.png);
  95. background-repeat: no-repeat;
  96. background-origin: padding;
  97. background-position: left center;
  98. background-color: rgb(56, 63, 71);
  99. }
  100. </string>
  101. </property>
  102. <layout class="QGridLayout" name="gridLayout">
  103. <item row="0" column="0" colspan="2">
  104. <spacer name="horizontalSpacer">
  105. <property name="orientation">
  106. <enum>Qt::Horizontal</enum>
  107. </property>
  108. <property name="sizeHint" stdset="0">
  109. <size>
  110. <width>85</width>
  111. <height>20</height>
  112. </size>
  113. </property>
  114. </spacer>
  115. </item>
  116. <item row="0" column="2">
  117. <widget class="QPushButton" name="shrinkBtn">
  118. <property name="maximumSize">
  119. <size>
  120. <width>16</width>
  121. <height>16</height>
  122. </size>
  123. </property>
  124. <property name="styleSheet">
  125. <string notr="true">QPushButton:hover{
  126. background-color: rgb(98,102,105);
  127. }
  128. QPushButton:pressed{
  129. color: black;
  130. background-color: rgb(153,153,153);
  131. }</string>
  132. </property>
  133. <property name="text">
  134. <string/>
  135. </property>
  136. </widget>
  137. </item>
  138. <item row="1" column="0" colspan="3">
  139. <widget class="QPushButton" name="pushButton_0">
  140. <property name="minimumSize">
  141. <size>
  142. <width>0</width>
  143. <height>35</height>
  144. </size>
  145. </property>
  146. <property name="styleSheet">
  147. <string notr="true">QPushButton:hover{
  148. background-color: rgb(98,102,105);
  149. }
  150. QPushButton:pressed{
  151. color: black;
  152. background-color: rgb(153,153,153);
  153. }</string>
  154. </property>
  155. <property name="text">
  156. <string>系统设置</string>
  157. </property>
  158. <property name="checkable">
  159. <bool>true</bool>
  160. </property>
  161. <property name="checked">
  162. <bool>true</bool>
  163. </property>
  164. </widget>
  165. </item>
  166. <item row="2" column="0" colspan="3">
  167. <widget class="QPushButton" name="pushButton_1">
  168. <property name="minimumSize">
  169. <size>
  170. <width>0</width>
  171. <height>35</height>
  172. </size>
  173. </property>
  174. <property name="styleSheet">
  175. <string notr="true">QPushButton:hover{
  176. background-color: rgb(98,102,105);
  177. }
  178. QPushButton:pressed{
  179. color: black;
  180. background-color: rgb(153,153,153);
  181. }</string>
  182. </property>
  183. <property name="text">
  184. <string>测试参数</string>
  185. </property>
  186. <property name="checkable">
  187. <bool>false</bool>
  188. </property>
  189. </widget>
  190. </item>
  191. <item row="3" column="0" colspan="3">
  192. <widget class="QPushButton" name="pushButton_2">
  193. <property name="minimumSize">
  194. <size>
  195. <width>0</width>
  196. <height>35</height>
  197. </size>
  198. </property>
  199. <property name="styleSheet">
  200. <string notr="true">QPushButton:hover{
  201. background-color: rgb(98,102,105);
  202. }
  203. QPushButton:pressed{
  204. color: black;
  205. background-color: rgb(153,153,153);
  206. }</string>
  207. </property>
  208. <property name="text">
  209. <string>表格参数</string>
  210. </property>
  211. <property name="checkable">
  212. <bool>false</bool>
  213. </property>
  214. </widget>
  215. </item>
  216. <item row="4" column="0" colspan="3">
  217. <widget class="QPushButton" name="pushButton_3">
  218. <property name="minimumSize">
  219. <size>
  220. <width>0</width>
  221. <height>35</height>
  222. </size>
  223. </property>
  224. <property name="styleSheet">
  225. <string notr="true">QPushButton:hover{
  226. background-color: rgb(98,102,105);
  227. }
  228. QPushButton:pressed{
  229. color: black;
  230. background-color: rgb(153,153,153);
  231. }</string>
  232. </property>
  233. <property name="text">
  234. <string>PushButton</string>
  235. </property>
  236. <property name="checkable">
  237. <bool>false</bool>
  238. </property>
  239. </widget>
  240. </item>
  241. <item row="5" column="1">
  242. <spacer name="verticalSpacer">
  243. <property name="orientation">
  244. <enum>Qt::Vertical</enum>
  245. </property>
  246. <property name="sizeHint" stdset="0">
  247. <size>
  248. <width>20</width>
  249. <height>102</height>
  250. </size>
  251. </property>
  252. </spacer>
  253. </item>
  254. <item row="6" column="0">
  255. <widget class="QPushButton" name="unfoldBtn">
  256. <property name="maximumSize">
  257. <size>
  258. <width>16</width>
  259. <height>16</height>
  260. </size>
  261. </property>
  262. <property name="styleSheet">
  263. <string notr="true">QPushButton:hover{
  264. background-color: rgb(98,102,105);
  265. }
  266. QPushButton:pressed{
  267. color: black;
  268. background-color: rgb(153,153,153);
  269. }</string>
  270. </property>
  271. <property name="text">
  272. <string/>
  273. </property>
  274. </widget>
  275. </item>
  276. <item row="6" column="1" colspan="2">
  277. <spacer name="horizontalSpacer_2">
  278. <property name="orientation">
  279. <enum>Qt::Horizontal</enum>
  280. </property>
  281. <property name="sizeHint" stdset="0">
  282. <size>
  283. <width>85</width>
  284. <height>20</height>
  285. </size>
  286. </property>
  287. </spacer>
  288. </item>
  289. </layout>
  290. </widget>
  291. <resources/>
  292. <connections/>
  293. </ui>

.h文件

  1. #ifndef DEBUGLEFTWIDGET_H
  2. #define DEBUGLEFTWIDGET_H
  3. #include <QWidget>
  4. #include <QAbstractButton>
  5. namespace Ui {
  6. class DebugLeftWidget;
  7. }
  8. class DebugLeftWidget : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit DebugLeftWidget(QWidget *parent = 0);
  13. ~DebugLeftWidget();
  14. signals:
  15. void changePage(int index);
  16. private:
  17. void init();
  18. void initConnect();
  19. private slots:
  20. void btnClicked();
  21. private:
  22. Ui::DebugLeftWidget *ui;
  23. QList<QAbstractButton *> btnsMain;
  24. QString strBtnStyle;
  25. };
  26. #endif // DEBUGLEFTWIDGET_H

.cpp文件

  1. #include "debugleftwidget.h"
  2. #include "ui_debugleftwidget.h"
  3. #include <QDebug>
  4. #include <QPalette>
  5. DebugLeftWidget::DebugLeftWidget(QWidget *parent) :
  6. QWidget(parent),
  7. ui(new Ui::DebugLeftWidget)
  8. {
  9. ui->setupUi(this);
  10. init();
  11. initConnect();
  12. }
  13. DebugLeftWidget::~DebugLeftWidget()
  14. {
  15. delete ui;
  16. }
  17. void DebugLeftWidget::init()
  18. {
  19. btnsMain << ui->pushButton_0
  20. << ui->pushButton_1
  21. << ui->pushButton_2
  22. << ui->pushButton_3;
  23. strBtnStyle = ui->pushButton_0->styleSheet();
  24. }
  25. void DebugLeftWidget::initConnect()
  26. {
  27. for (int i = 0; i < btnsMain.size(); ++i) {
  28. QPushButton *btn = (QPushButton *)btnsMain.at(i);
  29. connect(btn, &QPushButton::clicked, this, &DebugLeftWidget::btnClicked);
  30. }
  31. ui->pushButton_0->click();
  32. }
  33. void DebugLeftWidget::btnClicked()
  34. {
  35. QPushButton *btn = qobject_cast<QPushButton *>(sender());
  36. foreach (auto elemBtn, btnsMain) {
  37. if(elemBtn->objectName() == btn->objectName()){
  38. elemBtn->setStyleSheet("background: rgb(98,102,105)");
  39. // qDebug() << "object name: " << btn->objectName().mid(11);
  40. emit changePage(btn->objectName().mid(11).toInt());
  41. }else{
  42. elemBtn->setStyleSheet(strBtnStyle);
  43. }
  44. }
  45. }

代码太简单,就不用多说了,可以直接拷贝过去用

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读