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

Parse the arguments given by user in QLineEdit and place them in QVector<QString>



  • Hi,

    I would like to parse only once the text in QLineEdit and place each word into a QVector<QString> m_vect.

    For example, if (QLineEdit *lineEdit) lineEdit->text() gives "solve" written by the user, then m_vect.at(0) would be equal to "solve", that is m_vect.at(0) = "solve";

    If the user then adds the following text: "from -5 to 10", then the lineEdit->text() would have "solve from -5 to 10" and m_vect would be composed of:
    m_vect.at(0) = "solve";
    m_vect.at(1) = "from";
    m_vect.at(2) = "-5";
    m_vect.at(3) = "to";
    m_vect.at(4) = "10";

    I have tried with QCoreApplications::arguments() but I cannot access each argument individually.

    Also QCommandLineParser doesn't seem to accept arguments (other than the fileName argument) that do not require a hyphen before it.
    The majority of my arguments like "solve from -5 to 10" do not have a hyphen preceding them.

    Here's my code:

    manwindows.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include "functions.h"
    #include <QMainWindow>
    #include <QEvent>
    #include <QAction>
    #include <QActionEvent>
    #include <QTextEdit>
    #include <QLineEdit>
    
    class QAction;
    class QString;
    class QLineEdit;
    
    class MainWindow: public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
    
    private Q_SLOTS:
        void onTextChanged(const QString &);
    
    private:
        QLineEdit *lineEdit;
        //QVector<QString> vect;
        Functions *functions;
    };
    
    #endif
    

    mainwindows.cpp:

    #include "mainwindow.h"
    
    #include <QApplication>
    #include <QtWidgets>
    #include <QDebug>
    #include <QGridLayout>
    #include <QLineEdit>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent), lineEdit(nullptr), functions(nullptr)
    {
        QWidget *centralWidget = new QWidget;
        
        lineEdit = new QLineEdit;
        
        functions = new Functions();
        
        connect(lineEdit, SIGNAL(textChanged(const QString &)), 
            this, SLOT(onTextChanged(const QString &)));
    
        QGridLayout *layout = new QGridLayout;
        layout->addWidget(lineEdit, 0, 0);
        
        centralWidget->setLayout(layout);
        setCentralWidget(centralWidget);
        
        lineEdit->setFocus();
    }
    
    // Here goes the command to be parsed to functions->receivedLineCommand()
    void MainWindow::onTextChanged(const QString &str)
    {
        QString my_line = lineEdit->text();
        functions->receivedLineCommand(my_line);
    }
    

    functions.h

    #ifndef FUNCTIONS_H
    #define FUNCTIONS_H
    
    #include <QVector>
    #include <QString>
    
    class Functions {
        QVector<QString> m_vect;
        int numberOfArguments = 0;
        int previousNumberOfArguments = 0;
        
    public:
        Functions();
        void receivedLineCommand(QString &command);
        void printVector(QVector<QString> vect); 
    };
    #endif
    

    functions.cpp

    #include "functions.h"
    
    #include <QDebug>
    #include <QCommandLineOption>
    #include <QCommandLineParser>
    #include <QCoreApplication>
    
    Functions::Functions() :  {}
    
    void Functions::printVector()
    {
        for (int i = 0; i < m_vect.size(); ++i)
            qDebug() << m_vect.at(i) << "\n";
    }
    
    void Functions::receivedLineCommand(QString &command)
    {    
        QString str; 
        std::string comm = command.toStdString();
    
        for (int i = 0; i < comm.length(); ++i) {
            if (comm.at(i) == ' ') {
                ++numberOfArguments;
            }
            if (i == comm.length() - 1) {
                ++numberOfArguments;
                previousNumberOfArguments = numberOfArguments;
            }
        }
        QStringList arguments = QCoreApplication::arguments();
    
        if (arguments.count() == 1)  {
            qDebug() << "arguments.at(1) = " << arguments.at(1) << "\n";
            //qDebug() << "arguments.at(1) = " << arguments.at(1) << "\n";
        } else if (arguments.count() == 2) {
            str = arguments.at(2);
            m_vect.push_back(str); 
    
        } else if (arguments.count() == 3) {
            str = arguments.at(3);
            m_vect.push_back(str);
        }
    }
    

    It seems that m_vect gets too big and this creates a segfault.

    Thank you


  • Qt Champions 2017

    You are trying to split the args ? If this is the can you just try the following ?

    QString args = "solve from -5 to 10";
    QStringList list = args.split(" ");
    

  • Lifetime Qt Champion

    Hi,

    To add to @dheerendra, what are you going to use these arguments for ?


Log in to reply