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. list of qtextedit. How to get the one that has changed?

list of qtextedit. How to get the one that has changed?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 359 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.
  • LeoCL Offline
    LeoCL Offline
    LeoC
    wrote on last edited by
    #1

    Hey,

    this one works absolutely fine:

    QTextEdit *textEdit ;
    
    void MainWindow::doStuff() {
        textEdit = new QTextEdit();
        connect(textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(texthaschanged()));
    }
    
    void MainWindow::texthaschanged() {
        QTextCursor myCursor;
        myCursor = textEdit ->textCursor();
        qDebug() << "text has changed at position" << myCursor.position();
    }
    

    But what to do when I have the following?

    QList<QTextEdit*> myList;
    

    How do I know now which cursor has changed?

    Thanks!

    JonBJ 1 Reply Last reply
    0
    • LeoCL LeoC

      Hey,

      this one works absolutely fine:

      QTextEdit *textEdit ;
      
      void MainWindow::doStuff() {
          textEdit = new QTextEdit();
          connect(textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(texthaschanged()));
      }
      
      void MainWindow::texthaschanged() {
          QTextCursor myCursor;
          myCursor = textEdit ->textCursor();
          qDebug() << "text has changed at position" << myCursor.position();
      }
      

      But what to do when I have the following?

      QList<QTextEdit*> myList;
      

      How do I know now which cursor has changed?

      Thanks!

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

      @LeoC
      You read up about C++ lambdas. :)

      void MainWindow::doStuff() {
          QTextEdit *textEdit = new QTextEdit();
          myList.append(textEdit);
          connect(textEdit, &QTextEdit::cursorPositionChanged,
                  this, [this](QTextEdit *textEdit) { texthaschanged(textEdit);  } );
      }
      
      void MainWindow::texthaschanged(QTextEdit *textEdit) {
          ...
      }
      

      This passes the textEdit emitting the signal as a parameter to the texthaschanged(QTextEdit *textEdit) slot, so it uses that to access that one in the body of the method.

      We are no longer using the single member variable QTextEdit *textEdit ; you had. You get rid of that, and use your QList<QTextEdit*> myList to store pointers to the various QTextEdits.

      You also use this opportunity to start using the new style signals & slots syntax instead of the SIGNAL/SLOT() macros. You get better edit/compile-time support.

      EDIT @KroMignon has corrected my (untested!) code below :) The connect() statement should read:

          connect(textEdit, &QTextEdit::cursorPositionChanged,
                  this, [this, textEdit]() { texthaschanged(textEdit);  } );
      
      1 Reply Last reply
      2
      • LeoCL Offline
        LeoCL Offline
        LeoC
        wrote on last edited by
        #3

        @JonB thanks for your answer.

        To be honest, I am not very familiar with lambdas. I just started reading about it and I think I got a basic understanding of it.

        Nevertheless I can't get your example working. I get the following error:

        error: static assertion failed: Signal and slot arguments are not compatible.
        

        Can you give ma a hint for this?

        KroMignonK 1 Reply Last reply
        0
        • LeoCL LeoC

          @JonB thanks for your answer.

          To be honest, I am not very familiar with lambdas. I just started reading about it and I think I got a basic understanding of it.

          Nevertheless I can't get your example working. I get the following error:

          error: static assertion failed: Signal and slot arguments are not compatible.
          

          Can you give ma a hint for this?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @LeoC said in list of qtextedit. How to get the one that has changed?:

          Can you give ma a hint for this?

          There is a little error in proposed code, QTextEdit::cursorPositionChanged() don't have arguments!
          You have to capture booth variable to use lambdas, like this:

          void MainWindow::doStuff() {
              QTextEdit *textEdit = new QTextEdit();
              myList.append(textEdit);
              connect(textEdit, &QTextEdit::cursorPositionChanged,
                      this, [this, textEdit]() { texthaschanged(textEdit);  } );
          }
          

          I could suggest you to take a look at https://blog.feabhas.com/2014/03/demystifying-c-lambdas/ to better understand lambda functions usage in C++.
          And this for combining lambdas and Qt: https://medium.com/genymobile/how-c-lambda-expressions-can-improve-your-qt-code-8cd524f4ed9f

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          3
          • LeoCL Offline
            LeoCL Offline
            LeoC
            wrote on last edited by
            #5

            @KroMignon thanks. I guess I figured it out. This one works fine:

                for(int i_Ctr = 0; i_Ctr < 3; i_Ctr++) {
                    QTextEdit *textEdit = new QTextEdit();
            
                    connect(textEdit, &QTextEdit::cursorPositionChanged, [this, textEdit] {
                        this->texthaschanged(textEdit);
                    });
                    textEdit->show();
                }
            

            In my previous example the definition of QTextEdit wasn't part of the function itself but rather part of the h-file. That was something I just didn't recognized.
            Anyway, now I can move on :) Thank you!

            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