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 Updated to NodeBB v4.3 + New Features

Are Widgets like LineEdit affected by scope ?

Scheduled Pinned Locked Moved Solved General and Desktop
line editscope
11 Posts 5 Posters 3.7k Views 3 Watching
  • 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.
  • B Offline
    B Offline
    bsomervi
    wrote on 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
    1
    • B bsomervi

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

      M Offline
      M Offline
      mrmorph
      wrote on 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

      kshegunovK 1 Reply Last reply
      2
      • M mrmorph

        @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

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on 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 last edited by mrmorph
          #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
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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?

            kshegunovK M 2 Replies Last reply
            2
            • mrjjM mrjj

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

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #7

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

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              2
              • mrjjM mrjj

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

                mrjjM 1 Reply Last reply
                0
                • M mrmorph

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • mrjjM mrjj

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

                      • Login

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