Issue
How can i properly position the popup dialog under the ModelIndex clicked in a QTreeView. The X position, width and height of the popup dialog appear correct. However the Y position i can't seem to get correct. What am i doing wrong?
import os, sys, pprint
from Qt import QtGui, QtWidgets, QtCore
class Window(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.resize(800, 400)
self.uiSearch = QtWidgets.QLineEdit()
self.uiItems = QtWidgets.QTreeView()
self.uiItems.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.uiItems.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.uiItems.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
self.uiItems.setModel(QtGui.QStandardItemModel())
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(self.uiSearch)
self.layout.addWidget(self.uiItems)
self.setLayout(self.layout)
for i in range(10):
col1 = QtGui.QStandardItem('Item' + str(i))
col2 = QtGui.QStandardItem('Item' + str(i))
col3 = QtGui.QStandardItem('Item' + str(i))
self.uiItems.model().appendRow([col1,col2,col3])
self.uiItems.clicked.connect(self.clickedItem)
def clickedItem(self, index):
print index
if index.isValid():
rect = self.uiItems.visualRect(index)
pos = self.uiItems.mapToGlobal(rect.bottomLeft())
# create editor
dlg = QtWidgets.QDialog(self)
dlg.setWindowFlags(QtCore.Qt.Popup)
dlg.resize(rect.width(), 100)
dlg.move(pos)
dlg.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = Window()
ex.show()
app.exec_()
Solution
visualRect()
is respect to the viewport()
, not the QTreeView (the QTreeView is headers + viewport):
def clickedItem(self, index):
if index.isValid():
rect = self.uiItems.visualRect(index)
pos = self.uiItems.viewport().mapToGlobal(rect.bottomLeft())
# create editor
dlg = QtWidgets.QDialog(self)
dlg.setWindowFlags(QtCore.Qt.Popup)
dlg.resize(rect.width(), 100)
dlg.move(pos)
dlg.show()
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.