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. Exception when reading widget store on QVector

Exception when reading widget store on QVector

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 263 Views
  • 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
    n34rt
    wrote on last edited by
    #1

    Why when i write like the first example i get an exception in the layout->addWidget line
    and when i write like the second example i get no exception?

    //1
    class SpinnerButton : public QPushButton
    {
        Q_OBJECT
    
    public:
    
        QLabel* lbl;
        QLabel* lbl2;
        QVector<QLabel*> lbl_vec { lbl, lbl2 };
        //...
    }
    
    for (int i = 0; i < 2; i++)
    {
        lbl_vec[i] = new QLabel(this);
        //...
    }
    
    QGridLayout* layout = new QGridLayout();
    this->setLayout(layout);
    layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- exception
    
    //2
    class SpinnerButton : public QPushButton
    {
        Q_OBJECT
    
    public:
    
        QLabel* lbl = new QLabel(this);
        QLabel* lbl2 = new QLabel(this);
        QVector<QLabel*> lbl_vec { lbl, lbl2};
        //...
    }
    
    for (int i = 0; i < 2; i++)
    {
        lbl_vec[i]->setText("Loading");
        //...
    }
    
    QGridLayout* layout = new QGridLayout();
    this->setLayout(layout);
    layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- no exception
    

    Also, is it wrong to initialize a label in a subclass header like this?

    public:
        QLabel* lbl = new QLabel(this);
    
    JonBJ jsulmJ 2 Replies Last reply
    0
    • N n34rt

      Why when i write like the first example i get an exception in the layout->addWidget line
      and when i write like the second example i get no exception?

      //1
      class SpinnerButton : public QPushButton
      {
          Q_OBJECT
      
      public:
      
          QLabel* lbl;
          QLabel* lbl2;
          QVector<QLabel*> lbl_vec { lbl, lbl2 };
          //...
      }
      
      for (int i = 0; i < 2; i++)
      {
          lbl_vec[i] = new QLabel(this);
          //...
      }
      
      QGridLayout* layout = new QGridLayout();
      this->setLayout(layout);
      layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- exception
      
      //2
      class SpinnerButton : public QPushButton
      {
          Q_OBJECT
      
      public:
      
          QLabel* lbl = new QLabel(this);
          QLabel* lbl2 = new QLabel(this);
          QVector<QLabel*> lbl_vec { lbl, lbl2};
          //...
      }
      
      for (int i = 0; i < 2; i++)
      {
          lbl_vec[i]->setText("Loading");
          //...
      }
      
      QGridLayout* layout = new QGridLayout();
      this->setLayout(layout);
      layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- no exception
      

      Also, is it wrong to initialize a label in a subclass header like this?

      public:
          QLabel* lbl = new QLabel(this);
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @n34rt said in Exception when reading widget store on QVector:

      QVector<QLabel*> lbl_vec { lbl, lbl2 };

      At that time lb1 and lb2 are invalid pointers! So, if you use them your app will crash.

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

      1 Reply Last reply
      0
      • N n34rt

        Why when i write like the first example i get an exception in the layout->addWidget line
        and when i write like the second example i get no exception?

        //1
        class SpinnerButton : public QPushButton
        {
            Q_OBJECT
        
        public:
        
            QLabel* lbl;
            QLabel* lbl2;
            QVector<QLabel*> lbl_vec { lbl, lbl2 };
            //...
        }
        
        for (int i = 0; i < 2; i++)
        {
            lbl_vec[i] = new QLabel(this);
            //...
        }
        
        QGridLayout* layout = new QGridLayout();
        this->setLayout(layout);
        layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- exception
        
        //2
        class SpinnerButton : public QPushButton
        {
            Q_OBJECT
        
        public:
        
            QLabel* lbl = new QLabel(this);
            QLabel* lbl2 = new QLabel(this);
            QVector<QLabel*> lbl_vec { lbl, lbl2};
            //...
        }
        
        for (int i = 0; i < 2; i++)
        {
            lbl_vec[i]->setText("Loading");
            //...
        }
        
        QGridLayout* layout = new QGridLayout();
        this->setLayout(layout);
        layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- no exception
        

        Also, is it wrong to initialize a label in a subclass header like this?

        public:
            QLabel* lbl = new QLabel(this);
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @n34rt said in Exception when reading widget store on QVector:

        layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- exception

        layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- no exception

        Did you run your code in a debugger to answer this? In the first case (QLabel* lbl;) lbl is an uninitialized variable so it "crashes", in the second case (QLabel* lbl = new QLabel(this);) lbl is set to a QLabel so it does not. Don't know what else you would expect. There is no relevance to the QVector you mention in your title or your lbl_vec.

        QLabel* lbl = new QLabel(this);

        You can put that in a class declaration/header if you wish. Personally I would not: I would do the lbl = new QLabel(this); in the class's constructor code.

        1 Reply Last reply
        1
        • N n34rt

          Why when i write like the first example i get an exception in the layout->addWidget line
          and when i write like the second example i get no exception?

          //1
          class SpinnerButton : public QPushButton
          {
              Q_OBJECT
          
          public:
          
              QLabel* lbl;
              QLabel* lbl2;
              QVector<QLabel*> lbl_vec { lbl, lbl2 };
              //...
          }
          
          for (int i = 0; i < 2; i++)
          {
              lbl_vec[i] = new QLabel(this);
              //...
          }
          
          QGridLayout* layout = new QGridLayout();
          this->setLayout(layout);
          layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- exception
          
          //2
          class SpinnerButton : public QPushButton
          {
              Q_OBJECT
          
          public:
          
              QLabel* lbl = new QLabel(this);
              QLabel* lbl2 = new QLabel(this);
              QVector<QLabel*> lbl_vec { lbl, lbl2};
              //...
          }
          
          for (int i = 0; i < 2; i++)
          {
              lbl_vec[i]->setText("Loading");
              //...
          }
          
          QGridLayout* layout = new QGridLayout();
          this->setLayout(layout);
          layout->addWidget(lbl, 0, 1, Qt::AlignRight); // <- no exception
          

          Also, is it wrong to initialize a label in a subclass header like this?

          public:
              QLabel* lbl = new QLabel(this);
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @n34rt said in Exception when reading widget store on QVector:

          QVector<QLabel*> lbl_vec { lbl, lbl2 };

          At that time lb1 and lb2 are invalid pointers! So, if you use them your app will crash.

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

          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