更改 QComboBox 项的光标形状

Changing cursor shape for QComboBox items

本文关键字:光标 QComboBox 更改      更新时间:2023-10-16

我想为QComboBox和他的项目设置光标形状。由于setCursor仅影响LineEdit QComboBox的一部分,如何访问项目视图以更改光标形状?

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

我们使用Qt 5.5.1

这段代码有效:

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);
}

查看Qt事件过滤器