Results 1 to 3 of 3

Thread: currentIndex and rowAt, and columnAt disagree

  1. #1
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Default currentIndex and rowAt, and columnAt disagree

    So I have a QTableWidget which I want to have a context menu on. The context menu actions slots need to work differently depending on where I click the context menu at. So initially I was using the currentIndex method to get the appropriately selected cell. However I want to react differently if the context menu is brought up in the empty space around my cells. So then I moved to using rowAt and columnAt along with the QCursor.pos() that the context menu appeared at. The problem is the values from these two methods disagree. It seems like somehow the rowAt and columnAt are using the bottom right hand side of the cursor to map there position instead of the top left. The code below demonstrates my problem. If you right click and select current position from the menu it should print out two indices one from each method. What am I doing wrong?


    Qt Code:
    1. from PyQt4 import QtGui, QtCore
    2. import sys
    3.  
    4.  
    5. class Test(QtGui.QTableWidget):
    6. def __init__(self):
    7. super(Test, self).__init__(5,5)
    8. self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
    9. self.customContextMenuRequested.connect(self.ctxMenu)
    10.  
    11. def ctxMenu(self):
    12. menu = QtGui.QMenu(self)
    13. a = QtGui.QAction("Current Cell", self)
    14. menu.addAction(a)
    15. a.triggered.connect(self.contextCurPos)
    16. self.cursor_zero = QtGui.QCursor.pos()
    17. menu.exec_(QtGui.QCursor.pos())
    18.  
    19. def contextCurPos(self):
    20. pos = self.mapFromGlobal(self.cursor_zero)
    21. col = self.columnAt(pos.x())
    22. row = self.rowAt(pos.y())
    23. print('Map From Cursor %d, %d' % (row, col))
    24. model_idx = self.model().createIndex(row, col)
    25.  
    26. model_idx = self.currentIndex()
    27. print('From Current Index %d, %d' % (model_idx.row(), model_idx.column()))
    28. print('\n')
    29.  
    30. app = QtGui.QApplication(sys.argv)
    31. x = Test()
    32. x.show()
    33. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  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: currentIndex and rowAt, and columnAt disagree

    Don't use QCursor::pos(). Read the docs on using context menus.
    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. The following user says thank you to wysota for this useful post:

    mechsin (21st June 2012)

  4. #3
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: currentIndex and rowAt, and columnAt disagree

    Ok thanks I took a closer look and I don't know how I managed to miss that the QPoint was being passed as an argument. Anyway just in case anyone else gets hung up on this here is and example of some code that works.

    Qt Code:
    1. from PyQt4 import QtGui, QtCore
    2. import sys
    3.  
    4.  
    5. class Test(QtGui.QTableWidget):
    6. def __init__(self):
    7. super(Test, self).__init__(5,5)
    8. # If you comment out these lines the contextMenuEvent slot will be
    9. # called when the context menu is suppose to appear. If you leave them
    10. # active the ctxMenu def is called when the context Menu is suppose
    11. # to appear
    12. self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
    13. self.customContextMenuRequested.connect(self.ctxMenu)
    14.  
    15. def contextMenuEvent(self, event):
    16. menu = QtGui.QMenu(self)
    17. a = QtGui.QAction("Current Cell", self)
    18. menu.addAction(a)
    19. a.triggered.connect(self.contextCurPosEvent)
    20. self.cursor_zero = event.pos()
    21. menu.exec_(event.globalPos())
    22.  
    23. def ctxMenu(self, position):
    24. menu = QtGui.QMenu(self)
    25. a = QtGui.QAction("Current Cell", self)
    26. menu.addAction(a)
    27. a.triggered.connect(self.contextCurPos)
    28. self.cursor_zero = position
    29. # Because the QTable Widget is a scroll area the position passed to
    30. # this def is actually with respect to the viewport of the widget
    31. # therefore you need to call the mapToGlobal from the viewport and not
    32. # the TableWidget
    33. menu.exec_(self.viewport().mapToGlobal(position))
    34.  
    35. def contextCurPosEvent(self):
    36. pos = self.cursor_zero
    37. col = self.columnAt(pos.x())
    38. row = self.rowAt(pos.y())
    39. print('Map From Cursor %d, %d' % (row, col))
    40. model_idx = self.model().createIndex(row, col)
    41.  
    42. model_idx = self.currentIndex()
    43. print('From Current Index %d, %d' % (model_idx.row(), model_idx.column()))
    44. print('\n')
    45.  
    46. def contextCurPos(self):
    47. pos = self.cursor_zero
    48. col = self.columnAt(pos.x())
    49. row = self.rowAt(pos.y())
    50. print('Map From Cursor %d, %d' % (row, col))
    51. model_idx = self.model().createIndex(row, col)
    52.  
    53. model_idx = self.currentIndex()
    54. print('From Current Index %d, %d' % (model_idx.row(), model_idx.column()))
    55. print('\n')
    56.  
    57. app = QtGui.QApplication(sys.argv)
    58. x = Test()
    59. x.show()
    60. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 18th October 2010, 14:33
  2. Replies: 3
    Last Post: 26th February 2010, 22:37
  3. Replies: 2
    Last Post: 26th January 2008, 10:20
  4. Replies: 1
    Last Post: 16th November 2007, 12:06
  5. currentIndex().internalPointer() problem
    By xgoan in forum Qt Programming
    Replies: 2
    Last Post: 14th December 2006, 09:55

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.