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. Array of QLabel
Forum Updated to NodeBB v4.3 + New Features

Array of QLabel

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 1.1k 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.
  • N Offline
    N Offline
    Nevez
    wrote on 3 Oct 2022, 12:22 last edited by
    #1

    Hello,
    I want to sort the labels alphabetically in a horizontal layout full of QLabels.
    I can keep the text of the labels in a vector and sort them alphabetically with Qcollator. The problem is not here.
    I am using a QList or QMap where I use these labels. I'm putting my labels in it without any problem.
    However, when adding my labels to the layout from this list, the program crashes. but why ?

    void dragglableWidgetUstBar::sortLabels()
    {
    
    if(!lay->isEmpty())
     {   
            QVector<QString>  veclist;
    
            for(int i = 0 ; i<lay->count();i++)
            {
                QWidget *widget = lay->itemAt(i)->widget();
                if(widget)
                {
                    krnLabel *label =  qobject_cast<KrnLabel *>(widget);
                    if(label)
                    {
                        veclist.append(label->text());                 //The texts of the labels are load to the vector.
                        labelMaps.insert(label->text(),label);      //   Label and text information is loadto qmap                }
                }
            }
    
    
            QCollator collator;
            collator.setNumericMode(true);
    
            std::sort(
                veclist.begin(),
                veclist.end(),
                [&](const QString &file1, const QString &file2)
                {
                    return collator.compare(file1, file2) < 0;            //  Sort alphabetically by string.        
        });
    
    
            if ( lay->layout() != NULL )
            {
                QLayoutItem* item;
                while ( ( item = lay->layout()->takeAt( 0 ) ) != NULL )     // delete items in layout
                {
                    delete item->widget();
                    delete item;
                }
                
            }
    
    
            for(auto &str : veclist)
            {
                lay->addWidget(labelMaps[str]);          //ERROR!! /
            }
    
       }
    }
    
    J 1 Reply Last reply 3 Oct 2022, 13:51
    0
    • N Nevez
      3 Oct 2022, 12:22

      Hello,
      I want to sort the labels alphabetically in a horizontal layout full of QLabels.
      I can keep the text of the labels in a vector and sort them alphabetically with Qcollator. The problem is not here.
      I am using a QList or QMap where I use these labels. I'm putting my labels in it without any problem.
      However, when adding my labels to the layout from this list, the program crashes. but why ?

      void dragglableWidgetUstBar::sortLabels()
      {
      
      if(!lay->isEmpty())
       {   
              QVector<QString>  veclist;
      
              for(int i = 0 ; i<lay->count();i++)
              {
                  QWidget *widget = lay->itemAt(i)->widget();
                  if(widget)
                  {
                      krnLabel *label =  qobject_cast<KrnLabel *>(widget);
                      if(label)
                      {
                          veclist.append(label->text());                 //The texts of the labels are load to the vector.
                          labelMaps.insert(label->text(),label);      //   Label and text information is loadto qmap                }
                  }
              }
      
      
              QCollator collator;
              collator.setNumericMode(true);
      
              std::sort(
                  veclist.begin(),
                  veclist.end(),
                  [&](const QString &file1, const QString &file2)
                  {
                      return collator.compare(file1, file2) < 0;            //  Sort alphabetically by string.        
          });
      
      
              if ( lay->layout() != NULL )
              {
                  QLayoutItem* item;
                  while ( ( item = lay->layout()->takeAt( 0 ) ) != NULL )     // delete items in layout
                  {
                      delete item->widget();
                      delete item;
                  }
                  
              }
      
      
              for(auto &str : veclist)
              {
                  lay->addWidget(labelMaps[str]);          //ERROR!! /
              }
      
         }
      }
      
      J Offline
      J Offline
      JonB
      wrote on 3 Oct 2022, 13:51 last edited by
      #2

      @Nevez said in Array of QLabel:

      However, when adding my labels to the layout from this list, the program crashes. but why ?

      What does the debugger tell you (e.g. stack trace) for a start?

      J 1 Reply Last reply 3 Oct 2022, 14:09
      0
      • N Offline
        N Offline
        Nevez
        wrote on 3 Oct 2022, 14:03 last edited by Nevez 10 Mar 2022, 14:04
        #3

        8e9a0ed1-7b3f-4930-8177-da3bf6937188-image.png

        this window is coming.

        In a little test I made, it works like this without any problems:

        KrnLabel *testlabel = new KrnLabel(view,_dataList,"testlabel",id=4);
        labels.append(testlabel);
        
        ...
        qDebug()<<"label name" : <<labels.at(0)->text();   // it works...
        

        but as in the code I shared at the top; the program crashes when using the label I added from the layout...

        1 Reply Last reply
        0
        • J JonB
          3 Oct 2022, 13:51

          @Nevez said in Array of QLabel:

          However, when adding my labels to the layout from this list, the program crashes. but why ?

          What does the debugger tell you (e.g. stack trace) for a start?

          J Offline
          J Offline
          JonB
          wrote on 3 Oct 2022, 14:09 last edited by JonB 10 Mar 2022, 14:18
          #4

          @Nevez

          @JonB said in Array of QLabel:

          What does the debugger tell you (e.g. stack trace) for a start?

          You need to look at he stack trace pane. You always need to look at the stack trace pane when you get a crash. Nobody wants to look through a whole program to trace down a crash somewhere when the first vital piece of information is given by the debugger. If you learn to do this you may even be able to see the problem and the fix yourself!

          As a separate point: the choice to implement "sorting of some existing labels" via: copy their text, delete each QLabel and create new QLabels from their text seems "not good" to me for a number of reasons. It would be preferable to write code which removes (but not deletes) the existing QLabels from their layout and re-uses them.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            Nevez
            wrote on 3 Oct 2022, 14:24 last edited by
            #5

            @JonB said in Array of QLabel:

            You need to look at he stack trace pane

            ce712c8d-5858-4e48-8c53-d81899ebd3c1-image.png

            Are you talking about this area?

            @JonB said in Array of QLabel:

            It would be preferable to write code which removes (but not deletes) the existing QLabels from their layout and re-uses them.

            yes, I'm currently researching how to change item's location in layout, but I haven't found anything yet.

            J 1 Reply Last reply 3 Oct 2022, 14:35
            0
            • N Nevez
              3 Oct 2022, 14:24

              @JonB said in Array of QLabel:

              You need to look at he stack trace pane

              ce712c8d-5858-4e48-8c53-d81899ebd3c1-image.png

              Are you talking about this area?

              @JonB said in Array of QLabel:

              It would be preferable to write code which removes (but not deletes) the existing QLabels from their layout and re-uses them.

              yes, I'm currently researching how to change item's location in layout, but I haven't found anything yet.

              J Offline
              J Offline
              JonB
              wrote on 3 Oct 2022, 14:35 last edited by JonB 10 Mar 2022, 14:40
              #6

              @Nevez
              So you can now see where it is crashing. What do you think you should do about it? What is the first thing you should print out or examine in that line? And why is your source code now different from what you posted?

              When you have answered, I will tell you what I presume is causing crash in your code :)

              1 Reply Last reply
              1
              • N Offline
                N Offline
                Nevez
                wrote on 3 Oct 2022, 14:48 last edited by
                #7

                @JonB said in Array of QLabel:

                Ve kaynak kodunuz neden şimdi yayınladığınızdan farklı?

                don't get hung up on it please, just some minor name differences.

                but I still don't understand why it crashed?
                can you tell me

                J 1 Reply Last reply 3 Oct 2022, 15:09
                0
                • Axel SpoerlA Offline
                  Axel SpoerlA Offline
                  Axel Spoerl
                  Moderators
                  wrote on 3 Oct 2022, 15:03 last edited by Axel Spoerl 10 Mar 2022, 15:04
                  #8

                  @Nevez said in Array of QLabel:

                  I am using a QList or QMap

                  ...so which of the two is used?
                  labelMaps.insert(label->text(),label); look s like it is QMap<QString, QLabel *> labelMaps

                  lay->addWidget(labelMaps[str]); looks like you should examine the documentation of QMap and QList.
                  You call will cast QString strto a qsizetypeand very likely overshoot the range of labelMaps. Then it crashes.

                  Assuming that labelMapsis a QMap, this is your friend.
                  The call in that case is lay->addWidget(labelMaps.value(str));

                  Software Engineer
                  The Qt Company, Oslo

                  1 Reply Last reply
                  0
                  • N Nevez
                    3 Oct 2022, 14:48

                    @JonB said in Array of QLabel:

                    Ve kaynak kodunuz neden şimdi yayınladığınızdan farklı?

                    don't get hung up on it please, just some minor name differences.

                    but I still don't understand why it crashed?
                    can you tell me

                    J Offline
                    J Offline
                    JonB
                    wrote on 3 Oct 2022, 15:09 last edited by JonB 10 Mar 2022, 15:22
                    #9

                    @Nevez
                    My understanding of your code:

                    labelMaps.insert(label->text(),label);      //   Label and text information is loadto qmap                }
                    

                    This inserts label, which is the (pointer to) the actual QLabel, into your map. Note that it does not copy it (nor can you copy a QWidget).

                    delete item->widget();
                    

                    This actually deletes the widget on the layout. This is the QLabel still pointed to in your saved map.

                    lay->addWidget(labelMaps[str]);          //ERROR!! /
                    

                    The widget has now been deleted, so you cannot re-add or do anything to it.

                    You cannot afford to delete the labels if you wish to re-add them. I think you want to delete the current layout items but not their widget? Does delete item without delete item->widget() solve your problem? Your earlier copy-to-map code checks if item widget is a KrnLabel *, to be robust you ought check that in your later removal from layout/re-add of widget.

                    UPDATE
                    I see @Axel-Spoerl has posted while I was typing this in. My analysis of your problem is quite different from his (I didn't look at your QMap access code, took it you have that right)....

                    1 Reply Last reply
                    2
                    • Axel SpoerlA Offline
                      Axel SpoerlA Offline
                      Axel Spoerl
                      Moderators
                      wrote on 3 Oct 2022, 15:33 last edited by
                      #10

                      @JonB said in Array of QLabel:

                      My analysis of your problem is quite different from his

                      Well, I stumbled over that one and didn't look any further. But @JonB is also right (nothing else expected).

                      Software Engineer
                      The Qt Company, Oslo

                      J 1 Reply Last reply 3 Oct 2022, 15:42
                      0
                      • Axel SpoerlA Axel Spoerl
                        3 Oct 2022, 15:33

                        @JonB said in Array of QLabel:

                        My analysis of your problem is quite different from his

                        Well, I stumbled over that one and didn't look any further. But @JonB is also right (nothing else expected).

                        J Offline
                        J Offline
                        JonB
                        wrote on 3 Oct 2022, 15:42 last edited by JonB 10 Mar 2022, 15:43
                        #11

                        @Axel-Spoerl
                        Just so @Nevez knows, his map access code is actually OK. He will be using a QMap not a QList. Only QList has the qsizetype, and I should be horrified if the compiler converted a QString to a qsizetype even if were QList :) So no "overshooting" here.

                        lay->addWidget(labelMaps.value(str)) would be safer here, because (although it does work here): labelMaps[str] returns a reference to the value but would silently create a default value at [str] if not found. OP's should be found OK, so it will work in this case, but .value(str) which would not do that would be nicer. Code ought also check value found and returned non-nullptr, as addWidget() would not like that and could have been cause of crash.

                        1 Reply Last reply
                        1
                        • N Offline
                          N Offline
                          Nevez
                          wrote on 4 Oct 2022, 07:43 last edited by
                          #12

                          @JonB and @Axel-Spoerl
                          Firstly , thank you so much for your amazing support and comments.

                          @Axel-Spoerl said in Array of QLabel:

                          Assuming that labelMapsis a QMap, this is your friend.
                          The call in that case is lay->addWidget(labelMaps.value(str));

                          Yes, I tried this method too. But the crash continued. What @JonB said in his comment caught my attention.

                          @JonB said in Array of QLabel:

                          This actually deletes the widget on the layout. This is the QLabel still pointed to in your saved map.

                          I can't believe how I missed this point.
                          This was the main point in my solving the problem.

                          so I wrote a new code that will remove the items from the layout without completely deleting them.
                          And I used the qmap method as @Axel-Spoerl said. no problem now
                          it's working.

                           QLayoutItem* item;
                            while ( !lay->isEmpty() && ( (item = lay->layout()->takeAt( 0 ) ) != NULL ) ){
                                      layout()->removeItem(item);
                          
                                    }
                           delete item;
                            for(auto &str : veclist)
                             {
                                      lay->addWidget(labelsMap.value(str));
                             }
                          

                          aca042a1-2b1f-469c-acf8-1cc0f5f8c0ea-image.png
                          As you can see in the picture, the labels are sorted without any problems, and the models in them are currently working without any problems.

                          1 Reply Last reply
                          2

                          1/12

                          3 Oct 2022, 12:22

                          • Login

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