左侧边栏按钮及图片样式
有时会用到左侧边栏,侧边栏上有按钮,点击按钮切换不同页面。
侧边栏类
cpp文件
1 #include "tabtitle.h" 2 3 TabTitle::TabTitle(int x, int y) 4 { 5 m_state = false; 6 m_titleItem = new QList<QPushButton*>(); 7 m_currentIndex = -1; 8 m_left = x; 9 m_top = y; 10 m_lastIndex = -1; 11 12 m_normalImg = ""; 13 m_hoverImage = ""; 14 m_checkedImage = ""; 15 connect(this,SIGNAL(titleOnClick()),this,SLOT(titleItemOnClick())); 16 } 17 18 void TabTitle::titleItemOnClick(/*QString title*/) 19 { 20 21 if(m_lastIndex != -1){ 22 m_titleItem->at(m_lastIndex)->setChecked(false); 23 m_titleItem->at(m_currentIndex)->setChecked(true); 24 }else { 25 m_titleItem->at(m_currentIndex)->setChecked(true); 26 } 27 m_lastIndex = m_currentIndex; 28 } 29 QPushButton * TabTitle::createTitleBtn(QString imgUrl,QString hoverImgUrl,QString checkedImgUrl,QRect rect,QWidget* parent) 30 { 31 QPushButton *_btn = new QPushButton(parent); 32 _btn->setCheckable(true); 33 _btn->setGeometry(rect); 34 QString str = "QPushButton{border-image:url("+imgUrl+");}" 35 "QPushButton:hover{" 36 "border-image:url("+hoverImgUrl+");}" 37 "QPushButton:checked{" 38 "border-image:url("+checkedImgUrl+");}"; 39 _btn->setStyleSheet(str); 40 connect(_btn,&QPushButton::clicked,this,[=](){ 41 if(!m_state) 42 { 43 _btn->setStyleSheet(str); 44 m_currentIndex = m_titleItem->indexOf(_btn); 45 emit titleOnClick(); 46 } 47 else 48 { 49 _btn->setStyleSheet("QPushButton{border-image:url("+imgUrl+");}"); 50 } 51 52 }); 53 return _btn; 54 } 55 56 void TabTitle::addTitleItem(QString imgUrl, QString hoverImgUrl, QString checkedImgUrl, QWidget* parent) 57 { 58 QRect rect; 59 rect.setRect(m_left,m_top+m_titleItem->count()*103,114,103); 60 m_titleItem->push_back(createTitleBtn(imgUrl,hoverImgUrl,checkedImgUrl,rect,parent)); 61 } 62 63 void TabTitle::deleteTitleItem(QString title) 64 { 65 for (int i =0 ; i < m_titleItem->count(); i++) { 66 if(m_titleItem->at(i)->text() != title){ 67 continue; 68 }else { 69 m_titleItem->removeAt(i); 70 return; 71 } 72 } 73 } 74 75 int TabTitle::getCurrentIndex() 76 { 77 return m_currentIndex; 78 } 79 80 void TabTitle::setCurrentIndex(int index) 81 { 82 m_currentIndex = index; 83 emit titleOnClick(); 84 }
h文件
#ifndef TABTITLE_H #define TABTITLE_H #include <QObject> #include <QWidget> #include <QPalette> #include <QImage> #include <QFont> #include <QWidget> #include <QList> #include <QSize> #include <QPushButton> class TabTitle: public QObject { Q_OBJECT public: TabTitle(int x, int y); void addTitleItem(QString imgUrl,QString hoverImgUrl, QString checkedImgUrl,QWidget* parent); void deleteTitleItem(QString title); int getCurrentIndex(); void setCurrentIndex(int index); public: bool m_state; signals: void titleOnClick(/*QString title*/); private slots: void titleItemOnClick(/*QString title*/); private: QPushButton *createTitleBtn(QString imgUrl, QString hoverImgUrl, QString checkedImgUrl, QRect rect, QWidget *parent); private: QList<QPushButton*> *m_titleItem; int m_currentIndex; int m_left,m_top; int m_lastIndex; QString m_normalImg,m_hoverImage,m_checkedImage; }; #endif // TABTITLE_H
使用方法:
在mainwindow.h中,
TabTitle *m_titleBar; //!左侧Tab页面
mainwindow.cpp文件
void MainWindow::InitUi() { m_titleBar = new TabTitle(0,54); m_titleBar->addTitleItem("../Bin/skin/image/normal_1.png","../Bin/skin/image/hover_1.png","../Bin/skin/image/checked_1.png",this); m_titleBar->addTitleItem("../Bin/skin/image/normal_2.png","../Bin/skin/image/hover_2.png","../Bin/skin/image/checked_2.png",this); m_titleBar->addTitleItem("../Bin/skin/image/normal_3.png","../Bin/skin/image/hover_3.png","../Bin/skin/image/checked_3.png",this); m_titleBar->setCurrentIndex(0); connect(m_titleBar,SIGNAL(titleOnClick()),this,SLOT(titleBarOnClick())); } //槽函数 void MainWindow::titleBarOnClick() { int nIndex = m_titleBar->getCurrentIndex(); ui->stackedWidget->setCurrentIndex(nIndex); switch (nIndex) { case 0: break; case 1: case 2: case 3: case 4: case 5: break; case 6: break; case 7: break; case 8: break; case 9: break; default: break; } }