Unsolved find child control
-
I create QTextEdit dynamically and what to get it next, but have app crash& Please told me where is error?
QTextEdit *O1Date= new QTextEdit("10.06.2016"); O1Date->setObjectName("O1Date"); QGraphicsScene *scene = new QGraphicsScene; QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(); proxy->setWidget(O1Date); scene->addItem(proxy); ui->graphicsView->setScene(scene);
And then
QTextEdit* O1Date1 = ui->graphicsView->findChild<QTextEdit*>("O1Date"); -
Where exactly crashes your application? Did you check the pointer returned by
findChild()
before you access/use it? -
Yes^ it is not accessible....
-
I just checked the documentation. I'm not an expert so please don't take what I'm saying for granted...
First of all,QGraphicsView::setScene()
doesn't make the scene become a child of theQGraphicsView
. Also,QGraphicsProxyWidget::setWidget()
doesn't make the widget become a child ofQGraphicsProxyWidget
. Hence you will never find your widget by callingfindChild()
on your view.So either you store a pointer to your
QTextEdit
widget yourself or you useQGraphicsProxyWidget::widget()
to access it.Again: I might be wrong and there might be a better/proper solution to this.
-
The problem here is you do not give a parent to O1Date
QTextEdit *O1Date= new QTextEdit("10.06.2016");
should become
QTextEdit *O1Date= new QTextEdit("10.06.2016",ui->graphicsView);
-
@VRonin said:
The problem here is you do not give a parent to O1Date
Well, look at that. I missed the obvious solution...
-
@VRonin said:
should become
QTextEdit *O1Date= new QTextEdit("10.06.2016",ui->graphicsView);In this case i cannot rotate it in graphic view :(
-
@sashapont
Tracing the inheritance tree, I'd look for the text edit in the scene, not in the view.QTextEdit *O1Date= new QTextEdit("10.06.2016"); O1Date->setObjectName("O1Date"); QGraphicsScene *scene = new QGraphicsScene; QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(); proxy->setWidget(O1Date); scene->addItem(proxy); ui->graphicsView->setScene(scene);
Then:
ui->graphicsView->scene()->findChild<QTextEdit *>("O1Date");
In any case I'd think the most simplest solution is to just keep a pointer to your item. E.g.
QPointer<QTextEdit> O1Date; //< This goes into the header file O1Date = new QTextEdit("10.06.2016"); //< This goes into the source file
EDIT:
Although the scene uses it's own child tree as can be seen here. -
@kshegunov said:
ui->graphicsView->scene()->findChild<QTextEdit *>("O1Date");
Not working :( app crashes
-
-
I see textedit, and that rotated^ that scene is not NULL
-
@sashapont
I'm sorry I don't follow. Do you mind uploading a screen shot and a stack trace of the crash. Also the exact type of the crash would be helpful. -
Thats all project
untitled -
@sashapont
Your app crashes because you don't handle the case when the line edit is not found.QTextEdit * O1Date1 = ui->graphicsView->scene()->findChild<QTextEdit *>("O1Date");
is NULL and you're directly calling:
O1Date1->setText("");
Which brings me to my original point - use a pointer to your widget instead of recursively searching through the object tree.
-
@kshegunov said:
QPointer<QTextEdit> O1Date;
When I try to write that in header file i have error
implicit instantiation of undefined template 'QPointer<QTextEdit>'
QPointer<QTextEdit> O1Date;
^I try to use static variables in header and it works thank you!
But i have new issue
Now i can set value to the text edit^ And in qDebug I see that it set, but in control i see old value... How i can redraw that? -
@sashapont said:
implicit instantiation of undefined template 'QPointer<QTextEdit>'
Have you forgotten to include the appropriate header? That is:
#include <QPointer>
Now i can set value to the text edit^ And in qDebug I see that it set, but in control i see old value... How i can redraw that?
Issue an update request to the graphics view:
ui->graphicsView->update();
Although, I believe it the change of data should be reflected automatically.