PDA

View Full Version : Use of same widget for display and edit in model-view



NIRANJAN BAI
12th February 2011, 04:43
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.

wysota
12th February 2011, 06:39
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.

NIRANJAN BAI
12th February 2011, 07:28
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?

aamer4yu
12th February 2011, 07:57
<Vertical Layout >
< Horizontal layout >
<line edit>
< check box >
< Horizontal layout >
<line edit>
< check box >
Get the idea ?
Use above layout in QScrollArea...

NIRANJAN BAI
12th February 2011, 10:04
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:


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?

wysota
12th February 2011, 20:35
You can mimic a widget by using QStyle API. It has a number of draw* methods for drawing widgets. But it's only drawing.

d_stranz
14th February 2011, 18:18
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.

wysota
14th February 2011, 18:40
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.


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.

d_stranz
15th February 2011, 18:02
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.


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.

wysota
17th February 2011, 22:31
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.



Through QStyle? Or through QStyledItemDelegate or a subclass? Can you point to an example?

style()->drawControl(...); // or similar