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. QLineEdit.setText() generating segmentation fault
Forum Updated to NodeBB v4.3 + New Features

QLineEdit.setText() generating segmentation fault

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 5.7k Views 1 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.
  • F Offline
    F Offline
    fpex
    wrote on last edited by
    #1

    I have a code which works fine without memory leaks which make a simple QDialog.
    I am trying to add some text to be displayed in the QLineEdit widgets as it gets created and it generates segmentation fault:

    @QLabel *step = new QLabel(tr("Line capture step"));
    QLineEdit *lineStep = new QLineEdit();
    lineStep->setMaximumWidth(30);
    lineStep->setAlignment(Qt::AlignCenter);
    step->setBuddy(lineStep);
    connect(lineStep, &QLineEdit::textChanged, this, &inputDialog::disableOnLetters);
    //QString test="00";
    //lineStep->setText(text);
    lineStep->setText("00"); @

    It does not matter how I enter the QString in setText, the result is always the same. If I debug the code, the issue is clearly at setText. I tried to clear the project and rebuild, even after restarting QT .... the problem stays.
    Anybody has a suggestion where to look?

    Thanks.

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Hi,
      I might not be the QLineEdit! The first think I would like to mention is the parent of the QLineEdit. Set this to the dialog you are creating, that makes cleaning up of memory a thing for Qt, instead of you as programmer.
      The second thing, you connect the 'textChanged' signal to a slot, so it might also be a failure in that slot!
      Check that part of code as well!

      Greetz, Jeroen

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qxoz
        wrote on last edited by
        #3

        Could problem be in inputDialog::disableOnLetters slot? What is happening there?

        edit: fpex@ Welcome to DevNet. :)

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fpex
          wrote on last edited by
          #4

          I doubt it as the code works fine. But just in case here is the code:

          @void inputDialog::disableOnLetters (const QString &text)
          {
          QRegExp re("\d*"); // a regular expression to capture digits
          bool enabling=acceptEnabled&&re.exactMatch(text);
          allButtons.at(1)->setEnabled(enabling);
          }@

          ...... and while typing I found my error.
          @allButtons.at(1)->setEnabled(enabling);@
          the setText is called before the allButtons vector is filled in the constructor.

          Sorry and thanks guys!

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi and welcome to devnet,

            Where do you call setText ? From the constructor or from another function ? If another function, you are probably using a member variable that you shadowed in the constructor so your lineEdit member variable is not initialized

            Hope it helps

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

            P 1 Reply Last reply
            1
            • F Offline
              F Offline
              fpex
              wrote on last edited by
              #6

              It is indeed so. I realised while writing the previous post that calling the set text was triggering a slot resulting in accessing a vector before it was initialised.

              Just for the archives, a better solution is to use textEdited() when using setText to avoid to trigger an unwanted signal/slot connection via textChanged().

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                One good way to approach that is to always do all widgets initialization first, then all the connection. Once that is setup, you should be able to call anything that might trigger a slot and be safe.

                Changing the signal also means that your disableOnLetters won't be called at all initially so your widget might not be in the state you wanted.

                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
                0
                • F Offline
                  F Offline
                  fpex
                  wrote on last edited by
                  #8

                  I agree. In my case This is not a problema as all is designed such that that check function is needed when the text is changed in a not programmatic way.

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Hi and welcome to devnet,

                    Where do you call setText ? From the constructor or from another function ? If another function, you are probably using a member variable that you shadowed in the constructor so your lineEdit member variable is not initialized

                    Hope it helps

                    P Offline
                    P Offline
                    philmacu
                    wrote on last edited by
                    #9

                    @SGaist I wish I found your post earlier today, wasted half a day chasing my tail, investigating crazy things, just couldn't find the reason for my segmentation fault, read your post ... thought to myself ... I couldn't be that dumb ... or could I???

                    And yes I was, was making a reference to the a lineEdit object before I had even initalised the ui frrl like a dope, but at least the seg fault has gone ;-)

                    Many thanks

                    1 Reply Last reply
                    0

                    • Login

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