Qt::DecorationRole returns QIcon (in the QVariant) not QPixmap, the correct way will be
Qt Code:
... ...To copy to clipboard, switch view to plain text mode
Qt::DecorationRole returns QIcon (in the QVariant) not QPixmap, the correct way will be
Qt Code:
... ...To copy to clipboard, switch view to plain text mode
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
What a coincidence, you are the person who posted the code in the first place!!!
Thank you very much, I had already tried that but for some reason I had uses static_cast instead of qvariant_cast.
Is there any way to get the original size of the image in the QListWidget, so I can get the editor to cover the item while editing (kind of like when I rename a file, so the QTextEdit would just expand below).
Furthermore whenever I de-comment
I get the following error:Qt Code:
... emit textChanged(mText); ...To copy to clipboard, switch view to plain text mode
symbol(s) not found for architecture x86_64
Even though I'm not sure why you implemented that method in the first place. I think it's needed to update the model data but i'm not sure how the two are connected. I've been reading around and if i'm not mistaken I should be the equivalent of the finishedEditing() which I should then connect to the commitAndClose() (parallel taken fromt the track editor example)
Could you expand a bit on that part.
Thank you very much
The post you took the code from has sightly different requirement, take that into account.What a coincidence, you are the person who posted the code in the first place!!!
Use QIcon::availableSizes(), and take the first size.Is there any way to get the original size of the image in the QListWidget, so I can get the editor to cover the item while editing (kind of like when I rename a file, so the QTextEdit would just expand below).
Make sure you use Q_OBJECT macro as the first line in the class definition, and make sure you have declared that function as a Qt signal.Furthermore whenever I de-comment
I get the following error:Qt Code:
... emit textChanged(mText); ...To copy to clipboard, switch view to plain text mode
symbol(s) not found for architecture x86_64
As I said earlier, the code was posted for a different requirement, the requirement was not to save the data into the model until the user clicks the save/apply pushButton explicitly. That is the reason updateText() slot and textChanged(QString) signal were suggested, and as such textChanged() was only provided to extend the functionality if required and has nothing to do with model/view, so just ignore it.Even though I'm not sure why you implemented that method in the first place. I think it's needed to update the model data but i'm not sure how the two are connected.
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
Hi,
I re-wrote the class to make sure I had understood how everything worked and to make sure there were no errors, and was able to get everything to work except for emitting the commitData fromt he QStyledItemDelegate. For some reason if I add the Q_OBJECT macro at the beginning of the definition I get a "symbol(s) not found for architecture x86_64" error, while if I do not add it it tells me "No such slot QStyledItemDelegate::FinishedEditing.
Header File
Qt Code:
#ifndef NEWITEMDELEGATE_H #define NEWITEMDELEGATE_H #include <QStyledItemDelegate> #include <QWidget> #include <QObject> class newItemDelegate : public QStyledItemDelegate { Q_OBJECT public: QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; private slots: void FinishedEditing(); }; #endif // NEWITEMDELEGATE_HTo copy to clipboard, switch view to plain text mode
CPP File
If i'm not mistaken emitting the commitData should change the value of QModelIndex in the QListWidgetQt Code:
#include "newitemdelegate.h" #include "newdelegatewidget.h" #include <QStyledItemDelegate> #include <QWidget> : QStyledItemDelegate(parent) { } QWidget * newItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const { newDelegateWidget *editWidget = new newDelegateWidget(parent, index); connect (editWidget, SIGNAL(lostFocus()),this, SLOT(FinishedEditing())); return new newDelegateWidget(parent, index); } { static_cast<newDelegateWidget*>(editor)->SetText(index.data(Qt::EditRole).toString()); } void newItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const { model->setData(index, value, Qt::EditRole); } void newItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { static_cast<newDelegateWidget*>(editor)->setGeometry(option.rect); } void newItemDelegate::FinishedEditing() { newDelegateWidget *editor = qobject_cast<newDelegateWidget *>(sender()); emit commitData(editor); emit closeEditor(editor); }To copy to clipboard, switch view to plain text mode
NO. Edited data has to be saved into the Model (QListWidget's Model) in setModelData(), and additional signal / slots are not required.If i'm not mistaken emitting the commitData should change the value of QModelIndex in the QListWidget
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
Ok, so if I connect the signal the widget I made emits when it looses focus, to the slot setModelData() like this:
whenever the widget loosed focus it should call the setModelData, and update the information. Still, when I add Q_OBJECT i get the symbols error, while if the macro is not added I get the slot (setModelData()) does not exist. What am I missing?Qt Code:
... QWidget * newItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const { newDelegateWidget *editWidget = new newDelegateWidget(parent, index); connect (editWidget, SIGNAL(lostFocus()),this, SLOT(setModelData(editWidget,option,index))); return new newDelegateWidget(parent, index); } ...To copy to clipboard, switch view to plain text mode
First thing setModelData() is not a slot, so you cannot connect it to a signal.Ok, so if I connect the signal the widget I made emits when it looses focus, to the slot setModelData() like this:
Second, you need not call setModelDataa(), view will call it when you editing is finished.
So I repeat, REMOVE SIGNALS AND SLOTS, YOU DON'T NEED THEM, THEY WERE ADDED FOR SOME OTHER REQUIREMENT, this I mentioned in my earlier two posts.
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
caster89 (25th April 2013)
Ok, so I had seen the post, but even removing the signals I still couldn't get the model to update when I finished editing, that is why I thought I had to reput the signals. I then realised the problem was that getText() I was returning the QString which did not update, by returning QTextEdit::toPlainText the data is updated.
Furthermore the reason i got the error when adding Q_OBJECT was that I forgot to delete the moc file, after that I didn't get the error anymore.
Thank you for your help
Bookmarks