Qt之实现下拉列表菜单

时间:2023-01-29 10:09:09

一、简介

      在QGraphicsItem下使用QListWidget实现下拉列表的功能,并显示所有的选项,便于浏览和查询数据。
Qt之实现下拉列表菜单

二、详解

1、部分代码

(1)clusterlistwidget.h

[html]  view plain  copy
  1. #ifndef CLUSTERLISTWIDGET_H  
  2. #define CLUSTERLISTWIDGET_H  
  3. #include <QWidget>  
  4. #include <QtGui>  
  5. #include "custombtn.h"  
  6. #include "pagenumbercontrol.h"  
  7. class SearchLineEdit;  
  8. enum TYPE{cluster=0,host,vm};  
  9. class ClusterListWidget : public QWidget  
  10. {  
  11.     Q_OBJECT  
  12. public:  
  13.     explicit ClusterListWidget(TYPE type = cluster,QWidget *parent = 0);  
  14.   
  15.     void setListWidget();  
  16.     void setTitle(QString title);  
  17.   
  18.     void listInit();  
  19. signals:  
  20.   
  21. public slots:  
  22.     void textChange(QString text);  
  23.     void pageChange(int currentnumber);  
  24.     void editClicked();  
  25.     void btnClicked();  
  26.     void itemEnteredSlot(QListWidgetItem * i);  
  27.     void itemClickedSlot(QListWidgetItem * i);  
  28.   
  29.     void rowChanged(int row);  
  30. protected:  
  31.     //bool eventFilter(QObject * obj, QEvent *e);  
  32.     void paintEvent(QPaintEvent *e);  
  33.     void showEvent(QShowEvent *e);  
  34.     void keyPressEvent(QKeyEvent *e);  
  35. private:  
  36.     void setImgs(const QString &normal,const QString &abnormal,const QString &error);  
  37.     void setVmImgs(const QString &linux_normal,const QString &linux_abnormal,const QString &linux_error,const QString &linux_off,  
  38.                    const QString &windows_normal,const QString &windows_abnormal,const QString &windows_error,const QString &windows_off);  
  39. private:  
  40.     //QString _icon;  
  41.     QPixmap backGroundPix;  
  42.     QStringList NameList;  
  43.     QStringList _searchList;  
  44.     QStringList _list;  
  45.     QLabel *lb1;  
  46.     QLabel *lb2;  
  47.     PageNumberControl *_page;  
  48.   
  49.     int _maxNum;  
  50.     int _currentNum;  
  51.     int _searchNUm;  
  52.     SearchLineEdit *_searchLineEdit;  
  53.     QListWidget * _cluster;  
  54.     QPushButton *_btn;  
  55.     int _n;//记录键盘 上下见  
  56.   
  57.     int _x;  
  58.     int _y;  
  59.     QPixmap _enterPixmap;  
  60.     bool _enter;  
  61.   
  62.     QString _title;  
  63.     QString _normal;  
  64.     QString _abnormal;  
  65.     QString __error;  
  66.   
  67.     TYPE _type;  
  68.   
  69.     QString _linux_normal;  
  70.     QString _linux_abnormal;  
  71.     QString _linux_error;  
  72.     QString _linux_off;  
  73.     QString _windows_normal;  
  74.     QString _windows_abnormal;  
  75.     QString _windows_error;  
  76.     QString _windows_off;  
  77. };  
  78. class SearchLineEdit : public QLineEdit  
  79. {  
  80.     Q_OBJECT  
  81.  public:  
  82.     explicit SearchLineEdit(QWidget *parent = 0);  
  83.  signals:  
  84.     void pressed();  
  85.  protected:  
  86.     void focusInEvent(QFocusEvent *e);  
  87.     void focusOutEvent(QFocusEvent *e);  
  88.  private:  
  89. };  
  90. #endif // CLUSTERLISTWIDGET_H  
(2)clusterlistwidget.cpp
[html]  view plain  copy
  1. #include "clusterlistwidget.h"  
  2.   
  3. ClusterListWidget::ClusterListWidget(TYPE type,QWidget *parent) :  
  4.     QWidget(parent,Qt::FramelessWindowHint)  
  5.   ,_maxNum(0)  
  6.   ,_searchNUm(0)  
  7.   ,_n(-1)  
  8.   ,_x(0)  
  9.   ,_y(0)  
  10.   ,_enter(false)  
  11. {  
  12.     NameList<<"a"<<"b"<<"c"<<"d"<<"e"<<"a"<<"b"<<"c"<<"d"<<"e"  
  13.              <<"a"<<"b"<<"c"<<"d"<<"e"<<"a"<<"b"<<"c"<<"d"<<"e"  
  14.                <<"a"<<"b"<<"c"<<"d"<<"e"<<"a"<<"b"<<"c"<<"d"<<"e"  
  15.                  <<"a"<<"b"<<"c"<<"d"<<"e"<<"a"<<"b"<<"c"<<"d"<<"e"<<"hjhjha";  
  16.     backGroundPix = QPixmap(":/images/handleMenu_bg.png");  
  17.     resize(backGroundPix.width(),  backGroundPix.height());  
  18.     qDebug()<<backGroundPix.width()<<backGroundPix.height();  
  19.     QPalette pal = palette();  
  20.     pal.setColor(QPalette::Background, QColor(0xFF,0xFF,0xFF,0x00));  
  21.     setPalette(pal);  
  22.     _listNameList;  
  23.     _type = type;  
  24.     lb1 = new QLabel(this);  
  25.     lb1->setPixmap(QPixmap(":/images/handleMenu_allClusters.png"));  
  26.     lb1->move(10,12);  
  27.   
  28.     lb2 = new QLabel(this);  
  29.     lb2->move(33,12);  
  30.     lb2->setStyleSheet("color:#b4e5fd");  
  31.   
  32.     _page = new PageNumberControl(this);  
  33.     _page->setCluster(true);  
  34.     _maxNum = (_list.count() - 1) / 16 +1;  
  35.     _currentNum = 1;  
  36.     _page->setTotalPage(_maxNum);  
  37.     _page->move(145,12);  
  38.   
  39.     _searchLineEdit = new SearchLineEdit(this);  
  40. //    _searchLineEdit->setImgs(":/images/handleMenu_searchInput.png",":/images/handleMenu_searchInput_click.png",  
  41. //                             ":/images/handleMenu_searchInput.png",":/images/handleMenu_searchInput.png");  
  42.     _searchLineEdit->setStyleSheet("background-image:url(:/images/handleMenu_searchInput.png);border:1px solid #536874;");  
  43.     _searchLineEdit->move(8,38);  
  44.     _searchLineEdit->resize(168,23);  
  45.     _searchLineEdit->installEventFilter(this);  
  46.     //_searchLineEdit->setFocusPolicy(Qt::NoFocus);  
  47.      _btn = new QPushButton(this);  
  48.      _btn->resize(14,14);  
  49.      _btn->move(155,41);  
  50.      _btn->setStyleSheet("QPushButton{background-color:transparent;border:0px;}");  
  51.      _btn->setFocusPolicy(Qt::NoFocus);  
  52.      _btn->setVisible(false);  
  53.   
  54.      connect(_btn,SIGNAL(clicked()),this,SLOT(btnClicked()));  
  55.      _cluster = new QListWidget(this);  
  56.     _cluster->show();  
  57.     setListWidget();  
  58.     _cluster->setMouseTracking(true);  
  59.     _cluster->setFocusPolicy(Qt::NoFocus);  
  60.     //_cluster->installEventFilter(this);  
  61.     connect(_searchLineEdit,SIGNAL(textChanged(QString)),this,SLOT(textChange(QString)));  
  62.     connect(_page,SIGNAL(currentPageChanged(int)),this,SLOT(pageChange(int)));  
  63.     connect(_searchLineEdit,SIGNAL(pressed()),this,SLOT(editClicked()));  
  64.     connect(_cluster,SIGNAL(itemEntered(QListWidgetItem*)),this,SLOT(itemEnteredSlot(QListWidgetItem*)));  
  65.     connect(_cluster,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(itemClickedSlot(QListWidgetItem*)));  
  66.     connect(_cluster,SIGNAL(currentRowChanged(int)),this,SLOT(rowChanged(int)));  
  67.     //this->setFocus();  
  68.     //this->installEventFilter(this);  
  69. }  
  70.   
  71. void ClusterListWidget::setListWidget()  
  72. {  
  73.     _cluster->setStyleSheet("QListWidget{background-color:transparent;color:#19649F;border:0px solid gray;}"  
  74.                                           "QListWidget::item{background-color:transparent;border:0px solid gray;padding-left:28px;color:#b4e5fd}"  
  75.                                           //"QListWidget::item:!selected{border:0px solid gray;padding-left:28px;color:#b4e5fd}"  
  76.                                           //"QListWidget::item:selected:active{background-image:url(:/images/handleMenu_lixt_bg_selected.png);color:#b4e5fd;border-width:0;}"  
  77.                                           "QListWidget::item:selected{background-image:url(:/images/handleMenu_lixt_bg_selected.png);}"  
  78.                                           );  
  79.     _cluster->move(0,62);  
  80.     _cluster->resize(183,540);  
  81.     _cluster->clear();  
  82.   
  83.     pageChange(1);  
  84.   
  85. }  
  86.   
  87. void ClusterListWidget::setTitle(QString title)  
  88. {  
  89.     lb2->setText(title);  
  90. }  
  91.   
  92. void ClusterListWidget::setImgs(const QString &normal, const QString &abnormal, const QString &error)  
  93. {  
  94.     _normal = normal;  
  95.     _abnormal = abnormal;  
  96.     __error = error;  
  97. }  
  98.   
  99. void ClusterListWidget::setVmImgs(const QString &linux_normal,  
  100.                                   const QString &linux_abnormal,  
  101.                                   const QString &linux_error,  
  102.                                   const QString &linux_off,  
  103.                                   const QString &windows_normal,  
  104.                                   const QString &windows_abnormal,  
  105.                                   const QString &windows_error,  
  106.                                   const QString &windows_off)  
  107. {  
  108.     _linux_normal = linux_normal;  
  109.     _linux_abnormal = linux_abnormal;  
  110.     _linux_error = linux_error;  
  111.     _linux_off = linux_off;  
  112.     _windows_normal = windows_normal;  
  113.     _windows_abnormal = windows_abnormal;  
  114.     _windows_error = windows_error;  
  115.     _windows_off = windows_off;  
  116. }  
  117.   
  118.   
  119. void ClusterListWidget::editClicked()  
  120. {  
  121.     _searchLineEdit->setStyleSheet("background-image:url(:/images/handleMenu_searchInput_click.png);border:1px solid #536874;");  
  122.     _searchLineEdit->update();  
  123.     _btn->setVisible(true);  
  124. }  
  125.   
  126. void ClusterListWidget::btnClicked()  
  127. {  
  128.     _searchLineEdit->clear();  
  129.     _searchLineEdit->setStyleSheet("background-image:url(:/images/handleMenu_searchInput.png);border:1px solid #536874;");  
  130.     this->setFocus();  
  131. }  
  132.   
  133. void ClusterListWidget::itemEnteredSlot(QListWidgetItem *i)  
  134. {  
  135.     _n = _cluster->row(i);  
  136.     if(_n == _cluster->currentRow())  
  137.     {  
  138.         _enter = false;  
  139.     }else{  
  140.     _enter = true;}  
  141.     _x = 0;  
  142.     _y = _cluster->row(i)*33 +62;  
  143.     _enterPixmap = QPixmap(":/images/handleMenu_lixt_bg_hover.png");  
  144.   
  145.     _cluster->update();  
  146. }  
  147.   
  148. void ClusterListWidget::itemClickedSlot(QListWidgetItem *i)  
  149. {  
  150.     int row =_cluster->row(i);  
  151.     qDebug()<< (_currentNum -1)*16 + row;  
  152.     _enter = false;//选中跟悬停  只显示选中背景图片  
  153.     _cluster->update();  
  154.   
  155. }  
  156.   
  157. void ClusterListWidget::rowChanged(int row)  
  158. {  
  159.   
  160.     //qDebug()<< (_currentNum -1)*16 + row;  
  161.     _enter = false;//选中跟悬停  只显示选中背景图片  
  162.     _cluster->update();  
  163.     _n = row;  
  164. }  
  165.   
  166. //bool ClusterListWidget::eventFilter(QObject *obj, QEvent *e)  
  167. //{  
  168. //    if(obj == _cluster){  
  169. //        if(e->type() == QEvent::Leave)  
  170. //        {  
  171. //            _enter = false;  
  172. //            _cluster->update();  
  173. //            _n = _cluster->currentRow();  
  174. //            return true;  
  175. //        }  
  176. //    }  
  177. //   return false;  
  178. //    QWidget::eventFilter(obj,e);  
  179. //}  
  180.   
  181. void ClusterListWidget::textChange(QString text)  
  182. {  
  183.     _cluster->clear();  
  184.     _list.clear();  
  185.     _searchList.clear();  
  186.     for(int i = 0;i < NameList.count();i++){  
  187.         if(NameList.at(i).contains(text))  
  188.         {  
  189.             _searchList.append(NameList.at(i));  
  190.         }  
  191.     }  
  192.     _list = _searchList;  
  193.     _maxNum = (_list.count() - 1) / 16 +1;  
  194.     _currentNum = 1;  
  195.     _page->setTotalPage(_maxNum);  
  196.     pageChange(1);  
  197.     _cluster->update();  
  198.   
  199. }  
  200.   
  201. void ClusterListWidget::pageChange(int currentnumber)  
  202. {  
  203.   
  204.     _currentNum = currentnumber;  
  205.     _cluster->clear();  
  206.   
  207.     switch (_type) {  
  208.     case 0:  
  209.         setTitle(tr("all cluster"));  
  210.         setImgs(":/images/handleMenu_clusters.png",  
  211.                 ":/images/handleMenu_clusters_abnormal.png",":/images/handleMenu_clusters_error.png");  
  212.         if(_currentNum != _maxNum)  
  213.         {  
  214.             for(int i= (_currentNum -1) * 16;i <(_currentNum) * 16 ;i++ )  
  215.             {  
  216.                 QListWidgetItem *item = new QListWidgetItem;  
  217.                 item->setSizeHint(QSize(183,33));  
  218.                 item->setText(" "+_list.at(i));  
  219.                 item->setIcon(QIcon(_normal));  
  220.                 _cluster->addItem(item);  
  221.             }  
  222.         }else{  
  223.             for(int i = (_currentNum -1) * 16;i < _list.count();i++)  
  224.             {  
  225.                 QListWidgetItem *item = new QListWidgetItem;  
  226.                  item->setSizeHint(QSize(183,33));  
  227.                 item->setText(" "+_list.at(i));  
  228.                 item->setIcon(QIcon(__error));  
  229.                 _cluster->addItem(item);  
  230.             }  
  231.         }  
  232.         break;  
  233.     case 1:  
  234.   
  235.         setImgs(":/images/handleMenu_host.png",  
  236.                 ":/images/handleMenu_host_abnormal.png",":/images/handleMenu_host_error.png");  
  237.         if(_currentNum != _maxNum)  
  238.         {  
  239.             for(int i= (_currentNum -1) * 16;i <(_currentNum) * 16 ;i++ )  
  240.             {  
  241.                 QListWidgetItem *item = new QListWidgetItem;  
  242.                 item->setSizeHint(QSize(183,33));  
  243.                 item->setText(" "+_list.at(i));  
  244.                 item->setIcon(QIcon(_abnormal));  
  245.                 _cluster->addItem(item);  
  246.             }  
  247.         }else{  
  248.             for(int i = (_currentNum -1) * 16;i < _list.count();i++)  
  249.             {  
  250.                 QListWidgetItem *item = new QListWidgetItem;  
  251.                  item->setSizeHint(QSize(183,33));  
  252.                 item->setText(" "+_list.at(i));  
  253.                 item->setIcon(QIcon(__error));  
  254.                 _cluster->addItem(item);  
  255.             }  
  256.         }  
  257.         break;  
  258.     case 2:  
  259.         setVmImgs(":/images/handleMenu_vm.png",":/images/handleMenu_vm_linux_abnormal.png",":/images/handleMenu_vm_linux_error.png",  
  260.                   ":/images/handleMenu_vm_linux_shutdown.png",":/images/handleMenu_vm_windows.png",":/images/handleMenu_vm_windows_abnormal.png",  
  261.                   ":/images/handleMenu_vm_windows_error.png",":/images/handleMenu_vm_windows_shutdown.png");  
  262.         if(_currentNum != _maxNum)  
  263.         {  
  264.             for(int i= (_currentNum -1) * 16;i <(_currentNum) * 16 ;i++ )  
  265.             {  
  266.                 QListWidgetItem *item = new QListWidgetItem;  
  267.                 item->setSizeHint(QSize(183,33));  
  268.                 item->setText(" "+_list.at(i));  
  269.                 item->setIcon(QIcon(_linux_abnormal));  
  270.                 _cluster->addItem(item);  
  271.             }  
  272.         }else{  
  273.             for(int i = (_currentNum -1) * 16;i < _list.count();i++)  
  274.             {  
  275.                 QListWidgetItem *item = new QListWidgetItem;  
  276.                  item->setSizeHint(QSize(183,33));  
  277.                 item->setText(" "+_list.at(i));  
  278.                 item->setIcon(QIcon(_windows_error));  
  279.                 _cluster->addItem(item);  
  280.             }  
  281.         }  
  282.         break;  
  283.     default:  
  284.         break;  
  285.     }  
  286.   
  287.     _cluster->setCurrentRow(0);  
  288. }  
  289.   
  290. void ClusterListWidget::paintEvent(QPaintEvent *e)  
  291. {  
  292.     QPainter painter(this);  
  293.     painter.drawPixmap(0, 0, backGroundPix.width(), backGroundPix.height(), backGroundPix);  
  294.     if(_enter)  
  295.     {  
  296.         painter.drawPixmap(_x,_y,183,33,_enterPixmap);  
  297.     }  
  298.     //painter.setPen(QColor("#b4e5fd"));  
  299. }  
  300.   
  301. void ClusterListWidget::showEvent(QShowEvent *e)  
  302. {  
  303.     setListWidget();  
  304. }  
  305.   
  306. void ClusterListWidget::keyPressEvent(QKeyEvent *e)  
  307. {  
  308.     qDebug()<<"aaaaaaaaaaaaaaaaaaaaaa"<<_n;  
  309.     //n = _cluster->currentRow();  
  310.     if(e->key() == Qt::Key_Up)  
  311.     {  
  312.         if(_n > 0)  
  313.         {  
  314.             _n -1;  
  315.         }  
  316.         itemEnteredSlot(_cluster->item(_n));  
  317.         //_cluster->setCurrentRow(n);  
  318.         return;  
  319.     }  
  320.     if(e->key() == Qt::Key_Down)  
  321.     {  
  322.         if( _n < _cluster->count() -1){  
  323.             _n += 1;  
  324.         itemEnteredSlot(_cluster->item(_n));  
  325.         }  
  326.         //_cluster->setCurrentRow(n);  
  327.         return;  
  328.     }  
  329.     if(e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return)  
  330.     {  
  331.         qDebug()<<"777777777777777";  
  332.         _cluster->setCurrentRow(_n);  
  333.         return;  
  334.     }  
  335.     QWidget::keyPressEvent(e);  
  336. }  
  337. SearchLineEdit::SearchLineEdit(QWidget *parent)  
  338.    :QLineEdit(parent)  
  339. {  
  340.   
  341. }  
  342.   
  343. void SearchLineEdit::focusInEvent(QFocusEvent *e)  
  344. {  
  345.     emit pressed();  
  346.   
  347.     QLineEdit::focusInEvent(e);  
  348.   
  349. }  
  350.   
  351. void SearchLineEdit::focusOutEvent(QFocusEvent *e)  
  352. {  
  353.     QLineEdit::focusInEvent(e);  
  354. }  
(3)clusterlistitem.h
[html]  view plain  copy
  1. #ifndef CLUSTERLISTITEM_H  
  2. #define CLUSTERLISTITEM_H  
  3.   
  4. #include <QGraphicsItem>  
  5. #include "graphicsitembutton.h"  
  6. #include "clusterlistwidget.h"  
  7. #include <QPalette>  
  8. #include <QtGui>  
  9. class clusterListItem : public QObject, public QGraphicsItem  
  10. {  
  11.     Q_OBJECT  
  12. public:  
  13.     explicit clusterListItem(QGraphicsItem *parent = 0);  
  14.     QRectF boundingRect() const;  
  15. signals:  
  16.   
  17. protected:  
  18.     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);  
  19.   
  20.     bool eventFilter(QObject *obj, QEvent *event);  
  21.   
  22. private slots:  
  23.     void slotBtnClicked();  
  24.   
  25. private:  
  26.     int _width;  
  27.     int _height;  
  28.     GraphicsItemButton * _listBtn;  
  29.     ClusterListWidget * _clusterWidget;  
  30.     QGraphicsProxyWidget *_proxy;  
  31.     bool _show;  
  32.     bool _focus;  
  33. };  
  34.   
  35. #endif // CLUSTERLISTITEM_H  
(4) clusterlistitem.cpp
[html]  view plain  copy
  1. #include "clusterlistitem.h"  
  2.   
  3. clusterListItem::clusterListItem(QGraphicsItem *parent) :  
  4.     QGraphicsItem(parent)  
  5.   ,_show(false)  
  6. {  
  7.     setFiltersChildEvents(true);  
  8.     //setAcceptedMouseButtons(Qt::LeftButton);  
  9.     //setAcceptHoverEvents(true);  
  10.     _width = 22;  
  11.     _height =21;  
  12.     _listBtn = new GraphicsItemButton(":/images/handleMenu_icon.png",  
  13.                                       ":/images/handleMenu_icon_hover.png",  
  14.                                       ":/images/handleMenu_icon_hover.png",  
  15.                                       "",  
  16.                                        GraphicsItemButton::normal,this);  
  17.   
  18.     _listBtn->setSize(22,21);  
  19.     _listBtn->setPos(0,0);  
  20.     _listBtn->installEventFilter(this);  
  21.     _clusterWidget = new ClusterListWidget(cluster);  
  22.     _proxy = new QGraphicsProxyWidget(this);  
  23.     _proxy->setWidget(_clusterWidget);  
  24.     _proxy->setPos(0,21);  
  25.     _proxy->setVisible(_show);  
  26.     _clusterWidget->installEventFilter(this);  
  27.     //this->installEventFilter(this);  
  28.     connect(_listBtn,SIGNAL(clicked()),this,SLOT(slotBtnClicked()));  
  29. }  
  30.   
  31. QRectF clusterListItem::boundingRect() const  
  32. {  
  33.     return QRectF(0, 0, _width, _height);  
  34. }  
  35.   
  36. void clusterListItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)  
  37. {  
  38.       //painter->fillRect(QRectF(0, 0, _width, _height), QColor("#FF0000"));  
  39. }  
  40.   
  41. bool clusterListItem::eventFilter(QObject *obj, QEvent *event)  
  42. {  
  43.     if (event->type() == QEvent::FocusIn) {  
  44.         qDebug() << "QEvent::FocusIn---" << obj;  
  45.     }  
  46.     if (event->type() == QEvent::FocusOut) {  
  47.         qDebug() << "QEvent::FocusOut---" << obj;  
  48.     }  
  49.     if (event->type() == QEvent::MouseButtonPress) {  
  50.         qDebug() << obj;  
  51.         if (obj == _clusterWidget) {  
  52.             return true;  
  53.         }  
  54.         else {  
  55.             if (_show == true) {  
  56.                 _listBtn->setCurrent(false);  
  57.                 _show = false;  
  58.                 //_clusterWidget->hide();  
  59.                 QPropertyAnimation *animation = new QPropertyAnimation(_clusterWidget, "geometry");  
  60.                 animation->setDuration(300);  
  61.                 animation->setStartValue(QRect(0,21,183,602));  
  62.                 animation->setEndValue(QRect(0,21,183,0));  
  63.                 animation->start();  
  64.             }  
  65.         }  
  66.     }  
  67.      return QObject::eventFilter(obj, event);  
  68. }  
  69.   
  70. void clusterListItem::slotBtnClicked()  
  71. {  
  72.     this->setFocus();  
  73.     _listBtn->setCurrent(true);  
  74.     if(!_show){  
  75.         //_proxy->setVisible(!_show);  
  76.         _proxy->show();  
  77.         QPropertyAnimation *animation = new QPropertyAnimation(_clusterWidget, "geometry");  
  78.         animation->setDuration(300);  
  79.         animation->setStartValue(QRect(0,21,183,0));  
  80.         animation->setEndValue(QRect(0,21,183,602));  
  81.         animation->start();  
  82.         _show = true;  
  83.     }  
  84.     else{  
  85.   
  86.         QPropertyAnimation *animation = new QPropertyAnimation(_clusterWidget, "geometry");  
  87.         animation->setDuration(300);  
  88.         animation->setStartValue(QRect(0,21,183,602));  
  89.         animation->setEndValue(QRect(0,21,183,0));  
  90.         animation->start();  
  91.         _show = false;  
  92.     }  
  93. }  
(5)main.cpp
[html]  view plain  copy
  1. #include "clusterlistwidget.h"  
  2. #include <QApplication>  
  3. #include <QTextCodec>  
  4. #include <QtGui>  
  5. #include "clusterlistitem.h"  
  6. int main(int argc, char *argv[])  
  7. {  
  8.     QApplication a(argc, argv);  
  9.     QTextCodec::setCodecForTr(QTextCodec::codecForLocale());  
  10.   
  11.     QGraphicsView view;  
  12.     //view.resize(300,800);  
  13.   
  14.     QGraphicsScene scene;  
  15.   
  16.     scene.setSceneRect(-100, -300, 200, 600);  
  17.     view.setScene(&scene);  
  18.     view.setSceneRect(0, 0, 200, 600);  
  19.     //view.setCacheMode(QGraphicsView::CacheBackground);  
  20.     //view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);  
  21.     clusterListItem *item = new clusterListItem;  
  22.     scene.addItem(item);  
  23.     item->setPos(0, 0);  
  24.     view.resize(314, 814);  
  25.     view.show();  
  26.     view.installEventFilter(item);  
  27.     return a.exec();  
  28. }  

三、总结

(1)可以在eventFilter的QEvent::FocusIn和QEvent::FocusOut增加当鼠标移出菜单后自动回收的功能。其他的功能还需进一步完善。
(2)原文链接:http://blog.csdn.net/taiyang1987912/article/details/51002420