python tableview双击排序_如何使QTableView仅在双击时才能进入编辑模式

Setting a Qt.ItemIsEnabled flag makes the QTableView items editable.

To enter the item's editing mode the user can simply double-click it. Another way to edit the item is to select it and press a keyboard key.

How to disable this second way of entering the item's editing mode?

Here is the image showing the QTableView with the item selected:

As soon as the user presses the keyboard key the selected item is already in the edit mode:

This default QTableView behavior makes it impossible to define the functions shortcuts since instead of triggering the linked-to-shortcut function the QListView's item enters the editing mode.... How to make QTableView to enter the editing mode only on the double-click?

from PyQt4.QtCore import *

from PyQt4.QtGui import *

import sys

class Model(QAbstractTableModel):

def __init__(self, parent=None, *args):

QAbstractTableModel.__init__(self, parent, *args)

self.items = ['Item_A_001','Item_A_002','Item_B_001','Item_B_002']

def rowCount(self, parent=QModelIndex()):

return len(self.items)

def columnCount(self, parent=QModelIndex()):

return 1

def data(self, index, role):

if not index.isValid(): return QVariant()

elif role != Qt.DisplayRole:

return QVariant()

row=index.row()

if row

return QVariant(self.items[row])

else:

return QVariant()

def flags(self, index):

return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable

class MyWindow(QWidget):

def __init__(self, *args):

QWidget.__init__(self, *args)

tableModel=Model(self)

self.view=QTableView(self)

self.view.setModel(tableModel)

self.view.horizontalHeader().setResizeMode(QHeaderView.Stretch)

layout = QVBoxLayout(self)

layout.addWidget(self.view)

self.setLayout(layout)

if __name__ == "__main__":

app = QApplication(sys.argv)

w = MyWindow()

w.show()

sys.exit(app.exec_())

解决方案

you need to reimplement the even handler keyPressEvent on your QTableView. To do so, you can create a custom QTableView class and reimplement the event handler inside it.

from PyQt4.QtCore import *

from PyQt4.QtGui import *

import sys

#Your new customized QTableView

class CustomQTableView(QTableView):

def __init__(self, *args, **kwargs):

QTableView.__init__(self, *args, **kwargs) #Use QTableView constructor

def keyPressEvent(self, event): #Reimplement the event here, in your case, do nothing

return

class Model(QAbstractTableModel):

def __init__(self, parent=None, *args):

QAbstractTableModel.__init__(self, parent, *args)

self.items = ['Item_A_001','Item_A_002','Item_B_001','Item_B_002']

def rowCount(self, parent=QModelIndex()):

return len(self.items)

def columnCount(self, parent=QModelIndex()):

return 1

def data(self, index, role):

if not index.isValid(): return QVariant()

elif role != Qt.DisplayRole:

return QVariant()

row=index.row()

if row

return QVariant(self.items[row])

else:

return QVariant()

def flags(self, index):

return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable

class MyWindow(QWidget):

def __init__(self, *args):

QWidget.__init__(self, *args)

print "c"

tableModel=Model(self)

self.view=CustomQTableView(self) #Call your custom QTableView here

self.view.setModel(tableModel)

self.view.horizontalHeader().setResizeMode(QHeaderView.Stretch)

layout = QVBoxLayout(self)

layout.addWidget(self.view)

self.setLayout(layout)

if __name__ == "__main__":

app = QApplication(sys.argv)

w = MyWindow()

w.show()

sys.exit(app.exec_())

This way, the keyPressEvent is overwritten and nothing happens, but your double click event remains the same.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值