Click on drop-down list of QComboBox



  • Hello ! :)

    I currently have a QComboBox in a QGraphicsScene and I need it to detect clicks. To see if there is a widget in the clicked position, I use :

    void BlockScene::mousePressEvent(QMouseEvent *event)
    {
        if (itemAt(event->pos()) != m_widgetItem)
        {
            // ...
        }
    }
    

    This works well for different widgets except for combo boxes where it only takes into account the original widget and not the drop-down list that appears after a first click.

    To know if it came from the scene or not, I tested also by redefining mousePressEvent of the class QComboBox and same problem: it is called only when clicking on the initial widget. :(

    Is there a way to get the drop-down list ? To detect a click on it ? Ideas ?

    Thanks ! ;)


  • Moderators

    @Maluna34
    install an event-filter on the combo box's view and inspect the mouse events.



  • @raven-worx
    Do you mean comboBox->view()->installEventFilter(this); or comboBox->installEventFilter(this); ?

    It does not work. :(


  • Moderators

    @Maluna34 said in Click on drop-down list of QComboBox:

    It does not work. :(

    my glassball is broken, please help me out here.

    Just to make sure: do you know how event-filters work? I guess you didn't overload eventFilter() no?



  • @raven-worx
    I did don't worry ! ;) It's just that I'm at work. ^^

    So I tried this :

        virtual bool eventFilter(QObject *watched, QEvent *event) override
        {
            if (watched == m_box)
            {
                if (event->type() == QEvent::MouseButtonPress)
                {
                    qDebug() << "event";
    
                    return QMainWindow::eventFilter(watched, event);
                }
            }
            else
                return QMainWindow::eventFilter(watched, event);
        }
    

    With m_box->installEventFilter(this); this is the same as with mousePressEvent, the message only appears when the initial widget is clicked.
    And with m_box->view()->installEventFilter(this); nothing is happening.


  • Moderators

    @Maluna34
    install it on the view: comboBox->view()->installEventFilter(this);
    Then you of course in the eventFilter() implementation you also need to check for the view:

    virtual bool eventFilter(QObject *watched, QEvent *event) override
        {
            if (watched == m_box->view())
            {
                if (event->type() == QEvent::MouseButtonPress)
                {
                    qDebug() << "event";
                }
            }
    
            return QMainWindow::eventFilter(watched, event);
        }
    


  • @raven-worx
    Thanks, I have events on the m_box->view() widget but no click. Here are the kind of events I have on the view :

    QEvent::Type(Show)
    QEvent::Type(FocusIn)
    QEvent::Type(UpdateLater)
    QEvent::Type(Enter)
    QEvent::Type(FocusAboutToChange)
    QEvent::Type(InputMethodQuery)
    QEvent::Type(ToolTip)
    QEvent::Type(Leave)
    QEvent::Type(Enter)
    QEvent::Type(FocusAboutToChange)
    QEvent::Type(FocusOut)
    QEvent::Type(InputMethodQuery)
    QEvent::Type(Hide)
    QEvent::Type(Leave)
    QEvent::Type(Timer)
    

  • Moderators

    @Maluna34
    and when you do the same on view()->viewport()?



  • @raven-worx
    It works !!! Thank you !!!

    Now I just have to see if I can check it into the scene (maybe create a widgetitem for the viewport, I'll see.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.