QTreeView on Mac OS X: how to move cursor without touching the selection?

  • Initially, QTreeView doesn't have a visible cursor on Mac OS and Linux. This is how I've added it. I've subclassed QTreeView and added this line in the constructor (only for Linux and Mac OS):

    @setStyle(new CFocusFrameStyle);@

    And then:

    @void CFocusFrameStyle::drawPrimitive(PrimitiveElement element, const QStyleOption * option, QPainter * painter, const QWidget * widget) const
    if (element == QStyle::PE_FrameFocusRect) {
    if (const QStyleOptionFocusRect *fropt = qstyleoption_cast<const QStyleOptionFocusRect >(option)) {
    QColor bg = fropt->backgroundColor;
    QPen oldPen = painter->pen();
    QPen newPen;
    if (bg.isValid()) {
    int h, s, v;
    bg.getHsv(&h, &s, &v);
    if (v >= 128)
    } else {
    QRect focusRect = option->rect /
    .adjusted(1, 1, -1, -1) */;
    painter->drawRect(focusRect.adjusted(0, 0, -1, -1)); //draw pen inclusive
    } else
    QApplication::style()->drawPrimitive(element, option, painter, widget);

    This draws the cursor and it looks exactly the same as stock cursor on Windows. Now, my problem is: how to move the cursor (using arrow keys, Home/End, PgUp / PgDwn) without touching selection? By default, those keys move the cursor and selection synchronously.

    On Windows the independent cursor movement is achieved by pressing cursor movement keys + Ctrl:

    @void CFileListView::keyPressEvent(QKeyEvent *event)
    if (event->key() == Qt::Key_Down || event->key() == Qt::Key_Up ||
    event->key() == Qt::Key_PageDown || event->key() == Qt::Key_PageUp ||
    event->key() == Qt::Key_Home || event->key() == Qt::Key_End)
    #ifndef APPLE
    if (!(event->modifiers() & Qt::ControlModifier))
    event->setModifiers(event->modifiers() | Qt::ControlModifier);


    The problem is, this doesn't work on Mac OS X, hence the #ifndef APPLE. I couldn't find any way to move cursor independently of the selection. Please advise.

    P. S. This is only a problem on OSX, on Linux Ctrl works fine, just as on Windows.

  • Lifetime Qt Champion


    On OS X the equivalent of the Ctrl functionality is done using the Cmd key

  • Weird, I've tried pressing Cmd + arrows on the keyboard and it didn't help. In fact, the cursor doesn't move at all.

  • Lifetime Qt Champion

    I've played a bit with Apple's Numbers application and it seems that it's an unknown concept...

  • But Qt draw a cursor. Is there no reasonably easy way to implement Windows cursor behavior on Mac?

  • Lifetime Qt Champion

    Qt does the right thing. It follows the guide lines.

    keyPressEvent comes to mind.

  • I am overriding keyPressEvent, but I'm not very enthusiastic about re-implementing from scratch reasonably complicated logic that QTreeView already does implement.
    I'm not saying Qt isn't doing the right thing, just wondering what would be the simplest way to do what I want.

  • Lifetime Qt Champion

    Except digging in Qt's code…

Log in to reply

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