#ifndef DRAWER_HPP#define DRAWER_HPP#include <QToolBox>#include <QLayout>#include "listwidget.hpp"#include "listwidgetitem.hpp"class Drawer : public QToolBox{ Q_OBJECTpublic: Drawer(const QIcon &iconOpen, const QIcon &iconClose, QWidget *parent = 0,Qt::WindowFlags f = 0); void setSpacing(int value) { layout()->setSpacing(value); } /** * @brief addErea 在drawer中開闢一塊地區 * @param lable */ void addErea(const QString &label); /** * @brief addEreaElem 向指定的地區中添加一個元素 * @param _index 地區下標 從零開始 * @param item 元素 */ void addEreaElem(int _index, QListWidgetItem *item); /** * @brief addEreaElem 向指定的地區中添加一個元素 * @param _index 地區名稱 * @param item 元素 */ void addEreaElem(const QString &label, QListWidgetItem *item);signals: /** * @brief sig_click 那個地區的那個元素被點擊 * @param ereaIndex 地區下標 * @param elemIndex 元素下標 */ void sig_click(int ereaIndex, int elemIndex);public slots:private slots: void slt_elemClick(QModelIndex _index); /** * @brief slt_currentIndexActive 當前的那個地區是活動的,來來改變表徵圖 * @param _index */ void slt_currentIndexActive(int _index);private: /** * @brief openIcon */ QIcon openIcon; /** * @brief closeIcon */ QIcon closeIcon; /** * @brief preIndex 前一個即時區域的下標 */ int preIndex; QMap<QString,QListWidget*> ereaLabels; };#endif // DRAWER_HPP#include "drawer.hpp"Drawer::Drawer(const QIcon &iconOpen, const QIcon &iconClose, QWidget *parent, Qt::WindowFlags f) : QToolBox(parent, f), openIcon(iconOpen), closeIcon(iconClose), preIndex(-1){ connect(this,SIGNAL(currentChanged(int)),this,SLOT(slt_currentIndexActive(int)));}void Drawer::slt_currentIndexActive(int _index){ if (preIndex != -1) { setItemIcon(preIndex,closeIcon); } setItemIcon(_index,openIcon); preIndex = _index;}void Drawer::addErea(const QString &label){ if (!ereaLabels.contains(label)) { ListWidget *erea = new ListWidget(); connect(erea,SIGNAL(clicked(QModelIndex)),this,SLOT(slt_elemClick(QModelIndex))); int index = addItem(static_cast<QWidget*>(erea),label); setItemIcon(index,openIcon); ereaLabels.insert(label,erea); }}void Drawer::addEreaElem(const QString &label, QListWidgetItem *item){ if (ereaLabels.contains(label)) { QListWidget *w = ereaLabels[label]; w->addItem(item); } else { delete item; }}void Drawer::addEreaElem(int _index, QListWidgetItem *item){ QWidget *w = widget(_index); if (w) { static_cast<QListWidget*>(w)->addItem(item); }}void Drawer::slt_elemClick(QModelIndex _index){ int i = _index.row(); QWidget * w = static_cast<QWidget*> (sender()); int j = indexOf(w); emit sig_click(j,i);}
其中繼承了QListWidget和QListWIdgetItem來提供一些特殊的實現,不過示範的時候沒有用。
還有一個問題就是QListWidget除了項之外,還有好多的空白項
代碼:http://www.kuaipan.cn/file/id_45404676266394511.htm
修複一個bug
void Drawer::addErea(const QString &label){ if (!ereaLabels.contains(label)) { ListWidget *erea = new ListWidget(); connect(erea,SIGNAL(clicked(QModelIndex)),this,SLOT(slt_elemClick(QModelIndex))); int index = addItem(static_cast<QWidget*>(erea),label);//當添加第一個時,回觸發currentChanged訊號,再回到這裡時,表徵圖為close,其實是open if (index != 0) { setItemIcon(index,closeIcon); } ereaLabels.insert(label,erea); }}