Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to update QLineEdit in a QDialog

How to update QLineEdit in a QDialog

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    frnklu20
    wrote on last edited by
    #1

    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?

    Pablo J. RoginaP Gojir4G 2 Replies Last reply
    0
    • F frnklu20

      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?

      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

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

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3
      • F frnklu20

        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?

        Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by Gojir4
        #3

        @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.
        jsulmJ 1 Reply Last reply
        3
        • Gojir4G Gojir4

          @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.
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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();
              }
              ...
          }
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • F Offline
            F Offline
            frnklu20
            wrote on last edited by
            #5

            Thank you!!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved