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. Set Array of QlineEdit to spaces
Qt 6.11 is out! See what's new in the release blog

Set Array of QlineEdit to spaces

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 3.7k Views 1 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.
  • SGaistS SGaist

    Hi,

    Are you sure all array elements have been properly allocated before clearing them ?

    TheCipo76T Offline
    TheCipo76T Offline
    TheCipo76
    wrote on last edited by
    #3

    @SGaist
    All elements was added to form but
    some of this can be empty

    i've use to show some component who create an article
    an article might have 0 min /10 max component

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

      Then why don't you check that they exists before calling methods on them ?

      By the way, you should rather use QVector do store your pointers, you can then more easily properly manage the dynamic side of your widget.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      TheCipo76T 1 Reply Last reply
      2
      • SGaistS SGaist

        Then why don't you check that they exists before calling methods on them ?

        By the way, you should rather use QVector do store your pointers, you can then more easily properly manage the dynamic side of your widget.

        TheCipo76T Offline
        TheCipo76T Offline
        TheCipo76
        wrote on last edited by
        #5

        @SGaist
        Ok with check you suggest it works.

        one question:
        why you tell me to store my pointers using Qvector?
        i can't manage it with index of loop like i'm doing now??
        what you mean with "more easily properly"?

        Thank you

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #6

          That makes three questions ;)

          1. Because you have a dynamic number of widgets so rather than burning CPU for checking empty widgets, you could use only what you need.
          2. That would be the same but you would use the QVector size rather than a fixed number that you will but all over your code. You can still set a maximum though.
          3. See previous numbers.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          TheCipo76T 1 Reply Last reply
          1
          • SGaistS SGaist

            That makes three questions ;)

            1. Because you have a dynamic number of widgets so rather than burning CPU for checking empty widgets, you could use only what you need.
            2. That would be the same but you would use the QVector size rather than a fixed number that you will but all over your code. You can still set a maximum though.
            3. See previous numbers.
            TheCipo76T Offline
            TheCipo76T Offline
            TheCipo76
            wrote on last edited by
            #7

            @SGaist
            Ok, i understand..
            as soon as possible i will try

            I hope i will be able do it

            Thank you

            1 Reply Last reply
            0
            • TheCipo76T Offline
              TheCipo76T Offline
              TheCipo76
              wrote on last edited by
              #8

              @SGaist said in Set Array of QlineEdit to spaces:

              Then why don't you check that they exists before calling methods on them ?
              By the way, you should rather use QVector do store your pointers, you can then more easily properly manage the dynamic side of your widget.

              Sorry, i'm in error.. don't work..

              i've used this code:

              for  (int i=0; i<10; i++){
                      if (array_Cod[i]->text() != "") {
                          //array_Cod[i]->setPlaceholderText("load by Database");
                          array_Cod[i]->setText("");
                          //array_Desc[i]->setText("");
                          //array_Fabb[i]->setText("");
                      }
                  }
              

              but application crash again..

              where is the problem??

              jsulmJ 1 Reply Last reply
              0
              • TheCipo76T TheCipo76

                @SGaist said in Set Array of QlineEdit to spaces:

                Then why don't you check that they exists before calling methods on them ?
                By the way, you should rather use QVector do store your pointers, you can then more easily properly manage the dynamic side of your widget.

                Sorry, i'm in error.. don't work..

                i've used this code:

                for  (int i=0; i<10; i++){
                        if (array_Cod[i]->text() != "") {
                            //array_Cod[i]->setPlaceholderText("load by Database");
                            array_Cod[i]->setText("");
                            //array_Desc[i]->setText("");
                            //array_Fabb[i]->setText("");
                        }
                    }
                

                but application crash again..

                where is the problem??

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #9

                @TheCipo76 said in Set Array of QlineEdit to spaces:

                where is the problem??

                The problem is that your if is completely useless.
                You need to check the pointer:

                if (array_Cod[i]) {
                
                }
                

                You need to learn use pointers: if you declare a pointer variable but don't assign it a valid pointer and then try to use it your app will crash because to pointer is pointing to not allocated memory.

                int* some_pointer;
                std::cout << *some_pointer; // Crash here
                

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

                TheCipo76T 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @TheCipo76 said in Set Array of QlineEdit to spaces:

                  where is the problem??

                  The problem is that your if is completely useless.
                  You need to check the pointer:

                  if (array_Cod[i]) {
                  
                  }
                  

                  You need to learn use pointers: if you declare a pointer variable but don't assign it a valid pointer and then try to use it your app will crash because to pointer is pointing to not allocated memory.

                  int* some_pointer;
                  std::cout << *some_pointer; // Crash here
                  
                  TheCipo76T Offline
                  TheCipo76T Offline
                  TheCipo76
                  wrote on last edited by
                  #10

                  @jsulm

                  Later (in the code) i've used this code :

                  int x = 0;
                      if (q.next())
                      {
                          do {
                              for (int j = x; j < 10; j++){
                                  if (q.value(0) == array_Cod[j]->text()) {
                                     array_Disp[j]->setText(QVariant(q.value(1)).toString());
                                      x++;
                                  } else {
                                      if (array_Cod[j]->text() != ""){
                                      array_Disp[j]->setText("0");
                                      }
                                  }
                              };
                          } while (q.next());
                  
                      }
                  

                  and it works..

                  Where is the differences??

                  jsulmJ 1 Reply Last reply
                  0
                  • TheCipo76T TheCipo76

                    @jsulm

                    Later (in the code) i've used this code :

                    int x = 0;
                        if (q.next())
                        {
                            do {
                                for (int j = x; j < 10; j++){
                                    if (q.value(0) == array_Cod[j]->text()) {
                                       array_Disp[j]->setText(QVariant(q.value(1)).toString());
                                        x++;
                                    } else {
                                        if (array_Cod[j]->text() != ""){
                                        array_Disp[j]->setText("0");
                                        }
                                    }
                                };
                            } while (q.next());
                    
                        }
                    

                    and it works..

                    Where is the differences??

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #11

                    @TheCipo76 I don't know. Maybe this time all pointers in array_Cod were valid ("Later (in the code)")?

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

                    TheCipo76T 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @TheCipo76 I don't know. Maybe this time all pointers in array_Cod were valid ("Later (in the code)")?

                      TheCipo76T Offline
                      TheCipo76T Offline
                      TheCipo76
                      wrote on last edited by TheCipo76
                      #12

                      @jsulm

                      this is my code:

                      void regtemciclo::on_comboBox_Codice_currentTextChanged(const QString &arg1)
                      {
                          // CODICE SELEZIONATO
                          if (!aDatabase.open())
                              {
                              // Messaggio Errore
                              QMessageBox::critical(this, "Errore", aDatabase.lastError().text());
                              return;
                              }
                          // Pulizia Dati Articolo
                          for  (int i=0; i<10; i++){
                              if (array_Cod[i]->text() != "") {
                                  //array_Cod[i]->setText("0");
                                  array_Desc[i]->setText("0"); // (1) don't work
                                  //array_Fabb[i]->setText("");
                              }
                          }
                      
                          //Carico Dati Articolo da DATABASE
                          QSqlQuery q;
                          q.prepare("Select * from ARTICOLI where CODICE = '"+ arg1 +"'");
                          q.exec();
                      
                          if (q.next())
                          {
                              ui->lineEdit_Conto->setText(q.value(2).toString());
                              ui->lineEdit_Esp->setText(q.value(4).toString());
                              ui->lineEdit_Descrizione->setText(q.value(3).toString());
                              ui->lineEdit_SAP->setText(q.value(5).toString());
                              }
                          // Carico Componenti da DATABASE
                          //QSqlQuery q;
                          q.prepare("SELECT CODICE, DESCRIZIONE, QTA FROM COMPONENTI WHERE COMPLESSIVO =" + arg1 + " order by CODICE");
                          if (q.exec())
                          {
                              if (q.next()) {
                                  int i = 0;
                                  do
                                     {
                                      array_Cod[i]->setText(q.value(0).toString());
                                      array_Desc[i]->setText(q.value(1).toString());
                                      array_Fabb[i]->setText(q.value(2).toString());
                                      i++;
                                  }
                                 while (q.next());
                                 }
                          }
                          q.prepare("Select Codice, Quantità from PZSCORTA where Complessivo = " + arg1 + "order by Codice");
                          q.exec();
                          int x = 0;
                          if (q.next())
                          {
                              do {
                                  for (int j = x; j < 10; j++){
                                      if (q.value(0) == array_Cod[j]->text()) {
                                         array_Disp[j]->setText(QVariant(q.value(1)).toString());
                                          x++;
                                      } else {
                                          if (array_Cod[j]->text() != ""){
                                          array_Disp[j]->setText("0"); // (2) work OK
                                          }
                                      }
                                  };
                              } while (q.next());
                      
                          }
                      
                      }
                      

                      i'm new with c++ and with Qt both
                      but i can't view any difference.. but (1) don't work and (2) work..

                      (0_1537961922860_Schermata 2018-09-26 alle 13.37.50.png

                      jsulmJ 1 Reply Last reply
                      0
                      • TheCipo76T TheCipo76

                        @jsulm

                        this is my code:

                        void regtemciclo::on_comboBox_Codice_currentTextChanged(const QString &arg1)
                        {
                            // CODICE SELEZIONATO
                            if (!aDatabase.open())
                                {
                                // Messaggio Errore
                                QMessageBox::critical(this, "Errore", aDatabase.lastError().text());
                                return;
                                }
                            // Pulizia Dati Articolo
                            for  (int i=0; i<10; i++){
                                if (array_Cod[i]->text() != "") {
                                    //array_Cod[i]->setText("0");
                                    array_Desc[i]->setText("0"); // (1) don't work
                                    //array_Fabb[i]->setText("");
                                }
                            }
                        
                            //Carico Dati Articolo da DATABASE
                            QSqlQuery q;
                            q.prepare("Select * from ARTICOLI where CODICE = '"+ arg1 +"'");
                            q.exec();
                        
                            if (q.next())
                            {
                                ui->lineEdit_Conto->setText(q.value(2).toString());
                                ui->lineEdit_Esp->setText(q.value(4).toString());
                                ui->lineEdit_Descrizione->setText(q.value(3).toString());
                                ui->lineEdit_SAP->setText(q.value(5).toString());
                                }
                            // Carico Componenti da DATABASE
                            //QSqlQuery q;
                            q.prepare("SELECT CODICE, DESCRIZIONE, QTA FROM COMPONENTI WHERE COMPLESSIVO =" + arg1 + " order by CODICE");
                            if (q.exec())
                            {
                                if (q.next()) {
                                    int i = 0;
                                    do
                                       {
                                        array_Cod[i]->setText(q.value(0).toString());
                                        array_Desc[i]->setText(q.value(1).toString());
                                        array_Fabb[i]->setText(q.value(2).toString());
                                        i++;
                                    }
                                   while (q.next());
                                   }
                            }
                            q.prepare("Select Codice, Quantità from PZSCORTA where Complessivo = " + arg1 + "order by Codice");
                            q.exec();
                            int x = 0;
                            if (q.next())
                            {
                                do {
                                    for (int j = x; j < 10; j++){
                                        if (q.value(0) == array_Cod[j]->text()) {
                                           array_Disp[j]->setText(QVariant(q.value(1)).toString());
                                            x++;
                                        } else {
                                            if (array_Cod[j]->text() != ""){
                                            array_Disp[j]->setText("0"); // (2) work OK
                                            }
                                        }
                                    };
                                } while (q.next());
                        
                            }
                        
                        }
                        

                        i'm new with c++ and with Qt both
                        but i can't view any difference.. but (1) don't work and (2) work..

                        (0_1537961922860_Schermata 2018-09-26 alle 13.37.50.png

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #13

                        @TheCipo76

                        array_Desc[i]->setText("0"); // (1) don't work
                        array_Disp[j]->setText("0"); // (2) work OK
                        

                        Come on array_Desc is not the same as array_Disp, right? So, again you have invalid pointers in array_Desc, but in array_Disp all pointers are valid, that's why second works.
                        Did you actually try what I suggested?
                        Simply check whether the pointer is valid or not, this is really basic:

                        if (array_Desc[i])
                            array_Desc[i]->setText("0");
                        

                        And as already suggested you should use QVector instead of arrays, because array have fixed length.
                        And if you still prefer to use arrays then you should at least make sure you initialize them with 0 or nullptr at the beginning, so the check I posted can work.

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

                        TheCipo76T 1 Reply Last reply
                        4
                        • jsulmJ jsulm

                          @TheCipo76

                          array_Desc[i]->setText("0"); // (1) don't work
                          array_Disp[j]->setText("0"); // (2) work OK
                          

                          Come on array_Desc is not the same as array_Disp, right? So, again you have invalid pointers in array_Desc, but in array_Disp all pointers are valid, that's why second works.
                          Did you actually try what I suggested?
                          Simply check whether the pointer is valid or not, this is really basic:

                          if (array_Desc[i])
                              array_Desc[i]->setText("0");
                          

                          And as already suggested you should use QVector instead of arrays, because array have fixed length.
                          And if you still prefer to use arrays then you should at least make sure you initialize them with 0 or nullptr at the beginning, so the check I posted can work.

                          TheCipo76T Offline
                          TheCipo76T Offline
                          TheCipo76
                          wrote on last edited by
                          #14

                          @jsulm
                          OK, now works

                          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