Solved Subclass of QTextEdit makes the program to crash
-
@Gojir4 said in Subclass of QTextEdit makes the program to crash:
QWidget::focusOutEvent(e);
I was wrong here, you have to call
QTextEdit::focusOutEvent(e);
, sorry@davidesalvetti Actually I have tested your QCustomTextEdit class and I don't have any crash with it, so, as @SGaist suggested, I think it's coming from somewhere else in your code too.
-
@jsulm It would be the right solution if the crash would still remain also after starting the debugging.
When I use the debugging everything works fine, no crash. That's why I think it's some kind of optimization that is causing the problem. -
I think it's some kind of optimization that is causing the problem.
Don't blame the compiler, we all do and it's never its fault
ui->frame_3->setLayout(layout);
is useless. remove it- before
QVBoxLayout *layout = new QVBoxLayout(ui->frame_3);
addQ_ASSERT(!ui->frame_3->layout());
to make sure you don't have a layout already - after
qDebug() << "HERE8";
addif(!textedit) qDebug("textedit is NULL!");
- in the constructor add
connect(textedit,&QObject::destroyed,[](){qDebug("textedit was destroyed!");});
to check if it gets deleted
-
@VRonin Thanks for your answer.
I updated my code with your changes, this is the debug output:HERE4 HERE5
and then the crash...I don't understand :/
It crashes every time I call the textedit object. After "HERE5" there is the code lineif(!textedit) qDebug() << "textedit is NULL!"; qDebug() << "HERE8" << textedit;
But these message are not shown in the application output.
-
create your Pointer with a nullptr set
private: Ui::Archivio *ui; QCustomTextEdit *textedit = nullptr;
then it's created pointed to 0 and only changes when you
new
it, in case your function is called before the customTextEdit could be created. -
As an experiment you could also just try with a normal
QTextEdit
rather than aQCustomTextEdit
-
@J.Hilk Thanks for your answer. I tried but no changes, it's still crashing.
Anyway I don't know if this could be usefull but I've seen that sometimes it works right just for the first time I click the button, then I close the 'Archivio' and then I reopen it and clicking again on the button makes the crash.
-
@davidesalvetti
from the code example I can see that you have a ui-file for your class,why don't you Promote your CustomWidget in the Desginer and let Qt's internal manage the creation and destruction of the object?
-
@VRonin I just tried it. Creating the QTextEdit object from the constructor doesn't make change to the behaviour of the program. Is it possible that the problem is in how I create and put into the layout the Object?
If I create the QTexteEdit from the layout it works perfectly.
-
@J.Hilk That made the trick! I din't know about promoting custom widget, but it worked!
So there must be something wrong in the way I've put the object into the layout and into the frame.
If somebody knows what is the problem is welcome so that I can avoid it the next times.Anyway the topic is solved, thank you all.