#include <QApplication>
#include <QListWidget>
#include <QListWidgetItem>
#include <QMouseEvent>
#include <QStyle>
#include <QStyleOptionViewItem>
#include <QStyleOptionButton>
{
public:
{
// contiguous selection mode
// add some dummy checkable example items
for (int i = 0; i < 10; ++i)
{
item->setCheckState(Qt::Unchecked);
addItem(item);
}
}
protected:
{
if (item)
{
// mouse pressed over a selected items checkbox,
// change the check states of all selected items
setSelectedCheckStates(item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked);
}
else
{
// otherwise let the base class handle mouse press
}
}
{
// do nothing if mouse released over a selected items checkbox
if (!selectedCheckStateItem(event->pos()))
{
// otherwise let the base class handle mouse release
}
}
private:
// returns the selected item whose checkbox lies under 'pos'
// returns 0 if not selected, no item at pos, or does not fit inside checkbox
{
if (item)
{
// with the help of styles, check if checkbox rect contains 'pos'
opt.rect = visualItemRect(item);
QRect r
= style
()->subElementRect
(QStyle::SE_ViewItemCheckIndicator,
&opt
);
// assure that the item is also selected
if (selectedItems().contains(item) && r.contains(pos))
{
return item;
}
}
return 0;
}
// sets the check state of all selected items to 'checkState'
void setSelectedCheckStates(Qt::CheckState checkState)
{
item->setCheckState(checkState);
}
};
int main(int argc, char *argv[])
{
ListWidget w;
w.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}
#include <QApplication>
#include <QListWidget>
#include <QListWidgetItem>
#include <QMouseEvent>
#include <QStyle>
#include <QStyleOptionViewItem>
#include <QStyleOptionButton>
class ListWidget : public QListWidget
{
public:
ListWidget(QWidget* parent = 0) : QListWidget(parent)
{
// contiguous selection mode
setSelectionMode(QAbstractItemView::ContiguousSelection);
// add some dummy checkable example items
for (int i = 0; i < 10; ++i)
{
QListWidgetItem* item = new QListWidgetItem(QString::number(i+i));
item->setCheckState(Qt::Unchecked);
addItem(item);
}
}
protected:
void mousePressEvent(QMouseEvent* event)
{
QListWidgetItem* item = selectedCheckStateItem(event->pos());
if (item)
{
// mouse pressed over a selected items checkbox,
// change the check states of all selected items
setSelectedCheckStates(item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked);
}
else
{
// otherwise let the base class handle mouse press
QListWidget::mousePressEvent(event);
}
}
void mouseReleaseEvent(QMouseEvent* event)
{
// do nothing if mouse released over a selected items checkbox
if (!selectedCheckStateItem(event->pos()))
{
// otherwise let the base class handle mouse release
QListWidget::mouseReleaseEvent(event);
}
}
private:
// returns the selected item whose checkbox lies under 'pos'
// returns 0 if not selected, no item at pos, or does not fit inside checkbox
QListWidgetItem* selectedCheckStateItem(const QPoint& pos)
{
QListWidgetItem* item = itemAt(pos);
if (item)
{
// with the help of styles, check if checkbox rect contains 'pos'
QStyleOptionButton opt;
opt.QStyleOption::operator=(viewOptions());
opt.rect = visualItemRect(item);
QRect r = style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt);
// assure that the item is also selected
if (selectedItems().contains(item) && r.contains(pos))
{
return item;
}
}
return 0;
}
// sets the check state of all selected items to 'checkState'
void setSelectedCheckStates(Qt::CheckState checkState)
{
foreach (QListWidgetItem* item, selectedItems())
item->setCheckState(checkState);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ListWidget w;
w.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks