2

I want to set cursor shape for QComboBox and his items. With setCursor affecting only LineEdit part of QComboBox, how do I access items view to change cursor shape?

QComboBox *combo = new QComboBox();
combo->addItem("One");
combo->addItem("Two");
combo->addItem("Three");
combo->setCursor(Qt::PointingHandCursor); // changes cursor only for LineEdit part, on popup cursor is still arrow
combo->view()->setCursor(Qt::PointingHandCursor); // does not affect popup view

we use Qt 5.5.1

Shf
  • 3,463
  • 2
  • 26
  • 42
  • Have you tried setting this on the view()->viewport(), which is the actual widget that the mouse is over? Perhaps the viewport overrides the cursor of the popup. – Johannes Schaub - litb Jun 13 '17 at 16:05
  • @JohannesSchaub-litb Great idea, maybe indeed view() is not widget, tried with `combo->view()->viewport()->setMouseTracking(true); combo->view()->viewport()->setCursor(Qt::PointingHandCursor);` with same result. It's so strange as i am able to access items via stylesheet like this: `QComboBox QAbstractItemView::item { color: red;}` and popup itself `QComboBox QAbstractItemView {}`. But so much trouble when attempting to change cursor shape – Shf Jun 13 '17 at 16:26
  • @JohannesSchaub-litb funny part is if i install eventFilter on `combo->view()->viewport()` i do get all mouveMove and mouseHover events, but it just can't install cursor for it `combo->view()->viewport()->setCursor(Qt::PointingHandCursor);`. – Shf Jun 13 '17 at 16:35

1 Answers1

3

This code works:

combo->installEventFilter(this);
//...

bool MainWin::eventFilter(QObject *obj, QEvent *ev)
{
    if( obj == combo 
        && (ev->type() == QEvent::Enter
        || ev->type() == QEvent::HoverMove) )
    {
        combo->setCursor(Qt::PointingHandCursor);
        combo->view()->setCursor(Qt::PointingHandCursor);

        return true;
    }
    return QMainWindow::eventFilter(obj, ev);
}

See Qt Event Filters

Vladimir Bershov
  • 2,701
  • 2
  • 21
  • 51
  • For my win 10 machine i've added one line before return `qDebug() << "event type: " << ev->type();` I get all kind of events in log, but when i expand items of combo box, events stop popping in log. For me cursor did not change for combobox items – Shf Jun 13 '17 at 15:52
  • Try `combo->setMouseTracking(true); combo->view()->setMouseTracking(true);` – Vladimir Bershov Jun 13 '17 at 16:29
  • I made it to respect mouse move events on popup via `combo->view()->viewport()->installEventFilter(this);` and changed if to `obj == mCategoryCBox->view()->viewport()`. Now i do get mouse move events when i move mouse and select different items in popup. But nothing works when i try to change cursor on view() , viewport() or combo itself. Fun thing is - i actually receive `event type: QEvent::Type(CursorChange)` in log, but still have my arrow cursor :D – Shf Jun 13 '17 at 16:39
  • You can also try `your_win->setCursor(),` `QApplication::setOverrideCursor()`... It also may be a bug, therefore you can try to change the Qt version to the last, *or to a version that has minor updates (5.6.2 for example)* – Vladimir Bershov Jun 13 '17 at 17:11