QComboBox with flags

I am creating a QComboBox with checkboxes. How can I prevent the collapse of the view when I click? I want to be able to set checkboxes, but every time I click on an item, the dropdown from the QComboBox is reset.

Note: I am currently debugging Qt sources and looking for a workaround ...

+4
source share
1 answer

First of all, you need to set the event filter in the list box, i.e.:

combobox->view()->viewport()->installEventFilter(someobj);

how do you need to filter out all mouse release events that occur in the list view to prevent it from closing when you click on it:

bool SomeObject::eventFilter(QObject *obj, QEvent *event)
{
     if (event->type() == QEvent::MouseButtonRelease) {
         int index = view()->currentIndex().row();

         if (itemData(index, Qt::CheckStateRole) == Qt::Checked) {
            setItemData(index, Qt::Unchecked, Qt::CheckStateRole);
         } else {
            setItemData(index, Qt::Checked, Qt::CheckStateRole);
         }

         [..]

         return true;
     } else {
         // Propagate to the parent class.
         return QObject::eventFilter(obj, event);
     }
}
+5
source

Source: https://habr.com/ru/post/1534658/


All Articles