PDA

View Full Version : How to get cusor position and selection in QListWidget present editor



phill
22nd February 2012, 10:32
Hi, im relatively new to Qt, and I am using pyqt.

What I want to be able to do is to allow the user to enter data in to a QListWidget by typing. The user will type a sentence, and a list item will be created for each word.

What I have got so far, is a QListWidget set to display vertically, the user clicks on to the first and only QListWidgetItem which has "type here" in it. Clicking and typing opens the persistent editor. I use onKeyRelease to detect if the user has pressed space, and create a new QListWidgetItem. I then set this item as the selected item, so when they continue to type the text is now being entered in to the new QListWidgetItem.

What I want to do, and am having problems figuring out, is the following:
When the user presses delete, if the cursor is at the end of the persistent editor, and no text is selected, then I want to open the persistent editor on the next QListWidgetItem.
Similarly, if the user presses backspace, and the cursor is at the beginning of the persistent editor, and no text is selected, then the previous item is selected, the persistent editor opened, and the cursor moved to the end.

Is this do-able?
Any help, not just in pyqt, very much appreciated!

Phill

mentalmushroom
22nd February 2012, 14:35
You can try to set item delegate. Subclass QItemDelegate or QStyledItemDelegate and override createEditor, so you can use your own editor with all the necessary functionality.

phill
22nd February 2012, 17:19
Thats enough to get me Googleing so if I do something like: http://python.6.n6.nabble.com/QItemDelegate-createEditor-help-with-focusOut-on-custom-widget-td1798113.html then I could use QLineEdit instead of the standard editor, and then I would get access to app its methods?

How would I reference it? Using something like QListWidget.QListWidgetItem.QLineEdit ?

I also read somewhere this could use quite a bit of memory if I have 10 or more QListWidgetItem's

Phill

mentalmushroom
22nd February 2012, 18:30
Take a look at stardelegate sample. createEditor method returns a pointer to the editor widget. When you override it, you can use any widget you like, e.g. QLineEdit. Editor instance is created in createEditor method, there you connect editor's editingFinished signal to some slot in your delegate where you process it. Most likely you will need to override setEditorData and setModelData also.