Use of same widget for display and edit in model-view
The Qt View/Model uses different principle to display and edit (DisplayRole and EditRole) items.
The Delegate classes are easy in providing Editing widgets. But for display it provide "paint()" method to be implemented in subclass, which I feel a real pain to implement.
How would one achieve both Display and Edit by same kind of Widget in a nicer way.
For example, how to make QTableView to DISPLAY a QString using QLineEdit and also EDIT it by using same kind of Widget (QLineEdit).
Similarly, how to make QTableView to DISPLAY a bool using QCheckBox and also EDIT it by using same kind of Widget (QCheckBox).
Thanks in advance.
Re: Use of same widget for display and edit in model-view
If you displayed using widgets, your view would be awfully slow. Imagine having a million widgets and updating positions of each of them when you scroll through the view. If you want widgets then use QScrollArea.The whole idea of rendering using delegates is to avoid widgets.
1 Attachment(s)
Re: Use of same widget for display and edit in model-view
Hey, thanks for the reply.
I understand the problem you described.
However, I have a problem in hand to solve it where in I thought of using the QTableView.
(see the attachment)
Can you provide me some input how this can be achived in Qt?
Re: Use of same widget for display and edit in model-view
Code:
<Vertical Layout >
< Horizontal layout >
<line edit>
< check box >
< Horizontal layout >
<line edit>
< check box >
Get the idea ?
Use above layout in QScrollArea...
Re: Use of same widget for display and edit in model-view
However, I feel layout are good if number items are fixed.
Also I would like to have sorting enabled for columns, having headers, dynamic add and delete of rows etc.
With above approach I would be end of reimplimenting most of the properties of QTableView.
Is there any alternative approch and a clean solution?
Added after 1 40 minutes:
Quote:
Originally Posted by
wysota
If you displayed using widgets, your view would be awfully slow. Imagine having a million widgets and updating positions of each of them when you scroll through the view. If you want widgets then use QScrollArea.The whole idea of rendering using delegates is to avoid widgets.
Is there any readymade painter for LineEdit and CheckBox?
Re: Use of same widget for display and edit in model-view
You can mimic a widget by using QStyle API. It has a number of draw* methods for drawing widgets. But it's only drawing.
Re: Use of same widget for display and edit in model-view
I believe you can use Qt::CheckStateRole and Qt::ItemIsUserCheckable flags to display and edit the check box without using a delegate. For the line edit, you can probably cache a pixmap of a QLineEdit containing the cell text and bitblt that in the paint event. This will take some trickery - the delegate isn't visible until the item is edited, so you will need to make a dummy QLineEdit, paint it, and cache it when you initially fill the table and repaint it when the table is resized or changed.
But from a usability point of view, wouldn't it be very confusing to a user if every cell in a table looked like it was being edited all the time? You would have to pay very close attention to where the input cursor was in order to know which cell you were editing. This is not a very intuitive interface, in my opinion.
Re: Use of same widget for display and edit in model-view
Quote:
Originally Posted by
d_stranz
I believe you can use Qt::CheckStateRole and Qt::ItemIsUserCheckable flags to display and edit the check box without using a delegate.
It's quite the opposite. It is the delegate that draws the box. There is no real checkbox widget there.
Quote:
For the line edit, you can probably cache a pixmap of a QLineEdit containing the cell text and bitblt that in the paint event.
You don't need to cache anything. There is an API for this.
Re: Use of same widget for display and edit in model-view
Quote:
Originally Posted by
wysota
It's quite the opposite. It is the delegate that draws the box. There is no real checkbox widget there.
But you (the application writer) do not need to create an explicit checkbox delegate in order to have an editable checkbox appear in a table, isn't that correct? It is something the framework takes care of for you when you set the appropriate flags. That's what I was getting at.
Quote:
You don't need to cache anything. There is an API for this.
Through QStyle? Or through QStyledItemDelegate or a subclass? Can you point to an example?
Thanks.
Re: Use of same widget for display and edit in model-view
Quote:
Originally Posted by
d_stranz
But you (the application writer) do not need to create an explicit checkbox delegate in order to have an editable checkbox appear in a table, isn't that correct?
There is already a delegate setup for you when you create a view.
Quote:
Through QStyle? Or through QStyledItemDelegate or a subclass? Can you point to an example?
Code:
style()->drawControl(...); // or similar