How to update QLineEdit in a QDialog



  • In the program that i'm developing, there is a QDialog class named BarramDialog that contains 4 QLineEdit Widgets and there is a class named Diagrama.

    barramdialog.h

    class BarramDialog;
    }
    
    class BarramDialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit BarramDialog(QWidget *parent = nullptr);
        ~BarramDialog();
        QString nome="pegadinha";
        int num=42;
        float tensao;
        float ang;
    
        QString nome_dialog(QString &);
        void setdata(QList<QString>&);
        QList<QString> data=QList<QString>() <<""<<""<<""<<"";
    

    As you can see, in my class BarramDialog there is a QList<QString> variable that is responsible to set the text of the QLineEdit widgets when i execute the dialog:

    barramdialog.cpp

    BarramDialog::BarramDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::BarramDialog)
    {
        ui->setupUi(this);
    
        ui->ang_lin->setText(data[0]);
        ui->tensao_line->setText(data[1]);
        ui->num_lin->setText(data[2]);
        ui->nome_lin->setText(data[3]);
        update();
    }
    

    The problem is:

    diagrama.cpp

    void Diagrama::showMousePosition(const QPoint &pos, const QVariant property)
    {
        if(modo=="info")
        {
    
             BarramDialog *info=new BarramDialog;
             QList<QString> listagem= QList<QString>() <<QString::number(BarramentoMap[property.toString()]->ang)
                     <<QString::number(BarramentoMap[property.toString()]->tensao)
                     <<QString::number(BarramentoMap[property.toString()]->num)
                     <<BarramentoMap[property.toString()]->nome;
             info->setModal(true);
             info->setdata(listagem);
    
    
             info->exec();
        }
    

    the function setdata of BarramDialog is

    void BarramDialog::setdata(QList<QString> &new_list)
    {
       data=new_list;
    }
    

    But when i use this function and execute the dialog, the text on QLineEdit widget stay the same.

    How can i fix it?



  • @frnklu20 what about something like this (pseudo-code):

    BarramDialog::BarramDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::BarramDialog)
    {
        ui->setupUi(this);
        updateLineEdits();
        ....
    }
    
    void BarramDialog::updateLineEdits()
    {
         ui->ang_lin->setText(data[0]);
         ui->tensao_line->setText(data[1]);
         ui->num_lin->setText(data[2]);
         ui->nome_lin->setText(data[3]);
    }
    
    void BarramDialog::setdata(QList<QString> &new_list)
    {
       data=new_list;
       updateLineEdits();
    }
    


  • @frnklu20 Hi,

    Your QLineEdit are not updated because you don't update them in your method setData.

    I suggest to change setData like this:

    void BarramDialog::setdata(const QList<QString> &new_list)
    {
       ui->ang_lin->setText(data[0]);
       ui->tensao_line->setText(data[1]);
       ui->num_lin->setText(data[2]);
       ui->nome_lin->setText(data[3]);
    }
    

    Other points:

    • There is a memory leak issue in showMousePosition(). You have to set a parent to your dialog, or to destroy it at the end of the function:
    BarramDialog *info=new BarramDialog(this);
    
    //OR
    BarramDialog *info=new BarramDialog;
    ...
    info.deleteLater(); //put at the end of showMousePosition()
    

    Even simple, declare it on the stack:

    BarramDialog info;
    ...
    info.exec()
    
    • I have modified the parameter type in setData to const QList<QString> &new_list. I have added the const directive as there is no reason to pass it by non-const reference.

  • Qt Champions 2018

    @Gojir4 @frnklu20 Actually there is no need for heap allocation of BarramDialog.

    void Diagrama::showMousePosition(const QPoint &pos, const QVariant property)
    {
        if(modo=="info")
        {
            BarramDialog info;
            ...
            info.exec();
        }
        ...
    }
    


  • Thank you!!


 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.