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. Getting QStringList text to line edit based on a string inserted on another line edit
Forum Updated to NodeBB v4.3 + New Features

Getting QStringList text to line edit based on a string inserted on another line edit

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatorqstringlistline editindexof
15 Posts 3 Posters 5.1k 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.
  • M mrjj
    18 Dec 2017, 16:37

    Hi
    Hook up the QLine(1) textChanged() to a slot and in that slot
    call getNameIndex()

    L Offline
    L Offline
    Lasith
    wrote on 18 Dec 2017, 16:44 last edited by
    #3

    @mrjj
    void MainWindow::on_name_textChanged(const QString &arg1)
    {
    getNameIndex();
    }

    I did this now but still no change :(

    M 1 Reply Last reply 18 Dec 2017, 16:52
    0
    • L Lasith
      18 Dec 2017, 16:44

      @mrjj
      void MainWindow::on_name_textChanged(const QString &arg1)
      {
      getNameIndex();
      }

      I did this now but still no change :(

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 18 Dec 2017, 16:52 last edited by mrjj
      #4

      @Lasith
      did you use connect and checked the return code ?
      ahh, its via designer
      Set break point and see what happens.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Lasith
        wrote on 18 Dec 2017, 16:56 last edited by
        #5

        How to use connect and check return code?

        T M 3 Replies Last reply 18 Dec 2017, 16:59
        0
        • L Lasith
          18 Dec 2017, 16:56

          How to use connect and check return code?

          T Offline
          T Offline
          Taz742
          wrote on 18 Dec 2017, 16:59 last edited by
          #6
          This post is deleted!
          1 Reply Last reply
          0
          • L Lasith
            18 Dec 2017, 16:56

            How to use connect and check return code?

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 18 Dec 2017, 16:59 last edited by mrjj
            #7

            @Lasith
            Forget about that. You use Designer so its pr auto.

            Your error is that you dont set name to compare with from LineEdit1
            sorry. you do.

            L 1 Reply Last reply 18 Dec 2017, 17:01
            0
            • L Lasith
              18 Dec 2017, 16:56

              How to use connect and check return code?

              T Offline
              T Offline
              Taz742
              wrote on 18 Dec 2017, 16:59 last edited by
              #8

              @Lasith

              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              
              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
              
                  QObject::connect(ui->lnName, &QLineEdit::textChanged, this, [=](QString arg){
                      ui->lnIndex->setText(QString::number(this->getIndex(arg)));
                  });
              }
              
              MainWindow::~MainWindow()
              {
                  delete ui;
              }
              
              int MainWindow::getIndex(QString arg) {
                  QStringList names;
                  names << "John" << "Smith" << "Mary" <<"Anne";
              
                  for (int i = 0; i < names.size(); i++) {
                      if (names[i] == arg) {
                          return i;
                      }
                  }
                  return -1;
              }
              
              

              .h

              #ifndef MAINWINDOW_H
              #define MAINWINDOW_H
              
              #include <QMainWindow>
              
              namespace Ui {
              class MainWindow;
              }
              
              class MainWindow : public QMainWindow
              {
                  Q_OBJECT
              
              public:
                  explicit MainWindow(QWidget *parent = 0);
                  ~MainWindow();
              
              private:
                  Ui::MainWindow *ui;
                  int getIndex(QString arg);
              };
              
              #endif // MAINWINDOW_H
              
              
              1 Reply Last reply
              2
              • M mrjj
                18 Dec 2017, 16:59

                @Lasith
                Forget about that. You use Designer so its pr auto.

                Your error is that you dont set name to compare with from LineEdit1
                sorry. you do.

                L Offline
                L Offline
                Lasith
                wrote on 18 Dec 2017, 17:01 last edited by
                #9

                @mrjj No I already set the Name :(

                M 1 Reply Last reply 18 Dec 2017, 17:05
                0
                • L Lasith
                  18 Dec 2017, 17:01

                  @mrjj No I already set the Name :(

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 18 Dec 2017, 17:05 last edited by
                  #10

                  @Lasith
                  Yeah, sorry saw that after.
                  Use @Taz742 solution (which uses lambda and is more smooth)
                  or try this test project (that does it with your code)
                  https://www.dropbox.com/s/pozxsv4sd1sa64p/editindex.zip?dl=0

                  L 1 Reply Last reply 18 Dec 2017, 17:12
                  1
                  • M mrjj
                    18 Dec 2017, 17:05

                    @Lasith
                    Yeah, sorry saw that after.
                    Use @Taz742 solution (which uses lambda and is more smooth)
                    or try this test project (that does it with your code)
                    https://www.dropbox.com/s/pozxsv4sd1sa64p/editindex.zip?dl=0

                    L Offline
                    L Offline
                    Lasith
                    wrote on 18 Dec 2017, 17:12 last edited by
                    #11

                    @mrjj Thanx but what was the problem in my code?

                    M T 2 Replies Last reply 18 Dec 2017, 17:16
                    0
                    • L Lasith
                      18 Dec 2017, 17:12

                      @mrjj Thanx but what was the problem in my code?

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 18 Dec 2017, 17:16 last edited by mrjj
                      #12

                      @Lasith
                      Only thing wrong was you assigned an int to a QString

                      index=i; // this wont work

                      have to be
                      index=QString::number(i);

                      I also added it would say "not found" just for testing.

                      L 1 Reply Last reply 18 Dec 2017, 17:22
                      1
                      • M mrjj
                        18 Dec 2017, 17:16

                        @Lasith
                        Only thing wrong was you assigned an int to a QString

                        index=i; // this wont work

                        have to be
                        index=QString::number(i);

                        I also added it would say "not found" just for testing.

                        L Offline
                        L Offline
                        Lasith
                        wrote on 18 Dec 2017, 17:22 last edited by
                        #13

                        @mrjj Oh a silly mistake :(

                        M 1 Reply Last reply 18 Dec 2017, 17:24
                        0
                        • L Lasith
                          18 Dec 2017, 17:22

                          @mrjj Oh a silly mistake :(

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 18 Dec 2017, 17:24 last edited by
                          #14

                          @Lasith
                          Well its not that silly as i do it sometimes also - as my old string class would
                          make the int a string but QString think something else of it.

                          1 Reply Last reply
                          0
                          • L Lasith
                            18 Dec 2017, 17:12

                            @mrjj Thanx but what was the problem in my code?

                            T Offline
                            T Offline
                            Taz742
                            wrote on 18 Dec 2017, 17:28 last edited by Taz742
                            #15

                            @Lasith
                            Your problems:

                            • You only once call the returning function of the index, and in the designer. (In fact, when ui->name text changes, the function should be called every time.)

                            @Lasith said in Getting QStringList text to line edit based on a string inserted on another line edit:

                            QString index;
                            Name=ui->name->text();
                            for(int i=0;i<names.size();i++)
                            {
                            if(names[i]==Name){
                            index=i;
                            }

                            You want the string but in your cycle the 'i' of the integer. Why do not you pay attention to this(index = i)? Try compile the app? If yes, the compiler would inform you about it.

                            From your profile it seems that you are using Qt too several months. But it does not seem to be at all.

                            Do not try to conquer the peak, learn to walk first.

                            1 Reply Last reply
                            1

                            12/15

                            18 Dec 2017, 17:16

                            • Login

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