QStyledItemDelegate with QLineEdit
-
I want to draw a QLineEdit in the cell of the table.
I have:class ItemDelegate : public QStyledItemDelegate { public: ... void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override { QStyleOptionFrameV2 *panelFrame = new QStyleOptionFrameV2; QLineEdit *search = new QLineEdit; panelFrame->initFrom(search); panelFrame->rect = option.rect; panelFrame->state |= QStyle::State_Sunken; QApplication::style()->drawPrimitive(QStyle::PE_PanelLineEdit, panelFrame, painter); } ...
But this code does not work. How can i solve this problem?
-
Hi
What does it draw ?
In what way does not work ?
Note: you should delete both panelFrame and search as else you leak.
ps. did you get code from here ?
https://stackoverflow.com/questions/9897594/qt-how-to-draw-a-dummy-line-edit-control -
@mrjj said in QStyledItemDelegate with QLineEdit:
ps. did you get code from here ?
https://stackoverflow.com/questions/9897594/qt-how-to-draw-a-dummy-line-edit-controlI have seen that. Here I can not write a text too.
-
@Elnur_Ismailzada
No, as its painted. ?
If you want it "alive", you must provide full Delegate with CreateEditor also to make
a real edit when in edit mode. The paint part if only for non edit mode.http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html
You did also implement
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
? -
@Elnur_Ismailzada said in QStyledItemDelegate with QLineEdit:
I have seen that. Here I can not write a text too.
You mean it wont draw its text ?
did you try setting text on search?
Since its empty as you just create it. -
@mrjj said in QStyledItemDelegate with QLineEdit:
You did also implement
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;No. I'll try and let you know. thx
You mean it wont draw its text ?
did you try setting text on search?Yes and yes.
-
Hi
There is one thing i do wonder.
The default delegate for the Views is a LineEdit so
can i ask why you want to replace it again with delegate ?
You want draw extra stuff on it or how come the normal one is not good enough ? -
@Elnur_Ismailzada
So you want each cell to have 3 values?
Well u can do that with delegate but most of the work will be handling the editor widget.
it must then contain 3 Edits so user can select which one to edit and
you must handle writing back the data to the model in correct index.