[Interest] Double free in QItemDelegate editor

Kyle Edwards kyle.edwards at kitware.com
Fri Sep 4 21:22:22 CEST 2020


On 9/4/20 3:20 PM, Kyle Edwards wrote:
> I now have a minimum working test case: 

Err, looks like that got mis-formatted. Let me try again...

// BEGIN

#include <QItemDelegate>
#include <QApplication>
#include <QFileDialog>
#include <QLineEdit>
#include <QResizeEvent>
#include <QStandardItemModel>
#include <QToolButton>
#include <QTreeView>
#include <QWidget>

class TestEdit : public QLineEdit
{
     Q_OBJECT
public:
     TestEdit(QWidget* parent = nullptr);
     void resizeEvent(QResizeEvent* e) override;

public slots:
     void chooseFile();

private:
     QToolButton* toolButton;
};

TestEdit::TestEdit(QWidget* parent)
     : QLineEdit(parent)
{
     this->toolButton = new QToolButton(this);
     this->toolButton->setText("...");
     QObject::connect(this->toolButton, SIGNAL(clicked(bool)), this, 
SLOT(chooseFile()));
}

void TestEdit::resizeEvent(QResizeEvent* e)
{
   // make the tool button fit on the right side
   int h = e->size().height();
   // move the line edit to make room for the tool button
   this->setContentsMargins(0, 0, h, 0);
   // put the tool button in its place
   this->toolButton->resize(h, h);
   this->toolButton->move(this->width() - h, 0);
}

void TestEdit::chooseFile()
{
     auto path = QFileDialog::getOpenFileName(this, "title", QString(), 
QString(),
                                  nullptr, 
QFileDialog::DontResolveSymlinks);
     if (!path.isEmpty()) {
         this->setText(path);
     }
}

class TestItemDelegate : public QItemDelegate
{
     Q_OBJECT
public:
     QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& 
option, const QModelIndex& index) const override;
};

QWidget* TestItemDelegate::createEditor(QWidget* parent, const 
QStyleOptionViewItem& option, const QModelIndex& index) const
{
     return new TestEdit(parent);
}

int main(int argc, char** argv)
{
     QApplication app(argc, argv);
     QWidget window;
     QTreeView view(&window);
     auto* model = new QStandardItemModel;
     view.setModel(model);
     model->insertRow(0);
     model->insertColumn(0);
     model->setData(model->index(0, 0), "Hello");
     view.setItemDelegate(new TestItemDelegate);
     window.show();
     return app.exec();
}

#include "qttest.moc"

// END



More information about the Interest mailing list