Custom QStyledItemDelegate to customize apperence of QListView with a SQLModel under the hood
-
Hello everyone, i'm trying to achive a custom ListItemWidget that has 3 text element retrive from an SQL model (postgress version).
QT version either 6.7.2 or 6.8
QT creator 14
Built with MinGW_64I do not want to create a stand-alone widget and wrap it inside a listItem because is not memory efficient and I may end up with hundreds of items.
Description:
-
I have a PostGres DB with this colum: "Comment_posted_datetime" (NOT NULL UNIQUE), "USERNAME" (NOT NULL), "comment_body_txt" (NOT NULL), "modified_datetime".
-
the red-text for modified is present only if the "modified_datetime" has a value, if it doesn't have it with not be drawn
-
when theitem is double-clicked can be modified, only the body text should be editable with a QTextEdit Widget
this is how the final widget should look like:
I tryed to follow along the StarEditor Example provided with the documentation but is too different from what i'm trying to achive and is the first time I'm facing off this time of deep-level costomization.
Can someone help me to understand the core logic and steps that must be done to pull off this type of tasks?
thanks.
Andrea. -
-
@SGaist I knew it could potentially become an issue, but I decided to use that model temporarily to create and test the UI.
I guess this is also the cause why I can't subscribe the changes to the model.Now I'm refactoring the code to make it cleaner, after that I will try to use a QSqlRelationalModel or give up and create a customModel to filter and sort the data without a QFilterSortProxyModel..
this is how my database is Structed at the moment:
-
Hi,
The fact that your data come from a database is of no consequences here.
You can grab whatever you need based on the index passed to the QStyledItemDelegate::paint method using the QModelIndex::siblingAtColumn method.From there, you can test for whatever conditions you need to draw what you want. You can use QPainter::drawText to render the text you want. You will have to change the font used by the painter prior to painting.
As for the custom editor, use createEditor, setEditorData and setModelData to manage the edition part.
-
thanks @SGaist ; the tip to use QModelIndex::siblingAtColumn helped me to make a step toward the right direction.
I refactor the code and now i'm able to display something inside the QListView although the comments are painted over eachother and I don't know if i need to translate the painter every time a widget is painted or what
the Widget' ::paint() method is as follows:
void CommentsWidget::paint(QPainter *painter, const QRect &rect, const QPalette &palette, EditMode mode) const { Q_UNUSED(palette) painter->save(); painter->setRenderHint(QPainter::Antialiasing, true); painter->setPen(Qt::NoPen); const auto BG = (mode == EditMode::ReadOnly) ? CARD_BG_NORMAL : CARD_BG_SELECTED; painter->setBrush(BG); /* transparent background */ painter->fillRect(rect, Qt::transparent); /* card bg and border + clipping */ QPainterPath card_path; card_path.addRoundedRect(0, 0, rect.width(), rect.height(), CARD_BORDER_RADIUS, CARD_BORDER_RADIUS); painter->setClipPath(card_path, Qt::IntersectClip); painter->drawRoundedRect(0, 0, rect.width(), rect.height(), CARD_BORDER_RADIUS, CARD_BORDER_RADIUS); /* header */ auto headers_font = this->builtFont(HEADER_FONT_SIZE, "Inter", QFont::DemiBold); this->setBrushPenColor(painter, QColor(Qt::black), QColor(Qt::black)); /* calculate font bound */ QFontMetrics header_metrics(headers_font); int timestamp_h = header_metrics.boundingRect(m_comment_date).height(); /* timestamp header */ painter->drawText(QPoint(SIDE_MARGINS, SPACING), m_comment_date); /* text drawn only if the comment was modified */ int subheader_m_h{}; if (m_modified != "") { this->setBrushPenColor(painter, QColor(129, 18, 18), QColor(129, 18, 18)); auto subHeaderFont = builtFont(HEADER_FONT_SIZE - 2, "Inter", QFont::DemiBold); painter->setFont(subHeaderFont); int offset = SPACING + timestamp_h + 4; painter->drawText(QPoint(SIDE_MARGINS, offset), m_modified); QFontMetrics subheader_m(subHeaderFont); subheader_m_h = subheader_m.boundingRect(m_comment_date).height(); } /* BODY TEXT */ QFont body_font = this->builtFont(BODY_FONT_SIZE, "Inter", QFont::Normal); painter->setFont(body_font); this->setBrushPenColor(painter, QColor(Qt::black), QColor(Qt::black)); /* --- word wrapping --- */ QTextOption txt_opt; txt_opt.setAlignment(Qt::AlignLeft); txt_opt.setWrapMode(QTextOption::WordWrap); auto lines_count = (m_comment_body.length() / LINE_LENGHT) + 1; auto body_single_line_h = QFontMetrics( this->builtFont(BODY_FONT_SIZE, "Inter", QFont::Normal) ).boundingRect(m_comment_body).height(); QRectF body_rect( SIDE_MARGINS, SPACING + timestamp_h + 4 + subheader_m_h + SIDE_MARGINS, 275, lines_count * body_single_line_h ); painter->drawText(body_rect, m_comment_body, txt_opt); painter->restore(); /* painter->drawText( QPoint(SIDE_MARGINS, SPACING + timestamp_h + 4 + subheader_m_h + SIDE_MARGINS), m_comment_body ); */ }
test purpose program description:
-
I have a Top LineEdit that every time a "Serial number" is entered, filter the SQL model and refresh the Two View
-
contains A TableView is placed to see what values are passed to the ListView, to check the correctness of the information
-
contains A listView that i'm trying to customize like the image posted in the first post.
this is what I currently have:
.problems I saw
the view have allocate the right amount of space for each records (in this case 8) and is selectable (as can be seen by the orange rectangle) , but the CommentsWidget(s) are drawn over each other and the spacing inside is not correct.
Sometimes when the body_text and up spaning over two rows, the widget displays only the first. When I randomly hover, and move inside the white comment box, it switch text to another record
-
-
@jsulm can u please give me support about the customised list view with the delegate and representation ?
thanks
-
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