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. User Input to QtexEdit to extract Matrix elements
QtWS25 Last Chance

User Input to QtexEdit to extract Matrix elements

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 4.8k 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.
  • W Offline
    W Offline
    Warrior4just
    wrote on 21 Jan 2014, 00:35 last edited by
    #1

    Hello

    I am learning QT library ....I'd appreciate it if you could help me with this.
    Please do not assume i know a lot...i have C++ experience up to Polymorphism (college CS II level).
    I would like to use QT GUI to create a window that takes user string input from TextEdit boxes (for matrix input)

    What i'm trying to achieve is, reading matrix element (like Matlab: rows separated by space, and columns are separated by newline) ....

    I'll show the part that when clicked , it scans the TextEdit... this is the part i need help with

    void MainWindow::on_pushButton_4_clicked()
    {
    vector<double> Val1;
    QStringList textEdit5=ui->textEdit_5->toPlainText().split(" "); //to read list of string separated by space

     Val1=textEdit5.toVector();      // convert to vector  , i'm not sure this is correct
    
     QVector<double> qvec = QVector<double>::fromStdVector(Val1);    // convert to Qvector to manipulate 
    
     ui->textEdit_7->setPlainText();
    

    }

    Thanks a lot

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 21 Jan 2014, 08:00 last edited by
      #2

      something like that?
      @
      void MainWindow::on_pushButton_4_clicked()
      {
      QList< QList<double> > matrix;

      foreach( QString row, ui->textEdit_5->toPlainText().split(ā€\nā€œ) )
      {
      QList<double> cols;
      foreach( QString val, row.split(" ") )
      {
      bool check = false;
      double colValue = val.toDouble(&check);
      if( check )
      cols << colValue;
      }

           if( ! cols.isEmpty() )       //optional for your needs?
                matrix << cols;
      

      }

      //... do whatever you want with "matrix": e.g. matrix[0][1]

      }
      @

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • W Offline
        W Offline
        Warrior4just
        wrote on 22 Jan 2014, 16:58 last edited by
        #3

        Thanks a lot Raven-woks...that was helpful

        what does the bool do inside the .todouble() method, interesting...i suppose i understand it, but i didn't think .todouble() method could take a reference bool argument.

        My issue now, is the fact that i don' t know how to retrieve the data from the 2D Qlist to display them to QtextEdit.

        i just would like to know how to print a 2D Qlist to QtextEdit which takes only strings

        Thanks a lot for your help

        1 Reply Last reply
        0
        • R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 22 Jan 2014, 17:07 last edited by
          #4

          [quote author="Warrior4just" date="1390409926"]
          what does the bool do inside the .todouble() method, interesting...i suppose i understand it, but i didn't think .todouble() method could take a reference bool argument.
          [/quote]
          In case the string can not be converted to a numeric value the bool is set to false.
          The QString::toDouble() doesn't take a reference bool variable, but an optional pointer variable.

          [quote author="Warrior4just" date="1390409926"]
          My issue now, is the fact that i don' t know how to retrieve the data from the 2D Qlist to display them to QtextEdit.

          i just would like to know how to print a 2D Qlist to QtextEdit which takes only strings
          [/quote]
          Build your string using QString::number() with the values from the list. Also insert "\n" wherever you need it.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • D Offline
            D Offline
            daemon777
            wrote on 22 Jan 2014, 17:12 last edited by
            #5

            For building longer Strings the arg-Method from QString can be quite usefull.
            Something like QString output = QString("%1 \n%2").arg("String 1",someint) is a very fast way to build such a string.

            1 Reply Last reply
            0
            • W Offline
              W Offline
              Warrior4just
              wrote on 22 Jan 2014, 19:14 last edited by
              #6

              Thank you Raven-workx and Deamon777
              Great...
              But if i wanna use QString::number() , i need to build two loops for each index of the list
              How does one select the indices of 2D List matrix (e.g. rows and colums), i know im' probably thinking in terms of scientific programming, but u seem where i'm going , i(m using the Eigen librabry afterwards
              say
              @
              for (int i , i < matrix.row(), i++)
              {
              for (int j , j< matrix.cols(),j++)
              QString qsval= matrix[j][i].QString::number();
              ui->textEdit_7->setPlainText(qsval);
              }
              }
              @

              Thank you for clarifying...

              1 Reply Last reply
              0
              • R Offline
                R Offline
                raven-worx
                Moderators
                wrote on 22 Jan 2014, 19:27 last edited by
                #7

                Beside you are missing some error checks:
                @
                QString text;
                for (int i , i < matrix.row(), i++)
                {
                for (int j , j< matrix.cols(),j++)
                {
                text += QString::number(matrix[j][i]);
                }
                text += "\n";
                }
                ui->textEdit_7->setPlainText(text);
                @

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  Warrior4just
                  wrote on 22 Jan 2014, 19:46 last edited by
                  #8

                  Raven-workx

                  i don't think what i wrote is correct (assuming i fixed the syntax errors), because, matrix is a QList<Qlist<double>> ...so writing matrix.rows() makes no sense..
                  How do i access the length of matrix : numbers of rows
                  How do i access the width of matrix: number of columns

                  Thank you

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 22 Jan 2014, 20:20 last edited by
                    #9

                    Hi,

                    "QList::count()":http://qt-project.org/doc/qt-5/qlist.html#count-2 to get the number of item

                    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
                    • W Offline
                      W Offline
                      Warrior4just
                      wrote on 23 Jan 2014, 00:33 last edited by
                      #10

                      SGalst

                      but @Qlist@ is 2D list....

                      count would be for rows or cols...and what's for the other

                      thanks bro

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        raven-worx
                        Moderators
                        wrote on 23 Jan 2014, 07:19 last edited by
                        #11

                        yes calling count() on the outermost list is for rows. Calling count() on the QList of an row returns the column count. of this row.
                        Actually it's very similar to a multi-dimensional array.

                        You could also avoid the indices at all:
                        @
                        QString text;
                        foreach( QList<double> row, matrix ) //rows
                        {
                        foreach( double colValue, row ) //cols
                        {
                        text += QString::number(colValue);
                        }
                        text += "\n";
                        }
                        ui->textEdit_7->setPlainText(text);
                        @

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        1 Reply Last reply
                        0
                        • W Offline
                          W Offline
                          Warrior4just
                          wrote on 27 Jan 2014, 03:09 last edited by
                          #12

                          Thanks a lot raven-worx for your help, worked well

                          there is one little problem , is that i am unable to display Matrix elements of Eigen's Matrix class of the Eigen library for linear algebra , here is what i did when i wanted to solve an example of a linear system.

                          i can't show the numbers in text edits (text edit 5 and 6)

                          @void MainWindow::on_pushButton_5_clicked()
                          {
                          MatrixXd A = MatrixXd::Random(3,3);
                          VectorXd b = VectorXd::Random(3,1);

                          QString text1;
                          QString text2;
                          
                          for (int i; i<=A.rows();i++)
                          {
                              for (int j;j<=A.cols();j++)
                              {
                                  text1 += QString::number(A(i,j))+ " ";
                          
                              }
                          
                              text1 += "\n";
                              text2 +=  QString::number(b(i))+ "\n";
                          }
                          
                           ui->textEdit_5->setPlainText(text1);
                           ui->textEdit_6->setPlainText(text2);
                          
                           //    Vector3f x = A.colPivHouseholderQr().solve(b);
                          

                          }@

                          I appreciate it

                          1 Reply Last reply
                          0

                          3/12

                          22 Jan 2014, 16:58

                          topic:navigator.unread, 9
                          • Login

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