Are Widgets like LineEdit affected by scope ?
-
I'm still learning. I'm writing a plugin for Daz Studio.
I found when the form initially loads in my main function that something like :
QLineEdit *leDimWidth = new QLineEdit(); leDimWidth->setText("300");
Will work, but when I use set text in a separate function I get an 'ACCESS_VIOLATION' crash.
leDimWidth is set up in my header like this :
private: QLineEdit *leDimWidth;
The function calling it is in a public slot.
Not quite sure what I might be doing wrong.
Anyone have any ideas please ?
-
@bsomervi Nope I don't think so - the application is fully loaded and the layout displayed on screen.
To test it I set the text when I press a button on the layout.
Then it goes to a separate function to set the text, then crashes.
Can you give me one rep so I don't keep getting this 500 second time delay please ? I'm not a spammer :O
-
Here's the code...
main cpp :
..... SNIP.... QLineEdit *leDimWidth = new QLineEdit("11"); QPushButton *button1 = new QPushButton(tr("Button 1")); .....SNIP..... // * THIS WORKS WHEN SETTING UP THE FORM * leDimWidth->setText("300"); // Signals and Slots connect(button1, SIGNAL(pressed()), this, SLOT(doThis("Hmmm"))); ..... SNIP.... void Stacker::doThis(QString say) { // * THIS DOES NOT WORK - CAUSES CRASH * leDimWidth->setText("500"); }
Header File :
...SNIP... class Stacker : public DzPane { Q_OBJECT public: Stacker(); ~Stacker(); public slots: void doThis(QString say); private: QLineEdit *leDimWidth; }; #endif // STACKER_H
-
@mrmorph said:
QLineEdit *leDimWidth = new QLineEdit();
and you also have one in
private: QLineEdit *leDimWidth;
so should the line
QLineEdit *leDimWidth = new QLineEdit();
not beleDimWidth = new QLineEdit();
as else you new a local one and the one in "private" is just a dangling pointer?
-
@mrmorph
well I must have confess to have done it myself,
pasting from .h :)
Yeah better remove code then, so u wont get in trouble.Just a note.
We do not have to new Widgets. Most are quite happy as non pointers.private: QLineEdit leDimWidth;
-
Hi,
Just a quick note, if your widget on stack (i.e. leDimWidth) gets a parent, you'll have a double delete problem since it will get destroyed when the parent is and again when your Stacker object gets destroyed.