Solved Customizing QComboBox
-
Hello, i'm trying to reimplement the behaviour of the QCombobox. I just want the popup list of the combobox be closable only by clicking on the QCombobox's button. This is done and works fine. Now the popup won't close until u click on the corresponding button.
void CustomCbx::showPopup(){ if(m_view->model()==NULL) return; if(m_view->model()->rowCount()==0) return; m_view->show(); resize(width(),maxHeight); expanded=true; }
But, the expanding list now won't be shown if you add this CustomCbx to layout.Any suggestions,guys? Somehow the QCombobox able to show it's items even it's within layout though. Thanks in advance!
P.S. i'll go check the qt' sources, hope find something there.
-
@Ivan-Netskin
i am not quite sure what yourshowPopup()
implementation is supposted to do.
Is it just about resizing? Then it's enough to do this:void CustomCbx::showPopup() { this->view()->window()->setFixedSize( width(), maxHeight ); QComboBox::showPopup(); expanded=true; }
The important part is to show the window the view is in. Means the view is placed in a popup container widget. Thus showing the view in a hidden window wont change anything.
-
if you call QComboBox::showPopup() then the popup list will automatically will be closed if you click outside the combobox. This is why i don't call the CustomCbx::showPopup().
-
@Ivan-Netskin
even if i strongly disagree to override such a platform specific behavior, try this:void CustomCbx::showPopup() { this->view()->window()->setFixedSize( width(), maxHeight ); this->view()->window()->setWindowFlags( Qt::Tool | Qt::FramelessWindowHint | Qt::CustomizeWindowHint ); // this can even be called only once in the constructor i guess QComboBox::showPopup(); expanded=true; }
-
@raven-worx
Thanks, yes this helps, but side effect is that expand/collapse button stops working. i'll go to browse the qt's sources to find out the reason. -
@Ivan-Netskin
Hi
bool QComboBoxPrivateContainer::eventFilter
is stealing the mouseReleased from you.
( i think :) -
@mrjj
yep , probably these lines :case QEvent::MouseButtonRelease: { bool ignoreEvent = maybeIgnoreMouseButtonRelease && popupTimer.elapsed() < QApplication::doubleClickInterval(); QMouseEvent *m = static_cast<QMouseEvent *>(e); if (isVisible() && view->rect().contains(m->pos()) && view->currentIndex().isValid() && !blockMouseReleaseTimer.isActive() && !ignoreEvent && (view->currentIndex().flags() & Qt::ItemIsEnabled) && (view->currentIndex().flags() & Qt::ItemIsSelectable)) { combo->hidePopup(); emit itemSelected(view->currentIndex()); return true; } break; }
-
yep i think so.
But it seems you can get MouseDown so maybe u can maybe cheat and hax it that way
and make it close. -
customizing qcombobox is a bit pain. Changing little option may result in
an unexpected behaviour of the enitre combobox. Is there a way to get expandable list, even if the list inside layout? -
@Ivan-Netskin
Well you are fiddling with the core logic. That often leads to side effects.Qt is using private classes to increase compatibility but it also often kills
true reuse as only the virtual function the developers could imagine are
there to override. But it's worth it :)Also note, you can chain event filters so you might be able that way.
However, if you only need combobox then QListWidget and QPushbutton could
be use to make fake one. If you need the edit part it becomes more complicated.
set Min/max and fixed size are still allowed in layouts. -
thanks for reply, the problem is that if i change the widget size,which lies in layout - it will result in growing size of the whole application. This is undesirable behaviour in my case. Also just creating a listview widget without parent and moving it under the QCombobox looks like unclean solution a bit.
-
@Ivan-Netskin
ok. it must be floating then :)
Before giving up on your custom combobox I would have a look at the eventfilter docs.
You might be able to put yourself in front and control it that way. -
found something appropriate - http://libqxt.bitbucket.org/doc/tip/qxtcheckcombobox.html
-
@Ivan-Netskin
But will it stay open ? -
From discription it's allowing to select multiple items - that's good enough