PDA

View Full Version : QStyledItemDelegate Weird Error



daleotar
19th July 2011, 18:54
Hi.

I'm subclassing a QStyledItemDelegate in order to attach it to a QTableWidget.
It is working fine but I want to emit a signal from the delegate when I do some stuff inside the call of reimplemented QStyledItemDelegate::setEditorData, for instance:




class MyDelegate : public QStyledItemDelegate
{
Q_OBJECT

public:
MyDelegate(QObject* parent);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;

signals:
void mySignal(int value);

};

void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
int i = 1;
...do some stuff...

emit mySignal(i);
}


However I have a problem that I can't understand... The compiler is telling me:
"error: passing 'const MyDelegate' as 'this' argument of 'void MyDelegate::mySignal(int)' discards qualifiers"

I can't understand this error if I'm passing an int as an argument, not a "MyDelegate"...
If someone can explain or knows something about this or maybe, if anyone thinks that this is a bug...
Well, any help is appreciated...

Santosh Reddy
19th July 2011, 19:28
Define the signal this way,

...
signals:
void mySignal(int value) const;
...

daleotar
19th July 2011, 19:44
Thanks Santosh for your very opportune reply like always...

Well, Now it works, thanks a lot... However now my question is why do we have to define the signal as a const?

Again...Thanks a lot, XD

Santosh Reddy
19th July 2011, 19:50
a const function can call only another const function(s), signal is also a function, but you don't need to implement it, Qt (moc) will implement it for you.

daleotar
19th July 2011, 21:56
thanks for the clarification, I'm feeling like an idiot because the solution is kinda obvious, XD