我想为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事件过滤器