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. QVector< QVBoxLayout * > crashing on adding layout

QVector< QVBoxLayout * > crashing on adding layout

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 973 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.
  • I Offline
    I Offline
    Inzinejkr
    wrote on last edited by
    #1

    I'm trying to make a QStackedWidget with 5 labels and text inputs on each of the 10 pages. Like this: Screenshot_1.png
    So far I've got:

        int ss=0, s=0;
        ui->stck->setCurrentIndex(0);
        QVector< QWidget * > wdgVector(10);
        QVector< QVBoxLayout * > strVector(10);
        for (int i=0; i<50; i++){
            if (5<=s)
            {
                wdgVector[ss]->setLayout(strVector[ss]);
                ui->stck->insertWidget(ss, wdgVector[ss]);
                ss++;
                s=0;
            }
            QLabel *lbl = new QLabel(this);
            QHBoxLayout *hlay = new QHBoxLayout();
            QLineEdit *txt = new QLineEdit(this);
            hlay->addWidget(lbl);
            hlay->addWidget(txt);
            s++;
            strVector[ss]->addLayout(hlay);//<----CRASH
        }
    

    It's crashing on the last bit of code and I'm unsure how to fix it.

    sierdzioS 1 Reply Last reply
    0
    • I Inzinejkr

      I'm trying to make a QStackedWidget with 5 labels and text inputs on each of the 10 pages. Like this: Screenshot_1.png
      So far I've got:

          int ss=0, s=0;
          ui->stck->setCurrentIndex(0);
          QVector< QWidget * > wdgVector(10);
          QVector< QVBoxLayout * > strVector(10);
          for (int i=0; i<50; i++){
              if (5<=s)
              {
                  wdgVector[ss]->setLayout(strVector[ss]);
                  ui->stck->insertWidget(ss, wdgVector[ss]);
                  ss++;
                  s=0;
              }
              QLabel *lbl = new QLabel(this);
              QHBoxLayout *hlay = new QHBoxLayout();
              QLineEdit *txt = new QLineEdit(this);
              hlay->addWidget(lbl);
              hlay->addWidget(txt);
              s++;
              strVector[ss]->addLayout(hlay);//<----CRASH
          }
      

      It's crashing on the last bit of code and I'm unsure how to fix it.

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @Inzinejkr said in QVector< QVBoxLayout * > crashing on adding layout:

      strVector[ss]->addLayout(hlay);//<----CRASH

      You never insert any value into strVector. The vector is empty so it will always crash when you try to access an element of it.

      (Z(:^

      1 Reply Last reply
      2
      • I Offline
        I Offline
        Inzinejkr
        wrote on last edited by
        #3

        @sierdzio said in [QVector< QVBoxLayout * > crashing on adding layout]

        You never insert any value into strVector.

        So how would I do that to achieve the image shown?

        jsulmJ 1 Reply Last reply
        0
        • I Inzinejkr

          @sierdzio said in [QVector< QVBoxLayout * > crashing on adding layout]

          You never insert any value into strVector.

          So how would I do that to achieve the image shown?

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

          @Inzinejkr

          wdgVector[ss] = new QWidget();
          wdgVector[ss]->setLayout(strVector[ss]);
          

          But you also have to initialise strVector.

          QVector< QVBoxLayout * > strVector(10); // This only declares a vector containing pointers - you have to create actual objects and put their pointers there. C++ basics.
          

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

          1 Reply Last reply
          2
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by sierdzio
            #5
            QStackedWidget *parent = ... something;
            for (int i = 0; i < 10; ++i) {
              QVBoxLayout *vertical = new QVBoxLayout;
              for (int y = 0; y < 5; ++y) {
                QHBoxLayout *row = new QHBoxLayout;
                row->addWidget(new QLabel(tr("TextLabel")));
                row->addWidget(new QLineEdit);
                vertical->addLayout(row);
              }
              QWidget *page = new QWidget;
              page->setLayout(vertical);
              parent->addWidget(page);
            }
            

            (untested pseudocode ;-))

            (Z(:^

            1 Reply Last reply
            1
            • I Offline
              I Offline
              Inzinejkr
              wrote on last edited by
              #6

              @sierdzio said in QVector< QVBoxLayout * > crashing on adding layout:

              (untested pseudocode ;-))

              Alright, it does what I wanted, so good job! :)
              Just for the beauty I thought about inserting the label and text input in a group box, but I can't get that to work neither.

                  int z=0;
              
                  for (int i = 0; i < 3; i++) {
                  QVBoxLayout *vertical = new QVBoxLayout;
                  for (int y = 0; y < 5; y++) {
                      QGroupBox *grp=new QGroupBox();
                      QVBoxLayout *vlay=new QVBoxLayout;
                      grp->setTitle("Something number: "+QString::number(z));
                      vlay->addWidget(new QLabel("Label number: ("+QString::number(z)));
                      vlay->addWidget(new QLineEdit);
                      vertical->addLayout(vlay);
                      grp->setLayout(vertical);
                      z++;
                    }
                    QWidget *page = new QWidget;
                    page->setLayout(vertical);
                    ui->stck->insertWidget(i, page);
                    ui->stck->setCurrentIndex(0);
                  }
              

              Sorry for bothering you..

              JonBJ 1 Reply Last reply
              0
              • I Inzinejkr

                @sierdzio said in QVector< QVBoxLayout * > crashing on adding layout:

                (untested pseudocode ;-))

                Alright, it does what I wanted, so good job! :)
                Just for the beauty I thought about inserting the label and text input in a group box, but I can't get that to work neither.

                    int z=0;
                
                    for (int i = 0; i < 3; i++) {
                    QVBoxLayout *vertical = new QVBoxLayout;
                    for (int y = 0; y < 5; y++) {
                        QGroupBox *grp=new QGroupBox();
                        QVBoxLayout *vlay=new QVBoxLayout;
                        grp->setTitle("Something number: "+QString::number(z));
                        vlay->addWidget(new QLabel("Label number: ("+QString::number(z)));
                        vlay->addWidget(new QLineEdit);
                        vertical->addLayout(vlay);
                        grp->setLayout(vertical);
                        z++;
                      }
                      QWidget *page = new QWidget;
                      page->setLayout(vertical);
                      ui->stck->insertWidget(i, page);
                      ui->stck->setCurrentIndex(0);
                    }
                

                Sorry for bothering you..

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Inzinejkr said in QVector< QVBoxLayout * > crashing on adding layout:

                grp->setLayout(vertical);

                You are creating multiple QGroupBoxes in your loop, yet you set each one to have the same QVBoxLayout *vertical = new QVBoxLayout; instance. And you later do page->setLayout(vertical);. Something wrong here!

                I 1 Reply Last reply
                1
                • JonBJ JonB

                  @Inzinejkr said in QVector< QVBoxLayout * > crashing on adding layout:

                  grp->setLayout(vertical);

                  You are creating multiple QGroupBoxes in your loop, yet you set each one to have the same QVBoxLayout *vertical = new QVBoxLayout; instance. And you later do page->setLayout(vertical);. Something wrong here!

                  I Offline
                  I Offline
                  Inzinejkr
                  wrote on last edited by
                  #8

                  @JonB said in QVector< QVBoxLayout * > crashing on adding layout:

                  You are creating multiple QGroupBoxes in your loop, yet you set each one to have the same QVBoxLayout *vertical = new QVBoxLayout; instance. And you later do page->setLayout(vertical);. Something wrong here!

                  Well, I guess? I'm trying to group up one label and one textbox..
                  Screenshot_2.png
                  So this, just via code

                  JonBJ 1 Reply Last reply
                  0
                  • I Inzinejkr

                    @JonB said in QVector< QVBoxLayout * > crashing on adding layout:

                    You are creating multiple QGroupBoxes in your loop, yet you set each one to have the same QVBoxLayout *vertical = new QVBoxLayout; instance. And you later do page->setLayout(vertical);. Something wrong here!

                    Well, I guess? I'm trying to group up one label and one textbox..
                    Screenshot_2.png
                    So this, just via code

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @Inzinejkr
                    I can only say: start by creating separate instances of all your QVBoxLayouts: a given instance must only be used as a layout for a widget once, not re-used for multiple widgets as you presently show in your code.

                    1 Reply Last reply
                    2

                    • Login

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