Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

User Input to QtexEdit to extract Matrix elements



  • 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


  • Moderators

    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]

    }
    @



  • 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


  • Moderators

    [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.



  • 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.



  • 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...


  • Moderators

    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);
    @



  • 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


  • Lifetime Qt Champion

    Hi,

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



  • SGalst

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

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

    thanks bro


  • Moderators

    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);
    @



  • 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


Log in to reply