Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Handling QListWidget clicking and scrolling



  • I have a QListWidget with some items (QListWidgetItem's). Of course, the items have their own controls like buttons, lineedits and so on...

    For my QListWidget I set in constructor:

    this->setAttribute(Qt::WA_AcceptTouchEvents, true);
    this->grabGesture(Qt::TapGesture, Qt::IgnoredGesturesPropagateToParent | Qt::ReceivePartialGestures);
    

    Essentially the problem is my event handling:

    bool ListControl::event(QEvent* e)
    {
        auto event = e->type();
        if (event == QEvent::TouchBegin)
        {
            auto touch = dynamic_cast<QTouchEvent*>(e);
            if (touch)
            {
                return this->HandleTouch(touch, event);
            }
        }
    
        ...some code comes here...
    
        return QListWidget::event(e);
    }
    

    When I return on QEvent::TouchBegin the HandleTouch-Method, then I can move the list of elements (QListWidgetItems). With "move" I mean scrolling the list, with my finger on the mobile phone-touch screen. But then I can't get focus on any control of any QListWidgetItem.

    When I return on QEvent::TouchBegin QListWidget::event(e), then I get focus on the controls, but I can no more scroll the list.

    I tried a lot, but nothing helped. How could I solve this problem, to move/scroll the list with my finger, but also can get focus to the controls, on taping on it.

    Supplement: I measure the tap-duration. If it was less than 150ms, then I get the QWidget that was clicked. Only I do not manage to send the widget a message, so that it reacts.



  • Ok, I've got a solution! Quick and dirty explained... the trick is in the touch handler (QEvent::TouchBegin), you must set the focus, to the taped widget. If the widget is for example a QPushButton, you must return false, otherwise true. These solution works for me!

    const auto posf = event->touchPoints().first().pos();
    QPoint position(posf.x(), posf.y());
    
    auto widget = this->childAt(position);
    if (widget)
    {
        widget->setFocus();
        widget->activateWindow();
        widget->raise();
    }
    
    auto button = dynamic_cast<QPushButton*>(widget);
    result = (button == nullptr);
    

    Maybe I can help someone


Log in to reply