Results 1 to 9 of 9

Thread: Change cursor when pointer moving through QTableView's header

  1. #1
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Change cursor when pointer moving through QTableView's header

    Hi everyone,

    I'm using QTableView in a QWidget to show data from database. Users can click any cell in column 3 to play the music in that cell. Therefore I change the cursor to pointing hand when pointer entered the column and change it back to arrow when left.

    Here is some related code:
    Qt Code:
    1. m_view->setMouseTracking(true);
    2. connect(m_view, SIGNAL(clicked(QModelIndex)), this, SLOT(playAudio(QModelIndex)));
    3. connect(m_view, SIGNAL(entered(QModelIndex)), this, SLOT(changeCursor(QModelIndex)));
    4. connect(m_view, SIGNAL(viewportEntered()), this, SLOT(changeCursor()));
    5.  
    6. void Tab::changeCursor(QModelIndex index){
    7. if(index.column() == 3)
    8. setCursor(Qt::PointingHandCursor);
    9. else
    10. setCursor(Qt::ArrowCursor);
    11. }
    12. void Tab::changeCursor(){
    13. setCursor(Qt::ArrowCursor);
    14. }
    To copy to clipboard, switch view to plain text mode 

    Everything works well except when I move pointer from column 3 to its header and leave the QTableView, the viewportEntered signal is not emitted and the cursor is keeping pointing hand but not arrow.

    Could anyone help me with is issue? I'm using Qt 4.7.
    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Change cursor when pointer moving through QTableView's header

    Remove all your code and use this:
    Qt Code:
    1. m_view->horizontalHeader()->setCursor(Qt::PointingHandCursor);
    To copy to clipboard, switch view to plain text mode 

    Edit: Ah.. sorry, I didn't read your post carefully enough. Anyway, you can intercept QWidget::leaveEvent() to detect when the mouse pointer is leaving the widget. You can reset the cursor then.

    And by the way, use QApplication::setOverrideCursor() and QApplication::restoreOverrideCursor() instead of setCursor().

    Another approach would be to enable hover events for your view and intercept that. They you'd have total control over what happens where regarding the pointer position.
    Last edited by wysota; 17th July 2012 at 01:45.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change cursor when pointer moving through QTableView's header

    Thanks a lot for replying.

    The leaveEvent works well but after trying different signals and events, I found actually I can just set the cursor for QTableView instead of its parent.
    (i.e. use
    Qt Code:
    1. m_view->setCursor(Qt::ArrowCursor); //for viewportEntered signal
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. setCursor(Qt::ArrowCursor);
    To copy to clipboard, switch view to plain text mode 
    )
    The only problem now is when I move pointer from column 3 to horizontal header BUT NOT leave the QTableView, the pointer is still the pointing hand while it stay in the header area.
    I think this is because neither QTableView::entered(QModelIndex) singal nor QWidget::leaveEvent can detect the pointer moving from cells to headers?
    It should be much easier if I can get the column 3 as a separated object or a list of cell objects. Is their a way to get it in QTableView?

    And what's the reason to use QApplication::setOverrideCursor() and QApplication::restoreOverrideCursor() instead of setCursor()?
    Thanks again.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Change cursor when pointer moving through QTableView's header

    Quote Originally Posted by Seishin View Post
    If you use setCursor() then that's an absolute way of telling the desktop what cursor to show. If you use an override cursor, there is an internal stack kept that lets you revert to the last set cursor instead of having to set a particular shape hoping that nobody changed the cursor in the meantime.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change cursor when pointer moving through QTableView's header

    I see.
    Do you know is there a way to set events for just one column of cells instead of dealing with the whole tableView? The header is really annoying. Thanks.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Change cursor when pointer moving through QTableView's header

    No, the event system has no notion of columns.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change cursor when pointer moving through QTableView's header

    Sorry I think I used the wrong reply button.
    Just to repeat what I typed before,
    I see event cannot help me in this case, but is there a way to restore the cursor when the pointer moves to the header area?
    Thanks.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Change cursor when pointer moving through QTableView's header

    If the pointer moves on the header, the header will get an enterEvent. If you intercept such event, you can react on it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change cursor when pointer moving through QTableView's header

    I think enterEvent should work, but before that I've tried the signal entered like

    Qt Code:
    1. m_view->horizontalHeader()->setMouseTracking(true);
    2. connect(m_view->horizontalHeader(), SIGNAL(entered(QModelIndex)), this, SLOT(changeCursor(QModelIndex)));
    To copy to clipboard, switch view to plain text mode 

    And it does not work at all.
    I saw the link http://www.qtcentre.org/threads/4405...t-quot-signals
    which says it's a bug.

    Anyway thanks a lot for your help.

Similar Threads

  1. Change cursor inside QTableview
    By big4mil in forum Qt Programming
    Replies: 4
    Last Post: 15th November 2010, 11:04
  2. Cursor is not moving through QTreeWidgetItem list in QTreeWidget
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 0
    Last Post: 11th December 2009, 06:52
  3. Moving cursor in QPlainTextEdit
    By pgbackup in forum Qt Programming
    Replies: 2
    Last Post: 11th September 2009, 19:26
  4. [HELP] Display Mouse pointer/cursor ??
    By haconganh in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 21st May 2007, 09:09
  5. Moving Tooltip out of cursor-area
    By afo in forum KDE Forum
    Replies: 1
    Last Post: 7th January 2006, 15:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.