Thanks for the sample code. I expanded the code to illustrate my problem:
DelegateProblem.h
#include <QtGui>
{
Q_OBJECT
public:
~MyTextEdit() {}
Q_SIGNALS:
void EnterPressed();
};
{
Q_OBJECT
public:
ButtonEditor
(QWidget *parent
= NULL);
~ButtonEditor() { qDebug() << "Destroyed" << this; }
virtual void mouseDoubleClickEvent
( QMouseEvent * event
);
MyTextEdit *mTextEdit;
Q_SIGNALS:
void SignalEditingFinished
(QWidget*);
public slots:
void EditingFinished();
};
class MyDelegate: public QStyledItemDelegate
{
Q_OBJECT
public:
explicit MyDelegate
(QObject *p
= 0);
{
ButtonEditor *btn = new ButtonEditor(parent);
Q_ASSERT(connect(btn,
SIGNAL(SignalEditingFinished
(QWidget*)),
this,
SLOT(SlotCloseEditor
(QWidget*))));
return btn;
}
public slots:
};
class DelegateProblem
: public QWidget{
Q_OBJECT
public:
DelegateProblem
(QWidget *parent
= 0, Qt
::WFlags flags
= 0);
~DelegateProblem() {}
};
#include <QtGui>
class MyTextEdit: public QTextEdit
{
Q_OBJECT
public:
explicit MyTextEdit(QWidget *p = 0): QTextEdit(p) {}
~MyTextEdit() {}
void keyPressEvent(QKeyEvent *event);
Q_SIGNALS:
void EnterPressed();
};
class ButtonEditor : public QAbstractButton
{
Q_OBJECT
public:
ButtonEditor(QWidget *parent = NULL);
~ButtonEditor() { qDebug() << "Destroyed" << this; }
virtual void mouseDoubleClickEvent( QMouseEvent * event );
virtual void paintEvent(QPaintEvent *e);
MyTextEdit *mTextEdit;
Q_SIGNALS:
void SignalEditingFinished(QWidget*);
public slots:
void EditingFinished();
};
class MyDelegate: public QStyledItemDelegate
{
Q_OBJECT
public:
explicit MyDelegate(QObject *p = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
ButtonEditor *btn = new ButtonEditor(parent);
Q_ASSERT(connect(btn, SIGNAL(SignalEditingFinished(QWidget*)), this, SLOT(SlotCloseEditor(QWidget*))));
return btn;
}
public slots:
void SlotCloseEditor(QWidget*);
};
class DelegateProblem : public QWidget
{
Q_OBJECT
public:
DelegateProblem(QWidget *parent = 0, Qt::WFlags flags = 0);
~DelegateProblem() {}
};
To copy to clipboard, switch view to plain text mode
DelegateProblem.cpp
#include "DelegateProblem.h"
DelegateProblem
::DelegateProblem(QWidget *parent, Qt
::WFlags flags
){
t->setItemDelegate(new MyDelegate(t));
layout->addWidget(t);
setLayout(layout);
}
//---------- MyDelegate---------------------------------------------
//-------------------------------------------------------------------
MyDelegate
::MyDelegate( QObject *p
/*= 0*/ ) : QStyledItemDelegate(p)
{
}
void MyDelegate
::SlotCloseEditor( QWidget* ) {
qDebug() << "I don't hit ~ButtonEditor() afterwards";
}
//---------- ButtonEditor -------------------------------------------
//-------------------------------------------------------------------
void ButtonEditor
::mouseDoubleClickEvent( QMouseEvent * event
) {
mTextEdit->setText(text());
mTextEdit->setVisible(true);
Q_ASSERT(connect(mTextEdit, SIGNAL(EnterPressed()), this, SLOT(EditingFinished())));
}
{
opt.initFrom(this);
opt.palette = palette();
opt.
state |
= QStyle::State_AutoRaise;
painter.
drawControl(QStyle::CE_PushButtonBevel, opt
);
}
ButtonEditor
::ButtonEditor( QWidget *parent
/*= NULL*/ ) {
mTextEdit = new MyTextEdit(this);
mTextEdit->setVisible(false);
mTextEdit->setAcceptRichText(false);
mTextEdit
->setFrameShape
(QFrame::Box);
mTextEdit
->setFrameShadow
(QFrame::Plain);
}
void ButtonEditor::EditingFinished()
{
mTextEdit->setVisible(false);
emit SignalEditingFinished(this);
}
//---------- MyTextEdit -------------------------------------------
//-------------------------------------------------------------------
void MyTextEdit
::keyPressEvent( QKeyEvent *event
) {
if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
{
emit EnterPressed();
}
}
#include "DelegateProblem.h"
DelegateProblem::DelegateProblem(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags)
{
QVBoxLayout *layout = new QVBoxLayout;
QTableWidget *t = new QTableWidget(5, 5);
t->setItemDelegate(new MyDelegate(t));
t->setEditTriggers(QAbstractItemView::CurrentChanged | QAbstractItemView::SelectedClicked | QAbstractItemView::DoubleClicked);
layout->addWidget(t);
setLayout(layout);
}
//---------- MyDelegate---------------------------------------------
//-------------------------------------------------------------------
MyDelegate::MyDelegate( QObject *p /*= 0*/ )
: QStyledItemDelegate(p)
{
Q_ASSERT(connect(this, SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)), this, SLOT(SlotCloseEditor(QWidget*))));
}
void MyDelegate::SlotCloseEditor( QWidget* )
{
qDebug() << "I don't hit ~ButtonEditor() afterwards";
}
//---------- ButtonEditor -------------------------------------------
//-------------------------------------------------------------------
void ButtonEditor::mouseDoubleClickEvent( QMouseEvent * event )
{
mTextEdit->setText(text());
mTextEdit->setVisible(true);
Q_ASSERT(connect(mTextEdit, SIGNAL(EnterPressed()), this, SLOT(EditingFinished())));
}
void ButtonEditor::paintEvent( QPaintEvent *e )
{
QStylePainter painter( this );
QStyleOptionButton opt;
opt.initFrom(this);
opt.palette = palette();
opt.state |= QStyle::State_AutoRaise;
painter.drawControl(QStyle::CE_PushButtonBevel, opt);
}
ButtonEditor::ButtonEditor( QWidget *parent /*= NULL*/ )
: QAbstractButton(parent)
{
mTextEdit = new MyTextEdit(this);
mTextEdit->setVisible(false);
mTextEdit->setAcceptRichText(false);
mTextEdit->setFrameShape(QFrame::Box);
mTextEdit->setFrameShadow(QFrame::Plain);
}
void ButtonEditor::EditingFinished()
{
mTextEdit->setVisible(false);
emit SignalEditingFinished(this);
}
//---------- MyTextEdit -------------------------------------------
//-------------------------------------------------------------------
void MyTextEdit::keyPressEvent( QKeyEvent *event )
{
if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
{
emit EnterPressed();
}
QTextEdit::keyPressEvent(event);
}
To copy to clipboard, switch view to plain text mode
Double click on the button and the custom text edit appears. after entering something, press the Enter key: MyDelegate::SlotCloseEditor() gets called but not the ButtonEditor() destructor (unless I click into another cell)
What prevents it from being called?
Thanks!
Bookmarks