Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Are Widgets like LineEdit affected by scope ?
Forum Update on Monday, May 27th 2025

Are Widgets like LineEdit affected by scope ?

Scheduled Pinned Locked Moved Solved General and Desktop
line editscope
11 Posts 5 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrmorph
    wrote on 5 Dec 2015, 12:25 last edited by mrmorph 12 May 2015, 12:30
    #1

    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 ?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bsomervi
      wrote on 5 Dec 2015, 12:29 last edited by
      #2

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

      M 1 Reply Last reply 5 Dec 2015, 12:33
      1
      • B bsomervi
        5 Dec 2015, 12:29

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

        M Offline
        M Offline
        mrmorph
        wrote on 5 Dec 2015, 12:33 last edited by
        #3

        @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

        K 1 Reply Last reply 5 Dec 2015, 12:35
        2
        • M mrmorph
          5 Dec 2015, 12:33

          @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

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 5 Dec 2015, 12:35 last edited by
          #4

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

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mrmorph
            wrote on 5 Dec 2015, 12:40 last edited by mrmorph 12 May 2015, 13:05
            #5

            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
            
            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 5 Dec 2015, 12:46 last edited by
              #6

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

              K M 2 Replies Last reply 5 Dec 2015, 12:48
              2
              • M mrjj
                5 Dec 2015, 12:46

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

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 5 Dec 2015, 12:48 last edited by kshegunov 12 May 2015, 12:49
                #7

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

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                2
                • M mrjj
                  5 Dec 2015, 12:46

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

                  M Offline
                  M Offline
                  mrmorph
                  wrote on 5 Dec 2015, 12:51 last edited by
                  #8

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

                  M 1 Reply Last reply 5 Dec 2015, 12:55
                  0
                  • M mrmorph
                    5 Dec 2015, 12:51

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

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 5 Dec 2015, 12:55 last edited by
                    #9

                    @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;
                    
                    M 1 Reply Last reply 5 Dec 2015, 13:02
                    0
                    • M mrjj
                      5 Dec 2015, 12:55

                      @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;
                      
                      M Offline
                      M Offline
                      mrmorph
                      wrote on 5 Dec 2015, 13:02 last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 5 Dec 2015, 21:56 last edited by
                        #11

                        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.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1

                        4/11

                        5 Dec 2015, 12:35

                        7 unread
                        • Login

                        • Login or register to search.
                        4 out of 11
                        • First post
                          4/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved