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 combined with QStackedWidget, crash

QLineEdit combined with QStackedWidget, crash

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 5 Posters 1.2k 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.
  • WenchiW Wenchi

    @jsulm Here I post the whole code. As you can see I didn't change the lineEdit's text, I still don't understand why there're so many lineEdit_textChanged calls. I can only tell that changing the stackedWidget's current index is the reason which causes so many lineEdit_textChanged calls.

    #include "mainwindow.h"
    #include "./ui_mainwindow.h"
    
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)
      , ui(new Ui::MainWindow)
    {
      ui->setupUi(this);
      ui->stackedWidget->setCurrentIndex(0);
      connect(ui->lineEdit,&QLineEdit::textChanged,this,[this](const QString &tex){
      lineEdit_textChanged(tex);
      });
    }
    
    MainWindow::~MainWindow()
    {
      delete ui;
    }
    
    
    void MainWindow::on_pushButton_clicked()
    {
        qDebug()<< ui->lineEdit->text();
        //ui->lineEdit->setText(ui->lineEdit->text());
    }
    
    
    void MainWindow::lineEdit_textChanged(const QString &arg1)
    {
        qDebug()<<"on_lineEdit_textChanged"<<arg1;
        //ui->lineEdit->blockSignals(true);
        ui->stackedWidget->setCurrentIndex(1);
        //ui->lineEdit->blockSignals(false);
    }
    
    
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #8

    @Wenchi
    You don't say, but I assume that ui->lineEdit is on stacked widget #0 and there is a another, different page at stacked widget #1?

    You might get some indication what is going on if you place a breakpoint at the start of lineEdit_textChanged() and look at the stack trace as it keeps getting hit. You should also look at the stack trace when it "crashes".

    WenchiW 1 Reply Last reply
    0
    • JonBJ JonB

      @Wenchi
      You don't say, but I assume that ui->lineEdit is on stacked widget #0 and there is a another, different page at stacked widget #1?

      You might get some indication what is going on if you place a breakpoint at the start of lineEdit_textChanged() and look at the stack trace as it keeps getting hit. You should also look at the stack trace when it "crashes".

      WenchiW Offline
      WenchiW Offline
      Wenchi
      wrote on last edited by
      #9

      @JonB the three controls is independent with each other
      a8e523e7-bbb0-47f5-9bac-6ba0389143cb-企业微信截图_16659984944558.png

      JonBJ 1 Reply Last reply
      0
      • WenchiW Wenchi

        @JonB the three controls is independent with each other
        a8e523e7-bbb0-47f5-9bac-6ba0389143cb-企业微信截图_16659984944558.png

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #10

        @Wenchi
        So the lineEdit is not on either of the stacked widget pages --- how would we ever have known this? The stacked widget just has two blank QWidgets in it.

        OK. But what about using the debugger as everyone is saying to see what is going on?

        Meanwhile: do you just type in test once? Because everything looks like you are appending test to the line edit on textChanged() signal somewhere? Use that debugger to see....

        WenchiW 1 Reply Last reply
        0
        • JonBJ JonB

          @Wenchi
          So the lineEdit is not on either of the stacked widget pages --- how would we ever have known this? The stacked widget just has two blank QWidgets in it.

          OK. But what about using the debugger as everyone is saying to see what is going on?

          Meanwhile: do you just type in test once? Because everything looks like you are appending test to the line edit on textChanged() signal somewhere? Use that debugger to see....

          WenchiW Offline
          WenchiW Offline
          Wenchi
          wrote on last edited by Wenchi
          #11

          @JonB
          I insert a break point, and have clicked "run" for 3 times.
          7addc7b3-57d0-4853-b761-648c57181c4a-企业微信截图_16659999189789.png
          Then I canceled the breakpoint and run. The stack overflowed and it crashed.
          By the way, just intput sth by Chinese input method, and don't press "Enter" to make it finish.

          JonBJ 1 Reply Last reply
          0
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #12

            Wow, surprisingly I can reproduce that even in debug mode. This does seem to have some bug caused by repeatly gaining and losing focus.
            Only need to type one "test" then output endless

            [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "test"
            [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "testtest"
            [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "testtesttest"
            [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "testtesttesttest"
            [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "testtesttesttesttest"
            [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "testtesttesttesttesttest"
            [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "testtesttesttesttesttesttest"
            

            But I haven't got time to debug it through yet.

            WenchiW 1 Reply Last reply
            0
            • B Bonnie

              Wow, surprisingly I can reproduce that even in debug mode. This does seem to have some bug caused by repeatly gaining and losing focus.
              Only need to type one "test" then output endless

              [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "test"
              [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "testtest"
              [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "testtesttest"
              [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "testtesttesttest"
              [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "testtesttesttesttest"
              [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "testtesttesttesttesttest"
              [debug]MainWindow::on_lineEdit_textChanged(126):on_lineEdit_textChanged "testtesttesttesttesttesttest"
              

              But I haven't got time to debug it through yet.

              WenchiW Offline
              WenchiW Offline
              Wenchi
              wrote on last edited by
              #13

              @Bonnie I just tried it out yet in debug mode : )

              1 Reply Last reply
              0
              • WenchiW Wenchi

                @JonB
                I insert a break point, and have clicked "run" for 3 times.
                7addc7b3-57d0-4853-b761-648c57181c4a-企业微信截图_16659999189789.png
                Then I canceled the breakpoint and run. The stack overflowed and it crashed.
                By the way, just intput sth by Chinese input method, and don't press "Enter" to make it finish.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #14

                @Wenchi
                If @Bonnie is on it (a) I would guess he is not using "Chinese input method", so that's not it, and (b) he will track it down!

                But just so I understand (because I do not). You write "under the Chinese input method ", but I don't know what that is/how it works. Do I take it that it shows you that "bar" of possible inputs (numbered 1 to 7), you click on 1 test and that inserts test into the line edit, just once? Since I assume @Bonnie has reproduced without that it does not seem relevant, but I would like to understand.

                If you look carefully at your stack trace you can see that lineEdit_textChanged() is indeed calling itself recursively, as suspected, hence the eventual "crash".

                Meanwhile, I would guess --- bug or not --- that there is something "wrong" with "changing focus while you are inside the textChanged() signal handler", something assumes the focus will not change from the QLineEdit it is currently handling. And that somehow makes it keep "re-seeing" the same original test input and adding it. If this is so, you may have to do the ui->stackedWidget->setCurrentIndex(1); call from a QTimer::singleShot() you set off in your slot....

                WenchiW B 2 Replies Last reply
                0
                • JonBJ JonB

                  @Wenchi
                  If @Bonnie is on it (a) I would guess he is not using "Chinese input method", so that's not it, and (b) he will track it down!

                  But just so I understand (because I do not). You write "under the Chinese input method ", but I don't know what that is/how it works. Do I take it that it shows you that "bar" of possible inputs (numbered 1 to 7), you click on 1 test and that inserts test into the line edit, just once? Since I assume @Bonnie has reproduced without that it does not seem relevant, but I would like to understand.

                  If you look carefully at your stack trace you can see that lineEdit_textChanged() is indeed calling itself recursively, as suspected, hence the eventual "crash".

                  Meanwhile, I would guess --- bug or not --- that there is something "wrong" with "changing focus while you are inside the textChanged() signal handler", something assumes the focus will not change from the QLineEdit it is currently handling. And that somehow makes it keep "re-seeing" the same original test input and adding it. If this is so, you may have to do the ui->stackedWidget->setCurrentIndex(1); call from a QTimer::singleShot() you set off in your slot....

                  WenchiW Offline
                  WenchiW Offline
                  Wenchi
                  wrote on last edited by
                  #15

                  @JonB
                  First allow me to explain the "Chinese input method":
                  When I input "test":
                  68678bb1-869c-400d-a029-84dffe45e3e9-image.png
                  Then I can click "1"(or "2" or "3"...) or press "Enter" and the input is finished and this status won't cause crash:
                  11ce04b2-e558-41bd-896d-35a9ea96564a-image.png
                  If I click somewhere else, the input is not finished and this status can cause crash:
                  aed5348d-526b-49f6-89be-464bb42eae1f-image.png
                  To fix this problem, I add the code "blockSignals":

                  void MainWindow::lineEdit_textChanged(const QString &arg1)
                  {
                      qDebug()<<"on_lineEdit_textChanged"<<arg1;
                      ui->lineEdit->blockSignals(true);
                      ui->stackedWidget->setCurrentIndex(1);
                      ui->lineEdit->blockSignals(false);
                  }
                  
                  JonBJ 1 Reply Last reply
                  0
                  • WenchiW Wenchi

                    @JonB
                    First allow me to explain the "Chinese input method":
                    When I input "test":
                    68678bb1-869c-400d-a029-84dffe45e3e9-image.png
                    Then I can click "1"(or "2" or "3"...) or press "Enter" and the input is finished and this status won't cause crash:
                    11ce04b2-e558-41bd-896d-35a9ea96564a-image.png
                    If I click somewhere else, the input is not finished and this status can cause crash:
                    aed5348d-526b-49f6-89be-464bb42eae1f-image.png
                    To fix this problem, I add the code "blockSignals":

                    void MainWindow::lineEdit_textChanged(const QString &arg1)
                    {
                        qDebug()<<"on_lineEdit_textChanged"<<arg1;
                        ui->lineEdit->blockSignals(true);
                        ui->stackedWidget->setCurrentIndex(1);
                        ui->lineEdit->blockSignals(false);
                    }
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #16

                    @Wenchi
                    OK, now I know (a bit) about how "Chinese input" works! But I suspect this itself is not the issue, assuming @Bonnie is reproducing the issue.

                    I am not a great fan of blockSignals(), one is never sure what the side-effects ignoring signals might be. Given that I suggested that QLineEdit might not having like focus changed while inside textChanged() signal, what about the QTimer alternative I suggested instead of blocking signals:

                    QTimer::singleShot(0, [this]() { ui->stackedWidget->setCurrentIndex(1); });
                    
                    WenchiW 1 Reply Last reply
                    2
                    • JonBJ JonB

                      @Wenchi
                      OK, now I know (a bit) about how "Chinese input" works! But I suspect this itself is not the issue, assuming @Bonnie is reproducing the issue.

                      I am not a great fan of blockSignals(), one is never sure what the side-effects ignoring signals might be. Given that I suggested that QLineEdit might not having like focus changed while inside textChanged() signal, what about the QTimer alternative I suggested instead of blocking signals:

                      QTimer::singleShot(0, [this]() { ui->stackedWidget->setCurrentIndex(1); });
                      
                      WenchiW Offline
                      WenchiW Offline
                      Wenchi
                      wrote on last edited by
                      #17

                      @JonB QTimer::singleShot works! It's more convenient than blockSignals()! Adore you!

                      JonBJ 1 Reply Last reply
                      0
                      • WenchiW Wenchi

                        @JonB QTimer::singleShot works! It's more convenient than blockSignals()! Adore you!

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #18

                        @Wenchi Thank you, but I am not necessarily very adorable ;-)

                        1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Wenchi
                          If @Bonnie is on it (a) I would guess he is not using "Chinese input method", so that's not it, and (b) he will track it down!

                          But just so I understand (because I do not). You write "under the Chinese input method ", but I don't know what that is/how it works. Do I take it that it shows you that "bar" of possible inputs (numbered 1 to 7), you click on 1 test and that inserts test into the line edit, just once? Since I assume @Bonnie has reproduced without that it does not seem relevant, but I would like to understand.

                          If you look carefully at your stack trace you can see that lineEdit_textChanged() is indeed calling itself recursively, as suspected, hence the eventual "crash".

                          Meanwhile, I would guess --- bug or not --- that there is something "wrong" with "changing focus while you are inside the textChanged() signal handler", something assumes the focus will not change from the QLineEdit it is currently handling. And that somehow makes it keep "re-seeing" the same original test input and adding it. If this is so, you may have to do the ui->stackedWidget->setCurrentIndex(1); call from a QTimer::singleShot() you set off in your slot....

                          B Offline
                          B Offline
                          Bonnie
                          wrote on last edited by Bonnie
                          #19

                          @JonB said in QLineEdit combined with QStackedWidget, crash:

                          If @Bonnie is on it (a) I would guess he is not using "Chinese input method", so that's not it, and (b) he will track it down!

                          No, I'm using it actually :)
                          I've done debugging.
                          Because QStackedWidget::setCurrentIndex calls QWidget::clearFocus which makes the window to emit a focusObjectChanged signal (no matter it actually had the focus or not).
                          And during the application handling that, it finds the input method composing, so it send a QInputMethodEvent by sendEvent to commit the string and end the composition after that.
                          But while the line edit handling the event, it emits textChanged again so that never ends.
                          Not sure whether that is a bug or not...Maybe we are just not supposed to change focus within textChanged()

                          JonBJ 1 Reply Last reply
                          3
                          • B Bonnie

                            @JonB said in QLineEdit combined with QStackedWidget, crash:

                            If @Bonnie is on it (a) I would guess he is not using "Chinese input method", so that's not it, and (b) he will track it down!

                            No, I'm using it actually :)
                            I've done debugging.
                            Because QStackedWidget::setCurrentIndex calls QWidget::clearFocus which makes the window to emit a focusObjectChanged signal (no matter it actually had the focus or not).
                            And during the application handling that, it finds the input method composing, so it send a QInputMethodEvent by sendEvent to commit the string and end the composition after that.
                            But while the line edit handling the event, it emits textChanged again so that never ends.
                            Not sure whether that is a bug or not...Maybe we are just not supposed to change focus within textChanged()

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #20

                            @Bonnie said in QLineEdit combined with QStackedWidget, crash:

                            No, I'm using it actually :)

                            Oh! @Bonnie does not sound like a very Chinese name :)

                            Maybe we are just not supposed to change focus within textChanged()

                            That is evidently the case. For whatever reason, it appears the internals expect the line edit to have the focus while dealing with textChanged(), and changing it causes the signal to be re-emitted.

                            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