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 ?



  • You may be calling QLineEdit::setText() before the widget is constructed or after it has been destroyed.



  • @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


  • Qt Champions 2016

    @mrmorph
    Looks correct, do you mind sharing a bit more of the code?



  • 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
    

  • Qt Champions 2016

    @mrmorph said:

     QLineEdit *leDimWidth = new QLineEdit();
    

    and you also have one in

    private:
        QLineEdit       *leDimWidth;
    

    so should the line
    QLineEdit *leDimWidth = new QLineEdit();
    not be

    leDimWidth = new QLineEdit();
    

    as else you new a local one and the one in "private" is just a dangling pointer?


  • Qt Champions 2016

    @mrjj
    Good eye! I think you found it. :)



  • @mrjj Brilliant, thanks a lot, that was it :)

    I'd spent ages on this so many thanks.

    Great support here.

    If you don't mind I will remove most of my code as it's a commercial product.


  • Qt Champions 2016

    @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;
    


  • @mrjj Well you are good at this, you've saved me again :)

    I snipped up my code, but hopefully left enough to show where I went wrong for others.

    Thanks again.

    Rog.


  • Lifetime Qt Champion

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.