I'm using qt5.2.1 on RHEL6
Aside from using QTextBrowser
or rewriting a new widget that uses QLabel
instead of QListWidgetItem
like QListWidget
, how would I be able to make the text individually selectable with the mouse pointer?
For example in a text editor like vim you can drag-click the mouse button over some text and release the mouse, and then you can middle mouse click in a different editor or terminal and it pastes it. I know how to do that through QClipboard
but the problem is that the text isn't selectable in the first place. I still want the entire row to be selectable on a single click, which is why I'm using a QListWidget
, but if I hold and drag the mouse I'd like to be able to select individual text.
TL;DR:
A QLabel
has the function setTextInteractionFlags
where you can make it mouse selectable
, how can I do that for a QListWidgetItem
?
in QListWidget
can be used setEditTriggers(QtWidgets.QAbstractItemView.SelectedClicked)
and for all items setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsEnabled|QtCore.Qt.ItemIsEditable)
, then on first click the item is selected,
on second the hole text
and a part of text can be selected by mouse
Edit 24.03.2015
To prevent editing by user on this way subclass QstyledItemDelegate
(i did it in PyQt5)
class MyDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self):
QtWidgets.QStyledItemDelegate.__init__(self)
def setModelData(self,editor,model,index):
pass # no changes are written to model
and use this delegate for listwidget:
self.delegate = MyDelegate()
self.listWidget.setItemDelegate(self.delegate)
users can delete or change the items text, when editing is finished, the original text appears
Edit 25.03.15: add
def eventFilter(self,editor,event):
if event.type() == QtCore.QEvent.KeyPress and event.key() not in (QtCore.Qt.Key_Control, QtCore.Qt.Key_C):
return True
return QtWidgets.QStyledItemDelegate.eventFilter(self, editor, event)
to the delegate and users can't edit the text of the items, they only can copy from him