1 Attachment(s)
QComboBox with a clickable button while popup is active
I have created the ComboBox widget like this:
Attachment 13530
In addition to default QComboBox, it contains a custom clickable widget to the left of popup button. I pushed this widget as a child of QComboBox, but there is still one problem I cannot solve. I want this widget to be clickable while the popup is open, but the popup is closed when I press to thewidget's arrows, and the widget itself doesn't receive any mouse events. Is there any way to make this widget available for mouse events while the popup is active?
Here is the current combobox implementation:
CarMarksComboBox.h (the arrows widget is CarModelsSwitcher)
Code:
#pragma once
#include "VehiclesData.h"
#include "widgets.h"
{
Q_OBJECT
public:
CarMarksComboBox
(QWidget* parent
= nullptr
);
void init(const VehicleNamesMap& vehicleNamesMap);
void showPopup() override;
void hidePopup() override;
protected:
private:
void updateArrowsWidgetPosition();
private:
CarModelsSwitcher* m_carModelsSwitcher = nullptr;
};
CarMarksComboBox.cpp
Code:
#include "CarMarksComboBox.h"
#include "CarMarksModel.h"
CarMarksComboBox
::CarMarksComboBox(QWidget* parent
){
}
void CarMarksComboBox::init(const VehicleNamesMap& vehicleNamesMap)
{
CarMarksModel* carMarksModel = new CarMarksModel(vehicleNamesMap);
carMarksModel->setSelectionFacility(false);
carMarksView->header()->setVisible(false);
carMarksView->setMinimumHeight(300);
setView(carMarksView);
setModel(carMarksModel);
m_carModelsSwitcher = new CarModelsSwitcher(this);
m_carModelsSwitcher->installEventFilter(this);
m_carModelsSwitcher->setVisible(false);
}
{
if (obj
== m_carModelsSwitcher
&& ev
->type
() == QEvent::Resize) updateArrowsWidgetPosition();
}
void CarMarksComboBox::showPopup()
{
const CarMarksModel* carMarksModel = static_cast<const CarMarksModel*>(model());
if (carMarksModel)
{
const int checkedModelsCount = carMarksModel->getCheckedModelsCount();
if (checkedModelsCount > 0)
{
m_carModelsSwitcher
->setText
(QString::number(checkedModelsCount
));
m_carModelsSwitcher->setVisible(true);
}
}
}
void CarMarksComboBox::hidePopup()
{
m_carModelsSwitcher->setVisible(false);
}
{
updateArrowsWidgetPosition();
}
void CarMarksComboBox::updateArrowsWidgetPosition()
{
m_carModelsSwitcher->move(width() - m_carModelsSwitcher->width() - 28, (height() - m_carModelsSwitcher->height()) / 2);
}