Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use QStringList to store multiple strings

I’m using QT framework to create an employee interface which stores information of an employee (ID, name, surname, skills, ecc..)

In the widget interface I’ve used a lineEdit for everything since I need to store string types of information, the only exception of parameter skills that should be a list of strings

In my mainwindow.cpp however I should store a list of different skills, not just one, and that’s get me the error when I use my function .addEmployee() because it wants a list of skills, not one string.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I should use QStringList skills but how can I tell the user is inserting different skills and store them as a list?

void MainWindow::on_add_pushButton_clicked()
{
    QString id = ui->id_lineEdit->text();
    QString nome = ui->name_lineEdit->text();
    QString cognome = ui->surname_lineEdit->text();

    QString skills = ui->skills_lineEdit->text();
    // should be QStringList skills but how can I tell the user is inserting different skills? 

    manage.addEmployee(id.toStdString(), name.toStdString(), surname.toStdString(),                                                                     skills.toStdString());

// manage refers to the class manageEmployee.h I already implemented to use function addEmployee(id, name, surname, {skills});
}

>Solution :

The question is a bit unrefined, but I will try my best to answer based on what I could follow.

void MainWindow::on_add_pushButton_clicked()
{
    QString id = ui->id_lineEdit->text();
    QString name = ui->name_lineEdit->text();
    QString surname = ui->surname_lineEdit->text();

    QString skills = ui->skills_lineEdit->text();
    QStringList skillsList = skills.split(","); // or ";", etc.

    manage.addEmployee(id.toStdString(), name.toStdString(), surname.toStdString(), skillsList.toStdList());
}

So if you want the user to input list of skills, you wanna be splitting them using a delimiter as well.
Then you can pass the resulting QStringList to .addEmployee assuming it takes a fourth argument std::list<std::string>

By using a delimiter, you allow the user to enter multiple skills as a single input, which simplifies the interface. However, you should make sure to handle any errors that can arise from invalid input, such as an empty or malformed input string.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading