PDA

View Full Version : QLineEdit persistent selection



elmo
2nd September 2009, 17:33
I would like to have persistent selection in QLineEdit. This means if I i.e. I will switch to other widget (edition ended, QLineEdit has lost focus) selection remains.
I tried to get this behaviour by acting on editingFinished() signal, which works, but only to some extent.
There is problem with hiding window. If I switch to another window and then I'll go back to window with my QLineEdit with selection then selection is lost.

bunjee
2nd September 2009, 21:06
Have you tried QWidget::setFocusProxy( QWidget * w ) ?

elmo
3rd September 2009, 17:17
Could you elaborate?
I can't think of any way this method could solve my problem.

wysota
4th September 2009, 10:10
Check in Qt's source code if anything happens on the focusInEvent for the line edit. Also mousePressEvent is worth checking out. Maybe you can prevent the action somehow by intercepting the event.

bunjee
4th September 2009, 10:37
My bad I misunderstood your problem.

What do you need that persistent selection for ?

What you're trying to achieve is not a standard "line edit" behavior.

You might consider subclassing QLineEdit and alter the focus events + paint event.

elmo
5th September 2009, 13:15
What do you need that persistent selection for ?
I'm making a form for QBE (http://pl.wikipedia.org/wiki/Query_By_Example) with text completion (as for now it will only be a prefix search).
I have a data model inheriting from QAbstractTableModel with (actually not yet, but will be) relational data in it. And I want to give the user possibility to write only the beginnings of name, surename, city, order number etc. and then to auto-complete the rest of fields.

As this is the first time I'm trying to write something in Qt after some looking around the classes I thought that QTableView will be something I should go for, so I tried QItemDelegate to create persistent aditors in a nice grid. And there problems began:
- as I mentioned in the first post I couldn't get to keep the selection as I wanted
- when I set the input of fields it generated filter on data which returned 0 rows and I couldn't traverse through all fields with tab anymore
- also there was problem as I couldn't leave the grid using tab (it cycles through all cells on the grid, but does not leave the table widget)

So after some thinking and looking at my code I thought that I wrote much more code (handling data change in model, creating and monitoring editors) to force QTableView to act as I want then use the features of QTableView.
So I switched to QTableWidget. I just set it to have one row and never change it (this solves problem with 0 rows in data model). Set widgets of all cells to class inheriting from QLineEdit and wrote code to take care of data changes in data model.
And that solved my problem with "dissappearing" selection. It seems like QTableView does something more than just call QItemDelegate::setEditorData() when data in the model is changed.
Also the problem with the tab traversal inside the table was solved with this change.
And only thing I see now that I have to deal with is making the tab traversal not getting stuck inside the QTableWidget.