Details
-
Bug
-
Resolution: Done
-
P1: Critical
-
5.6.0
-
None
-
Ubuntu Linux. Qt5 installed via downloaded installer, no ubuntu package.
OSX 10.10.5 (qt 5.7 branch, qtbase SHA1:c8e4d15004bade8b99aaf74b6f899911b5d66962)
-
b75e10684cb4d14e49059e6c85bfe60ca6ab9dff
Description
It was hard to find any information on how to actually remove a QTextFrame from a QTextDocument, but I finally found the info in the documentation of the QTextFrame destructor: "Destroys the frame, and removes it from the document's layout."
Unfortunately this results in a segmentation fault. In the example provided hit 'x' one or more times. Then hit 'y' to remove a frame, or use the button. Either case, the app will crash either immediately, or when using the editor next. It really shouldn't crash, should it?
#include <QApplication> #include <QtCore> #include <QtGui> #include <QtWidgets> namespace { class TestEdit : public QTextEdit { public slots: void removeCurrentFrame() { QTextCursor cursor = this->textCursor(); QTextFrame* currentFrame = cursor.currentFrame(); if (currentFrame != NULL && currentFrame != this->document()->rootFrame()) { //delete currentFrame; currentFrame->deleteLater(); qDebug() << "deleted, editor still working?"; } } protected: virtual void keyPressEvent(QKeyEvent *e) { if (e->key() == Qt::Key_X) { QTextCursor cursor = this->textCursor(); QTextFrameFormat frameFormat; frameFormat.setBackground(QColor(Qt::red)); QTextFrame* frame = cursor.insertFrame(frameFormat); } else if (e->key() == Qt::Key_Y) { removeCurrentFrame(); } QTextEdit::keyPressEvent(e); } }; } int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow* w = new QMainWindow(); w->setAttribute(Qt::WA_DeleteOnClose); TestEdit* testEdit = new TestEdit(); QWidget* widget = new QWidget(w); QVBoxLayout *vLayout = new QVBoxLayout(widget); QPushButton *button = new QPushButton("delete current frame"); vLayout->addWidget(button); vLayout->addWidget(testEdit); w->setCentralWidget(widget); QObject::connect(button, &QAbstractButton::clicked, testEdit, &TestEdit::removeCurrentFrame); w->adjustSize(); w->move(QApplication::desktop()->screen()->rect().center() - w->rect().center()); w->show(); return app.exec(); }