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

How to change values of a QList<QString> using QDialog



  • I'm using a QDialog with 4 QLineEdits

    what i want is to take the text in these qlineEdit and change the values of each item of my Qlist

    equipamentos.h

    class BarramDialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit BarramDialog(QWidget *parent = nullptr);
        ~BarramDialog();
    
        QString nome_dialog(QString &);
        void setdata(QList<QString>&);
        QList<QString> data=QList<QString>() <<"pegadinha"<<""<<""<<"";
    
    private slots:
    
        void on_ok_button_clicked();
    
    private:
        Ui::BarramDialog *ui;
        void changevalues(int i, QString value);
    

    barramento.cpp

    BarramDialog::BarramDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::BarramDialog)
    {
        ui->setupUi(this);
        ui->name_line->setText(data[0]);
        ui->num_line->setText(data[1]);
        ui->v_line->setText(data[2]);
        ui->ang_line->setText(data[3]);
    
    }
    
    BarramDialog::~BarramDialog()
    {
        delete ui;
    }
    
    void BarramDialog::setdata(QList<QString> &new_list)
    {
        ui->name_line->setText(new_list[0]);
        ui->num_line->setText(new_list[1]);
        ui->v_line->setText(new_list[2]);
        ui->ang_line->setText(new_list[3]);
        update();
    }
    
    
    
    
    void BarramDialog::on_ok_button_clicked()
    {
    
        QMessageBox::StandardButton resposta=QMessageBox::question(this,"","Deseja salvar esses parametros?",QMessageBox::Yes|QMessageBox::No);
        if(resposta==QMessageBox::Yes)
        {
            data[0]=ui->name_line->text();
            data[1]=ui->num_line->text();
            data[2]=ui->v_line->text();
            data[3]=ui->ang_line->text();
            
            
            changevalues(0,ui->name_line->text());
            changevalues(1,ui->num_line->text());
            changevalues(2,ui->v_line->text());
            changevalues(3,ui->ang_line->text());
            
            this->close();
    
        }
    }
    
    void BarramDialog::changevalues(int i, QString value)
    {
        data[i]=value;
    }
    

    i did this but the values in the Qlist are not updated
    how can I fix it?


  • Qt Champions 2019

    @frnklu20 said in How to change values of a QList<QString> using QDialog:

    i did this but the values in the Qlist are not updated

    In which list? How do you check?
    And why do you do things twice?

    data[0]=ui->name_line->text();
    changevalues(0,ui->name_line->text());



  • @frnklu20 said in How to change values of a QList<QString> using QDialog:

    QList<QString> data=QList<QString>() <<"pegadinha"<<""<<""<<"";

    First...don't do this. It is ugly! Separate the container constructor from pushing values into it.

    I would argue that you should use a QMap so that each element being returned from your UI has a unique key associated with it and they are NOT position dependent within the QList.

    Finally, if I had to WAG (without testing) I'd guess that the constructor is the reason your list is failing. After calling the exact line you entered above, check data.size() and see what it says. Perhaps is is NOT actually four elements long?


  • Qt Champions 2019

    @Kent-Dorfman said in How to change values of a QList<QString> using QDialog:

    Perhaps is is NOT actually four elements long?

    It is - otherwise the compiler would be wrong.


  • Moderators

    @frnklu20 where exactly do you check, that the list was updated ? I can't see that from the code you posted


Log in to reply