Thanks for your advice, now it works perfectly without strange behaviours, if someone has the same problem, here comes the code of the delegate:
*.h
#ifndef STRINGDELEGATE_H
#define STRINGDELEGATE_H
#include <QItemDelegate>
#include <QLineEdit>
{
Q_OBJECT
public:
explicit stringDelegate
(int length,
QObject *parent
= 0);
signals:
public slots:
void commitAndCloseEditor();
private:
int length;
};
#endif // STRINGDELEGATE_H
#ifndef STRINGDELEGATE_H
#define STRINGDELEGATE_H
#include <QItemDelegate>
#include <QLineEdit>
class stringDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit stringDelegate(int length, QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const;
signals:
public slots:
void commitAndCloseEditor();
private:
int length;
};
#endif // STRINGDELEGATE_H
To copy to clipboard, switch view to plain text mode
*.cpp
#include "stringdelegate.h"
stringDelegate
::stringDelegate(int length,
QObject *parent
) :{
this->length=length;
}
line->setMaxLength(length);
line->setFrame(false);
connect(line, SIGNAL(editingFinished()),this, SLOT(commitAndCloseEditor()));
return line;
}
void stringDelegate::commitAndCloseEditor(){
QLineEdit *editor
= qobject_cast<QLineEdit
*>
(sender
());
emit commitData(editor);
emit closeEditor(editor);
}
#include "stringdelegate.h"
stringDelegate::stringDelegate(int length, QObject *parent) :
QItemDelegate(parent)
{
this->length=length;
}
QWidget* stringDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/ ) const{
QLineEdit *line = new QLineEdit(parent);
line->setMaxLength(length);
line->setFrame(false);
connect(line, SIGNAL(editingFinished()),this, SLOT(commitAndCloseEditor()));
return line;
}
void stringDelegate::commitAndCloseEditor(){
QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
emit commitData(editor);
emit closeEditor(editor);
}
To copy to clipboard, switch view to plain text mode
Bookmarks