Copy right is added in the tableview, want to locate alternative line and column number, get is always the thing [& lt; PyQt5. QtCore. QModelIndex object at 0 x00000000043b3ba0 & gt;] . What problem is. Please see
The source code is as follows:
# coding=utf-8
The import sys
The from PyQt5 import QtCore
The from PyQt5. QtCore import *
The from PyQt5. QtWidgets import *
The from PyQt5. QtGui import *
Class Table (QWidget) :
Def __init__ (self, parent=None) :
Super (Table, self) __init__ (parent)
# set the title and the initial size
Self. SetWindowTitle (' checkbox QTableView form case)
The self. The resize (500300)
The self. The tableView=QTableView ()
The self. The model=QStandardItemModel (self. TableView)
# set data hierarchy, four row 4 column
The self. The model=QStandardItemModel (4, 4)
T=QCheckBox (self)
# set the horizontal four heads label text content
Self. Model. SetHorizontalHeaderLabels ([' state ', 'name', 'id', 'address'])
For a row in the range (4) :
For the column in the range (4) :
Item_checked=QStandardItem ()
Item_checked. SetCheckState (Qt. Checked)
Item_checked. SetCheckable (True)
Self. Model. SetItem (column, 0, item_checked)
The item=QStandardItem (' s row, column % s' % (row, column))
# set each position of the text value
Self. Model. SetItem (row, column, item)
The self. The tableView. SetModel (self) model)
# set layout
Layout=QVBoxLayout ()
Layout. AddWidget (self. TableView)
Self. SetLayout (layout)
# if in QTableView use right-click menu, to enable the attribute
The self. The tableView. SetContextMenuPolicy (QtCore. Qt. CustomContextMenu)
# to create QMenu signal events
The self. The tableView. CustomContextMenuRequested. Connect (self. ShowMenu)
The self. The contextMenu=QMenu (self)
Self. CP=self. ContextMenu. AddAction (' copy ')
Self. CP. Triggered. Connect (self. Selected_tb_text)
Def selected_tb_text (self) :
The indexes=self. TableView. SelectedIndexes ()
Print (indexes)
Def showMenu (self, pos) :
# pos mouse position
Before # menu display, to move it to the position of the mouse to click
Self. ContextMenu. Exec_ (# QCursor. Pos ()) in mouse position according to
If __name__=="__main__ ':
App=QApplication (sys. Argv)
Table=table ()
Table. The show ()
Sys. Exit (app. Exec_ ())