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. using the slot setText(const QString &text)
Forum Updated to NodeBB v4.3 + New Features

using the slot setText(const QString &text)

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.2k 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.
  • Z Offline
    Z Offline
    Zunneh
    wrote on last edited by
    #1

    Hello guys ,
    i want to use the slot setText(const QString &text)

             QObject::connect(ui->treeWidget,SIGNAL(itemSelectionChanged()),ui->textEdit,SLOT(setText(const QString &text)));
    

    where can i put the text to display in my QTextEdit ? Thanks guys

    my english is average, please use simple words and try to be the most explicit, thank you

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      just create a slot or a lambda that decides what text to write:

      QObject::connect(ui->treeWidget->selectionModel(),&QItemSelectionModel::SelectionChanged,ui->textEdit,[this](const QItemSelection &selected)->void{
      const auto selectedIndexes = selected.indexes();
      QString newText;
      for(auto&& idx : selectedIndexes)
      newText += idx.data().toString() + QLatin1Char(';');
      ui->textEdit.setText(newText);
      });
      
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      Z 2 Replies Last reply
      3
      • VRoninV VRonin

        just create a slot or a lambda that decides what text to write:

        QObject::connect(ui->treeWidget->selectionModel(),&QItemSelectionModel::SelectionChanged,ui->textEdit,[this](const QItemSelection &selected)->void{
        const auto selectedIndexes = selected.indexes();
        QString newText;
        for(auto&& idx : selectedIndexes)
        newText += idx.data().toString() + QLatin1Char(';');
        ui->textEdit.setText(newText);
        });
        
        
        Z Offline
        Z Offline
        Zunneh
        wrote on last edited by
        #3

        @VRonin i didn't understand the code , are there a solution more easy ? i past your code , i have error (expected expression)

        my english is average, please use simple words and try to be the most explicit, thank you

        JonBJ 1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          are there a solution more easy ?

          create a private slots: in your class called updateText() then use QObject::connect(ui->treeWidget,SIGNAL(itemSelectionChanged()),this,SLOT(updateText()));
          Inside updateText then you can call ui->textEdit->setText() with whaterver argument you like

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          Z 1 Reply Last reply
          4
          • VRoninV VRonin

            are there a solution more easy ?

            create a private slots: in your class called updateText() then use QObject::connect(ui->treeWidget,SIGNAL(itemSelectionChanged()),this,SLOT(updateText()));
            Inside updateText then you can call ui->textEdit->setText() with whaterver argument you like

            Z Offline
            Z Offline
            Zunneh
            wrote on last edited by
            #5

            @VRonin i want to keep the 3rd parameter of connect ui->textEdit , i tried this the last idea you mention and it work , now i want to try with keeping ui->textEdit

            my english is average, please use simple words and try to be the most explicit, thank you

            VRoninV J.HilkJ 2 Replies Last reply
            0
            • Z Zunneh

              @VRonin i didn't understand the code , are there a solution more easy ? i past your code , i have error (expected expression)

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

              @Zunneh
              @VRonin's original code uses a C++ lambda --- effectively, a function defined in-line --- as the slot in the call to QObject::connect(). Lambda syntax can be a bit intimidating...!

              As per his second suggestion, you can instead simply write your own slot function and call that, which has a syntax you are perhaps more used to.

              1 Reply Last reply
              2
              • Z Zunneh

                @VRonin i want to keep the 3rd parameter of connect ui->textEdit , i tried this the last idea you mention and it work , now i want to try with keeping ui->textEdit

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                @Zunneh said in using the slot setText(const QString &text):

                i want to keep the 3rd parameter of connect ui->textEdit

                I honestly have no idea why you would want that.

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                Z 1 Reply Last reply
                0
                • Z Zunneh

                  @VRonin i want to keep the 3rd parameter of connect ui->textEdit , i tried this the last idea you mention and it work , now i want to try with keeping ui->textEdit

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #8

                  @Zunneh said in using the slot setText(const QString &text):

                  @VRonin i want to keep the 3rd parameter of connect ui->textEdit , i tried this the last idea you mention and it work , now i want to try with keeping ui->textEdit

                  than either use @VRonin first example or define yourself a signal that has a QString as argument and connect that to your textEdit updateText setText slot


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  1
                  • VRoninV VRonin

                    @Zunneh said in using the slot setText(const QString &text):

                    i want to keep the 3rd parameter of connect ui->textEdit

                    I honestly have no idea why you would want that.

                    Z Offline
                    Z Offline
                    Zunneh
                    wrote on last edited by
                    #9

                    @VRonin because i my big project i tried with this , it didn't work ,i tried it in sample project it work (i don't konw why ) i have to use the name of my object to do it , meanwhile i don't know how to create signal , i have to detect if itemSelectionChanged() of my QtreeWidget then display text , the only way is setText(const QString &text) but i don't know how to manipulate it , i have to create a signal to send the text ?

                    my english is average, please use simple words and try to be the most explicit, thank you

                    1 Reply Last reply
                    0
                    • VRoninV VRonin

                      just create a slot or a lambda that decides what text to write:

                      QObject::connect(ui->treeWidget->selectionModel(),&QItemSelectionModel::SelectionChanged,ui->textEdit,[this](const QItemSelection &selected)->void{
                      const auto selectedIndexes = selected.indexes();
                      QString newText;
                      for(auto&& idx : selectedIndexes)
                      newText += idx.data().toString() + QLatin1Char(';');
                      ui->textEdit.setText(newText);
                      });
                      
                      
                      Z Offline
                      Z Offline
                      Zunneh
                      wrote on last edited by
                      #10

                      @VRonin your idea works finaly , thank you guys , i search in internet and i added

                           CONFIG += c++11
                      

                      to use lambda

                       QObject::connect(ui->treeWidget- >selectionModel(),&QItemSelectionModel::SelectionChanged,ui->textEdit,[this](const QItemSelection &selected)->void{
                        const auto selectedIndexes = selected.indexes();
                       QString newText;
                       for(auto&& idx : selectedIndexes)
                       newText += idx.data().toString() + QLatin1Char(';');
                       ui->textEdit->setText(newText);
                        });
                      

                      i modified your code to make it more lisible

                         QObject::connect(ui->treeWidget- >selectionModel(),&QItemSelectionModel::selectionChanged,ui->textEdit,[this]()->void{
                      
                      QString test;
                      test=ui->treeWidget->currentItem()->text(ui->treeWidget->currentColumn());
                      
                      ui->textEdit->setText(test);
                      });
                      

                      my english is average, please use simple words and try to be the most explicit, thank you

                      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