Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2631277
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2009-04-21 16:20:53

 
#include
class CheckView: public QListView
{
public:
 CheckView(QWidget* parent = 0): QListView(parent)
 {
 }
 bool eventFilter(QObject* object, QEvent* event)
 {
  if (event->type() == QEvent::MouseButtonRelease)
  {
   QMouseEvent* mouse = static_cast(event);
   QModelIndex index = indexAt(mouse->pos());
   if (index.isValid())
   {
    // check if the mouse was released over the checkbox
    QStyleOptionButton option;
    option.QStyleOption::operator=(viewOptions());
    option.rect = visualRect(index);
    QRect rect = style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &option);
    if (rect.contains(mouse->pos()))
    {
     // mouse was release over a checkbox
     // bypass combobox and deliver the event just for the listview
     QListView::mouseReleaseEvent(mouse);
     return true;
    }
   }
  }
  return false;
 }
};
class CheckModel: public QStandardItemModel
{
public:
 CheckModel(QObject* parent = 0): QStandardItemModel(parent)
 {
  // a must
  insertColumn(0);
 }
 Qt::ItemFlags flags(const QModelIndex& index) const
 {
  return QStandardItemModel::flags(index) | Qt::ItemIsUserCheckable;
 }
};
int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 QComboBox*  combo = new QComboBox;
 CheckModel* model = new CheckModel(combo);
 CheckView*  view  = new CheckView(combo);
 combo->setModel(model);
 combo->setView(view);
 // these 2 lines below are important and must be
 // applied AFTER QComboBox::setView() because
 // QComboBox installs it's own filter on the view
 view->installEventFilter(view);    // <--- !!!
 view->viewport()->installEventFilter(view); // <--- !!!
 for (int i = 0; i < 5; ++i)
 {
  combo->addItem(QString::number(i));
  // init check state for every added item!
  combo->setItemData(i, Qt::Unchecked, Qt::CheckStateRole);
 }
 combo->show();
 return a.exec();
}
阅读(1097) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~