赞
踩
实现一个左侧导航界面,单独封装,按下对应的按钮,弹出对应的界面在中间区域
Axure设计图:
需求:按钮,中间区域跳转
跳转界面当然用Qt自带的 QStackedWidget类咯
QStackedWidget类提供了一个小部件堆栈,其中一次只有一个小部件可见。
QStackedWidget可用于创建类似于QTabWidget提供的用户界面。它是建立在QStackedLayout类之上的一个方便的布局小部件。
左侧导航页面也是相当简单的:qt designer
咱们有啥拖啥,QPushButton,拖起来
- <?xml version="1.0" encoding="UTF-8"?>
- <ui version="4.0">
- <class>DebugLeftWidget</class>
- <widget class="QWidget" name="DebugLeftWidget">
- <property name="geometry">
- <rect>
- <x>0</x>
- <y>0</y>
- <width>157</width>
- <height>354</height>
- </rect>
- </property>
- <property name="windowTitle">
- <string>Form</string>
- </property>
- <property name="styleSheet">
- <string notr="true">#DebugLeftWidget
- {
- background-color:#383F47;
- border-width:1px;
- }
-
- #pushButton_0 {
- qproperty-icon: url(:/resource/image/systemPage.png);
- background-repeat: no-repeat;
- background-origin: padding;
- background-position: left center;
- background-color: rgb(56, 63, 71);
- min-height: 30px;
- color:white;
- text-align: left center;
- padding-left: 30px;
- padding-top: 1px;
-
- border-style: solid;
- border-width: 2px;
- border-color: rgb(121, 121, 121);
- border-radius:5px;
-
- }
-
- #pushButton_1 {
- qproperty-icon: url(:/resource/image/testPage.png);
- background-repeat: no-repeat;
- background-origin: padding;
- background-position: left center;
- background-color: rgb(56, 63, 71);
- min-height: 30px;
- color:white;
- text-align: left center;
- padding-left: 30px;
- padding-top: 1px;
-
- border-style: solid;
- border-width: 2px;
- border-color: rgb(121, 121, 121);
- border-radius:5px;
- }
-
- #pushButton_2 {
- qproperty-icon: url(:/resource/image/tablePage.png);
- background-repeat: no-repeat;
- background-origin: padding;
- background-position: left center;
- background-color: rgb(56, 63, 71);
- min-height: 30px;
- color:white;
- text-align: left center;
- padding-left: 30px;
- padding-top: 1px;
-
- border-style: solid;
- border-width: 2px;
- border-color: rgb(121, 121, 121);
- border-radius:5px;
- }
-
- #pushButton_3 {
- qproperty-icon: url(:/resource/image/shrinkage2.png);
- background-repeat: no-repeat;
- background-origin: padding;
- background-position: left center;
- background-color: rgb(56, 63, 71);
- min-height: 30px;
- color:white;
- text-align: left center;
- padding-left: 30px;
- padding-top: 1px;
-
- border-style: solid;
- border-width: 2px;
- border-color: rgb(121, 121, 121);
- border-radius:5px;
- }
-
-
- #shrinkBtn{
- qproperty-icon: url(:/resource/image/shrinkage2.png);
- background-repeat: no-repeat;
- background-origin: padding;
- background-position: left center;
- background-color: rgb(56, 63, 71);
- }
-
- #unfoldBtn{
- qproperty-icon: url(:/resource/image/unfold2.png);
- background-repeat: no-repeat;
- background-origin: padding;
- background-position: left center;
- background-color: rgb(56, 63, 71);
- }
-
- </string>
- </property>
- <layout class="QGridLayout" name="gridLayout">
- <item row="0" column="0" colspan="2">
- <spacer name="horizontalSpacer">
- <property name="orientation">
- <enum>Qt::Horizontal</enum>
- </property>
- <property name="sizeHint" stdset="0">
- <size>
- <width>85</width>
- <height>20</height>
- </size>
- </property>
- </spacer>
- </item>
- <item row="0" column="2">
- <widget class="QPushButton" name="shrinkBtn">
- <property name="maximumSize">
- <size>
- <width>16</width>
- <height>16</height>
- </size>
- </property>
- <property name="styleSheet">
- <string notr="true">QPushButton:hover{
- background-color: rgb(98,102,105);
- }
- QPushButton:pressed{
- color: black;
- background-color: rgb(153,153,153);
- }</string>
- </property>
- <property name="text">
- <string/>
- </property>
- </widget>
- </item>
- <item row="1" column="0" colspan="3">
- <widget class="QPushButton" name="pushButton_0">
- <property name="minimumSize">
- <size>
- <width>0</width>
- <height>35</height>
- </size>
- </property>
- <property name="styleSheet">
- <string notr="true">QPushButton:hover{
- background-color: rgb(98,102,105);
- }
- QPushButton:pressed{
- color: black;
- background-color: rgb(153,153,153);
- }</string>
- </property>
- <property name="text">
- <string>系统设置</string>
- </property>
- <property name="checkable">
- <bool>true</bool>
- </property>
- <property name="checked">
- <bool>true</bool>
- </property>
- </widget>
- </item>
- <item row="2" column="0" colspan="3">
- <widget class="QPushButton" name="pushButton_1">
- <property name="minimumSize">
- <size>
- <width>0</width>
- <height>35</height>
- </size>
- </property>
- <property name="styleSheet">
- <string notr="true">QPushButton:hover{
- background-color: rgb(98,102,105);
- }
- QPushButton:pressed{
- color: black;
- background-color: rgb(153,153,153);
- }</string>
- </property>
- <property name="text">
- <string>测试参数</string>
- </property>
- <property name="checkable">
- <bool>false</bool>
- </property>
- </widget>
- </item>
- <item row="3" column="0" colspan="3">
- <widget class="QPushButton" name="pushButton_2">
- <property name="minimumSize">
- <size>
- <width>0</width>
- <height>35</height>
- </size>
- </property>
- <property name="styleSheet">
- <string notr="true">QPushButton:hover{
- background-color: rgb(98,102,105);
- }
- QPushButton:pressed{
- color: black;
- background-color: rgb(153,153,153);
- }</string>
- </property>
- <property name="text">
- <string>表格参数</string>
- </property>
- <property name="checkable">
- <bool>false</bool>
- </property>
- </widget>
- </item>
- <item row="4" column="0" colspan="3">
- <widget class="QPushButton" name="pushButton_3">
- <property name="minimumSize">
- <size>
- <width>0</width>
- <height>35</height>
- </size>
- </property>
- <property name="styleSheet">
- <string notr="true">QPushButton:hover{
- background-color: rgb(98,102,105);
- }
- QPushButton:pressed{
- color: black;
- background-color: rgb(153,153,153);
- }</string>
- </property>
- <property name="text">
- <string>PushButton</string>
- </property>
- <property name="checkable">
- <bool>false</bool>
- </property>
- </widget>
- </item>
- <item row="5" column="1">
- <spacer name="verticalSpacer">
- <property name="orientation">
- <enum>Qt::Vertical</enum>
- </property>
- <property name="sizeHint" stdset="0">
- <size>
- <width>20</width>
- <height>102</height>
- </size>
- </property>
- </spacer>
- </item>
- <item row="6" column="0">
- <widget class="QPushButton" name="unfoldBtn">
- <property name="maximumSize">
- <size>
- <width>16</width>
- <height>16</height>
- </size>
- </property>
- <property name="styleSheet">
- <string notr="true">QPushButton:hover{
- background-color: rgb(98,102,105);
- }
- QPushButton:pressed{
- color: black;
- background-color: rgb(153,153,153);
- }</string>
- </property>
- <property name="text">
- <string/>
- </property>
- </widget>
- </item>
- <item row="6" column="1" colspan="2">
- <spacer name="horizontalSpacer_2">
- <property name="orientation">
- <enum>Qt::Horizontal</enum>
- </property>
- <property name="sizeHint" stdset="0">
- <size>
- <width>85</width>
- <height>20</height>
- </size>
- </property>
- </spacer>
- </item>
- </layout>
- </widget>
- <resources/>
- <connections/>
- </ui>
.h文件
- #ifndef DEBUGLEFTWIDGET_H
- #define DEBUGLEFTWIDGET_H
-
- #include <QWidget>
- #include <QAbstractButton>
-
- namespace Ui {
- class DebugLeftWidget;
- }
-
- class DebugLeftWidget : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit DebugLeftWidget(QWidget *parent = 0);
- ~DebugLeftWidget();
-
- signals:
- void changePage(int index);
-
- private:
- void init();
- void initConnect();
-
- private slots:
- void btnClicked();
-
- private:
- Ui::DebugLeftWidget *ui;
- QList<QAbstractButton *> btnsMain;
- QString strBtnStyle;
- };
-
- #endif // DEBUGLEFTWIDGET_H
.cpp文件
- #include "debugleftwidget.h"
- #include "ui_debugleftwidget.h"
- #include <QDebug>
- #include <QPalette>
-
- DebugLeftWidget::DebugLeftWidget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::DebugLeftWidget)
- {
- ui->setupUi(this);
- init();
- initConnect();
- }
-
- DebugLeftWidget::~DebugLeftWidget()
- {
- delete ui;
- }
-
- void DebugLeftWidget::init()
- {
- btnsMain << ui->pushButton_0
- << ui->pushButton_1
- << ui->pushButton_2
- << ui->pushButton_3;
- strBtnStyle = ui->pushButton_0->styleSheet();
- }
-
- void DebugLeftWidget::initConnect()
- {
- for (int i = 0; i < btnsMain.size(); ++i) {
- QPushButton *btn = (QPushButton *)btnsMain.at(i);
- connect(btn, &QPushButton::clicked, this, &DebugLeftWidget::btnClicked);
- }
- ui->pushButton_0->click();
- }
-
- void DebugLeftWidget::btnClicked()
- {
- QPushButton *btn = qobject_cast<QPushButton *>(sender());
- foreach (auto elemBtn, btnsMain) {
- if(elemBtn->objectName() == btn->objectName()){
- elemBtn->setStyleSheet("background: rgb(98,102,105)");
- // qDebug() << "object name: " << btn->objectName().mid(11);
- emit changePage(btn->objectName().mid(11).toInt());
- }else{
- elemBtn->setStyleSheet(strBtnStyle);
- }
- }
- }
代码太简单,就不用多说了,可以直接拷贝过去用
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。