Custom QStyledItemDelegate to customize apperence of QListView with a SQLModel under the hood
-
Did you check that the rectangle you pass to your paint method is the correct one ?
-
I did some prints after u suggest to take a look at the Rectangle passed to the paint event of the widget and I found that the sizes were right, and the problem was in the "painter->draw()" method because I was passing x= 0 and y= 0 instead of the rect.x() and rect.y() so the body's messages were drawn on top of each others.
/* how it was originally */ painter->drawRoundedRect(0, 0, rect.width(), rect.height(),
/* card bg and border + clipping */ QPainterPath card_path; card_path.addRoundedRect(rect.x(), rect.y(), rect.width(), rect.height(), CARD_BORDER_RADIUS, CARD_BORDER_RADIUS); painter->setClipPath(card_path, Qt::IntersectClip); /* how should have been */ painter->drawRoundedRect(rect.x(), rect.y(), rect.width(), rect.height(), CARD_BORDER_RADIUS, CARD_BORDER_RADIUS);
the results now is like so:
Before mark this post as solved, I need to resolve the issues correlated to the multi-line body text been chopped off. If someone have a hint I will appreciate it, but in the meanwhile I'am going to search the solution checking the SizeHint() methods
-
Can you show an example of chopped off text ?
-
You can see it directly from the screen-shot above where the fifth comment-box has the text “Bentley never ever got” and is missing the word “problem” as you can see in the table widget where it displays the full data for reference and debugging purposes.
on top of that I have trouble to achieve a editor modification on the comment, displayed:
- On double clicking the comment-box the “edit-mode is triggered
- the edit mode, simply change the background color and should allow the user to edit the “body_text” (example.. the bantley never ever got problems” in maybe “Bentley should have A/C checked” )
but I’m stuck trying to thinking about how can I place a QTextEdit widget to place over the body for allowing text modification..
I tried to create a QTextEdit widget in the createEditorWidget method of the custom class that subclass QStyledItem but how I draw it over the CommentsWidget?
-
Hi, what about implementing
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override { return new QTextEdit(parent); }
from
QStyleItemDelegate
?Where is the TextEdit shown then?
-
thanks @Pl45m4 for the reply , I appreciate it so much, at the moment i don’t have access to the code in my working’s pc until Monday, but what I have in mind (theoretically speaking), after the list view receive a double-click on an item, the EditMode::edit will be passed to the widget paint event responsible for drawing the commentsWidget.
after that i want to replace the hard-coded text of the body with a functional and editable QTextEdit that contains the body text which can be taken from the member variable m_body_comment.when the return key is pressed, the QStyledItem will emit the EditingFinished signal and clode the editor.
I hope to be able to express clearly my plan, if not let me know
-
It's a two step operation:
- createEditor
- setEditorData
With the second you can set whatever is appropriate in the editor from your model.
-
@SGaist you are completly right, and I was confused following along the StarDelegate example that is diffrent from what I need.
So I adjust the EditorWidget with is own UI and the QPlainTextEdit, I was trying to test it but i notice that the "QStyledItemDelegate::createEditor()" is never called. I check online and the suggestion is to check the item's flags, so I did and the result is: "QFlagsQt::ItemFlag(ItemIsSelectable|ItemIsEnabled|ItemNeverHasChildren)".
As expected, the Items are missing the "isEditable" flags but I think there is not a direct way to modify ALL the item's flags at once from the QListView (at least from what I red).
Is my only choise left, to subclass QListView and re-implement the ::flags() method ?
this is my current basic setup in the main window for the default QListView:
// lst_vw rappresent the QListView placed in the UI file ui->lst_vw->setItemDelegate( new CommentListItem ); ui->lst_vw->setEditTriggers(QAbstractItemView::EditTrigger::DoubleClicked); ui->lst_vw->setModel(_m_model);
I already learn that this code inside MainWindow's constructor isn't enough.. what I'm asking is: Is it possible to set the editable flags without sub-classing the QListView and re-implement flags() ?
-
Usually this is done at the model level. Are you using one of the QSql models ?
-
@Andrea_Venturelli
Qt::ItemFlags
, includingQt::ItemIsEditable
and theflags()
method, come from the model, not from the view. So I don't know why you are thinking of subclassingQListView
, it does not store item flags or have aflags()
method to override.So you need to change what the model returns. Derive from
QAbstractItemModel
(or one of its existing derivations) and OR-in theItemIsEditable
to make the model editable. https://doc.qt.io/qt-6/qabstractitemmodel.html#subclassingTo enable editing in your model, you must also implement setData(), and reimplement flags() to ensure that ItemIsEditable is returned.
If you were using a
QListWidget
which hasQListWidgetItem
s you could callQListWidgetItem::setFlags()
to change an item's flags. However that is a "layer" added byQListWidget
to theQListView
you have. Internally it is presumably implemented byQListWidget
storing values to return from its model'sflags()
.For SQL stuff you should find that
QSqlQueryModel
returns non-editable flags (cannot edit items returned from a query), unless you subclass it. WhileQSqlTableModel
returns editable flags (can edit items stored in a table). You may want to subclass those, it might also be possible to interpose a (sub-classed)QAbstractProxyModel
where you overrideflags()
between your view and the SQL model.All ways round the one thing you do not need to do is change the
QListView
:) -
ahaha @JonB you are a legend, and I have a lot to study in order to understand very well the Model\View stuffs.
But bised that, you were right about the QSqlTableModel, and changing the model from QSqlQueryModel to it, "kinda worked"
Now (without trying to subclass anything) I got only the first item editable, and the others do not flinch.screenshot showing the first item is editable:
based on yours experience, which approach do you think is better and more elegant to resolve the problem (the problem is obviously my ignorance :) )?
-
The Query Model Example shows how to make a QSqlQueryModel editable.
-
@Andrea_Venturelli said in Custom QStyledItemDelegate to customize apperence of QListView with a SQLModel under the hood:
it, "kinda worked"
Now (without trying to subclass anything) I got only the first item editable, and the others do not flinch.I'm not sure what you're saying. From a
QSqlTableModel
you should be able to edit any/all columns, e.g. in aQTableView
, provided there is nothing "special" about their definitions. YourQListView
is only going to show/edit one column. Are you saying you have what you want or something else is a problem?If what you want to do is edit rows/columns in a SQL table
QSqlTableModel
should do fine. I think you only would want an editableQSqlQueryModel
instead in certain specialised circumstances.Don't forget there are two possibilities for presenting a UI to edit columns in tables. In one case the user does the edits "in situ" on items in the
QTableView
, by clicking on them there. This may indeed be what you want. But sometimes you might want to offer a "master-detail" view, where the user clicks on a row in the table view to read whichever editable fields into their own dedicated widgets not sitting on the table view. If you want that Qt offers QDataWidgetMapper. Just so you are aware. -
@JonB
I have a Sql model with all the informations needed to display the custom item inside a list view.the normal QStyledItemDelegate draw a rectangle with 3 labels (taken from the SqlModel):
-
- the string rappresenting the date when the message was first posted
-
- [optional] a string rappresenting the date when the “body” last modification occured (also taken from the Sql model, a empty string is provided if the comment was never edited before)
-
- the body containing a description about the problem or whatever (also present in the QSqlModel inside its specific column)
When the user double-click the comment he wants to modify, the createEditor() will provide the same data as before (post_date, post_modification_date if available, and the body text) but this time the body_text is not a label or a drawn text but it is a QPlainTextEdit.
I install a filterEvent on the QPlainTextEdit so when the user presses “Ctrl+Return” it emit a CommitAndCloseEditor signal to tell Qt to destroy the editor and save the data to the model (olso adding or updating the post_modified_date).
every rectangle rappresented inside the listView corresponds to a row of the model.
In the screenshot I posted, the first item , when double-clicked, work as expected and the Editor with the QPlainTextEdit. But if I try to do the same with the second, third and so on, the Editor don’t show up.
that’s all for the moment
-
-
@Andrea_Venturelli
I guess start by verifying whether the table view is seeing theItemIsEditable
flag for items when you double click (or whatever you have set) to edit them. Since that's talking about editing in place in aQTableView
I don't really see how that has anything to do with whatever you are doing with theQListView
. Or maybe you don't want cells editable in theQTableView
. Or maybe you want clicking in a cell in any column to invoke editing in theQListView
. I really don't know.In the screenshot I posted, the first item , when double-clicked, work as expected and the Editor with the QPlainTextEdit. But if I try to do the same with the second, third and so on, the Editor don’t show up.
First, second and third what?? What's an "item"? Are you talking about coulmns or rows or what? It would be a lot clearer if you stated this sort of thing.
UPDATE
Oh, I think your "items" are lines inside the List Item delegate, each of which corresponds to a column in the table view? So to edit columns within one row in the database. And you are having problems with which item you click and getting into the right edit. Or something like that. Quite outside of anything I have done. I just know about columns/row/cell editing, with or without delegates, of a table model in aQTableView
. -
Sorry @JonB I do not repeat an important point that I specified at the very early posts.
the table you see on the right is ONLY FOR DEBUG, on the final widget will only exist the “listView” with the custom delegate.
the table is only needed for me to troubleshoot potential errors and for the other here helping me understand what was given to the ListView!all the window is a test for develop the listView. it will be a stand alone widget where under it I’ll add a lineEdit to add new comments to the ListView.
THE TABLE IS ONLY FOR REFERENCE!Sorry if was not clear from the beginning
-
@Andrea_Venturelli
Yeah, that was my misunderstanding.You have a delegate which shows all the "columns" in one layout at different places. So won't you have to detect where user clicked to decide which "column" to edit?
-
not column , I go by row
every row is a record to my custom item in the ListViewid | body_text | post_date | modified_post_dt
1 basic text 12/02/2024 12/03/2024displayed as:
—————————-12/02/2024
12/03/2024
basic text
——————————
-
@Andrea_Venturelli
Yes I get that now. One list element shows all the columns in a database row, And you don't care where inside that user clicks because you only allow editing the comment column.So what you are saying is that the first row of the model is editable via the first item in the list view but the second is not, right? :) Then start by printing out the
flags()
for model index (row) 0 and 1 to verify the latter hasItemIsEditable
? -
exactly ! you get it right ahah
I printed the flags and the first give me “isEditable” with all the rest usual thinks (selectable, enabled, nochild)
but all the others don’t have it.. this looks strange to me. I’ll investigate deeper soon and I’ll post if I find something interesting