Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView on Mac OS X: how to move cursor without touching the selection?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by
    #1

    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)
    newPen.setColor(Qt::black);
    else
    newPen.setColor(Qt::white);
    } else {
    newPen.setColor(option->palette.foreground().color());
    }
    newPen.setWidth(0);
    newPen.setStyle(Qt::DotLine);
    painter->setPen(newPen);
    QRect focusRect = option->rect /
    .adjusted(1, 1, -1, -1) */;
    painter->drawRect(focusRect.adjusted(0, 0, -1, -1)); //draw pen inclusive
    painter->setPen(oldPen);
    }
    } 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);
    #endif
    }

    QTreeView::keyPressEvent(event);
    }@

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Violet Giraffe
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              keyPressEvent comes to mind.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • V Offline
                V Offline
                Violet Giraffe
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Except digging in Qt's code…

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved