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. Setting fixed values for height and width of array of widgets
Forum Updated to NodeBB v4.3 + New Features

Setting fixed values for height and width of array of widgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 4 Posters 1.4k 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.
  • Swati777999S Swati777999

    Refer to this Q here

    I want to set a fixed height and width to the tables so that it occupies a particular space in the window.

    For example, if n=1; it need not cover the entire window, instead of that it should cover a particular space.

     for (int ii=0;ii<n;ii++)
               {
                   tableLayout->addWidget(myTables[ii],0,ii);
                   myTable[ii]->setFixedHeight(20);
                   myTable[ii]->setFixedWidth(20);
               }
    

    My program crashes at n>=2. For n=1, the widget takes the central space of the window.

    Any suggestion about which function to select for fixing min height and width of the widgets.

    Thanks

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

    @Swati777999 said in Setting fixed values for height and width of array of widgets:

    tableLayout->addWidget(myTables[ii],0,ii);
    My program crashes at n>=2.

    So clearly, when you access myTables[2] that must be out-of-range --- I would guess your myTables only has 2 elements?

    jsulmJ 1 Reply Last reply
    0
    • JonBJ JonB

      @Swati777999 said in Setting fixed values for height and width of array of widgets:

      tableLayout->addWidget(myTables[ii],0,ii);
      My program crashes at n>=2.

      So clearly, when you access myTables[2] that must be out-of-range --- I would guess your myTables only has 2 elements?

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

      @JonB said in Setting fixed values for height and width of array of widgets:

      I would guess your myTables only has 2 elements?

      I asked that two times already, then @Christian-Ehrlicher asked same question, now you - let's see how long it takes to get this basic information :-)
      It is really hard to help people who do not read answers properly and do not answer questions...

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

      JonBJ Swati777999S 2 Replies Last reply
      2
      • jsulmJ jsulm

        @JonB said in Setting fixed values for height and width of array of widgets:

        I would guess your myTables only has 2 elements?

        I asked that two times already, then @Christian-Ehrlicher asked same question, now you - let's see how long it takes to get this basic information :-)
        It is really hard to help people who do not read answers properly and do not answer questions...

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

        @jsulm
        I wrote similar to same OP in https://forum.qt.io/topic/132497/how-to-get-the-dimensions-of-the-widgets/5. I hope that user will take time to read and act on people's questions/answers/suggestions.

        @Swati777999 wrote:

        I checked it in the debugger and found out the faulty point ,i.e n=2.

        It is good that you are using the debugger. When you were there did you look at the size/dimension/number of elements in myTables? There are windows in the debugger which display information like how many elements there are in a list/array. By the way, what type is your myTables (show its declaration)?

        Swati777999S 1 Reply Last reply
        2
        • jsulmJ jsulm

          @JonB said in Setting fixed values for height and width of array of widgets:

          I would guess your myTables only has 2 elements?

          I asked that two times already, then @Christian-Ehrlicher asked same question, now you - let's see how long it takes to get this basic information :-)
          It is really hard to help people who do not read answers properly and do not answer questions...

          Swati777999S Offline
          Swati777999S Offline
          Swati777999
          wrote on last edited by
          #9

          @jsulm

          but why would it have 2 entries? I've already used for-loop for adding entries to the widget.

          “ In order to be irreplaceable, one must always be different” – Coco Chanel

          jsulmJ 1 Reply Last reply
          0
          • JonBJ JonB

            @jsulm
            I wrote similar to same OP in https://forum.qt.io/topic/132497/how-to-get-the-dimensions-of-the-widgets/5. I hope that user will take time to read and act on people's questions/answers/suggestions.

            @Swati777999 wrote:

            I checked it in the debugger and found out the faulty point ,i.e n=2.

            It is good that you are using the debugger. When you were there did you look at the size/dimension/number of elements in myTables? There are windows in the debugger which display information like how many elements there are in a list/array. By the way, what type is your myTables (show its declaration)?

            Swati777999S Offline
            Swati777999S Offline
            Swati777999
            wrote on last edited by
            #10

            @JonB

            Declaration of myTables

             QMap<int, QTableWidget *>myTables;
            

            Actually, I want to create tables with 7 rows and 2 columns inside a QGridLayout tableLayout . I have performed
            inside a loop ,
            tableLayout->addwidgets(myTables[ii],0,ii)

             QWidget *myWidget = new QWidget();
             QGridLayout *tableLayout = new QGridLayout();
            

            In the debugger, for the value of myTable it says <not accessible >
            I hope that I made my point more clear.

            Let me write the code again:
            This code is working absolutely fine but

            for (int ii=0;ii<n;ii++)
             {
                tableLayout->addWidget(myTables[ii],0,ii);
             }
            

            when I add the following, it makes myTable[ii] in accessible, don't know why.

            for (int ii=0;ii<n;ii++)
            {
               tableLayout->addWidget(myTables[ii],0,ii);
                            //for adding table..............
               myTables[ii]->setRowCount(6);
               myTables[ii]->setColumnCount(2)
             }
            
            

            “ In order to be irreplaceable, one must always be different” – Coco Chanel

            1 Reply Last reply
            0
            • Swati777999S Swati777999

              @jsulm

              but why would it have 2 entries? I've already used for-loop for adding entries to the widget.

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

              @Swati777999 said in Setting fixed values for height and width of array of widgets:

              but why would it have 2 entries?

              Why are you asking me?! I did not write your code.
              Why don't you simply check how many entries you have there?!
              If you can't see it in debugger you can simply add a debug output:

              qDebug() << myTable.size();
              for (int ii=0;ii<n;ii++)
              ...
              

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

              Swati777999S 1 Reply Last reply
              2
              • jsulmJ jsulm

                @Swati777999 said in Setting fixed values for height and width of array of widgets:

                but why would it have 2 entries?

                Why are you asking me?! I did not write your code.
                Why don't you simply check how many entries you have there?!
                If you can't see it in debugger you can simply add a debug output:

                qDebug() << myTable.size();
                for (int ii=0;ii<n;ii++)
                ...
                
                Swati777999S Offline
                Swati777999S Offline
                Swati777999
                wrote on last edited by
                #12

                @jsulm

                Yes, I checked the size of table with qDebug() << myTable.size() which came out to be 1.

                Actually the fact is that

                QWidget *myWidget = new QWidget();
                QGridLayout *tableLayout = new QGridLayout();
                tableLayout->setMargin(10);
                myWidget->setLayout(tableLayout);
                QMainWindow::setCentralWidget(myWidget);
                
                int n=5;
                QMap<int, QTableWidget *>myTables;
                for (int ii=0;ii<n;ii++)
                { myTables[ii]=new QTableWidget();
                tableLayout->addWidget(myTables[ii],0,ii);
                }
                qDebug()<<"Size of the table is="<<myTables.size();
                

                The above code works fine with the size of myTable as 5, but
                whenever I want to do some operations on the elements of myTable [ii] , it crashes from there.
                What could be the reason of it?

                “ In order to be irreplaceable, one must always be different” – Coco Chanel

                jsulmJ JonBJ 2 Replies Last reply
                0
                • Swati777999S Swati777999

                  @jsulm

                  Yes, I checked the size of table with qDebug() << myTable.size() which came out to be 1.

                  Actually the fact is that

                  QWidget *myWidget = new QWidget();
                  QGridLayout *tableLayout = new QGridLayout();
                  tableLayout->setMargin(10);
                  myWidget->setLayout(tableLayout);
                  QMainWindow::setCentralWidget(myWidget);
                  
                  int n=5;
                  QMap<int, QTableWidget *>myTables;
                  for (int ii=0;ii<n;ii++)
                  { myTables[ii]=new QTableWidget();
                  tableLayout->addWidget(myTables[ii],0,ii);
                  }
                  qDebug()<<"Size of the table is="<<myTables.size();
                  

                  The above code works fine with the size of myTable as 5, but
                  whenever I want to do some operations on the elements of myTable [ii] , it crashes from there.
                  What could be the reason of it?

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

                  @Swati777999 said in Setting fixed values for height and width of array of widgets:

                  What could be the reason of it?

                  Use debugger to find the reason.
                  It will tell you in which line it crashes and you will have the stack trace which you can post here so others can see what is happening instead of guessing.

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

                  1 Reply Last reply
                  2
                  • Swati777999S Swati777999

                    @jsulm

                    Yes, I checked the size of table with qDebug() << myTable.size() which came out to be 1.

                    Actually the fact is that

                    QWidget *myWidget = new QWidget();
                    QGridLayout *tableLayout = new QGridLayout();
                    tableLayout->setMargin(10);
                    myWidget->setLayout(tableLayout);
                    QMainWindow::setCentralWidget(myWidget);
                    
                    int n=5;
                    QMap<int, QTableWidget *>myTables;
                    for (int ii=0;ii<n;ii++)
                    { myTables[ii]=new QTableWidget();
                    tableLayout->addWidget(myTables[ii],0,ii);
                    }
                    qDebug()<<"Size of the table is="<<myTables.size();
                    

                    The above code works fine with the size of myTable as 5, but
                    whenever I want to do some operations on the elements of myTable [ii] , it crashes from there.
                    What could be the reason of it?

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

                    @Swati777999
                    After you have done as @jsulm suggests and tracked down and solved the reason for your crash here.

                    Unless you have some reason in code we have not seen, I doubt you want the QMap<int, QTableWidget *>myTables you have chosen. It will just lead to complications. For an array why don't you declare it as QVector<QTableWidget *>myTables instead?

                    Swati777999S 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @Swati777999
                      After you have done as @jsulm suggests and tracked down and solved the reason for your crash here.

                      Unless you have some reason in code we have not seen, I doubt you want the QMap<int, QTableWidget *>myTables you have chosen. It will just lead to complications. For an array why don't you declare it as QVector<QTableWidget *>myTables instead?

                      Swati777999S Offline
                      Swati777999S Offline
                      Swati777999
                      wrote on last edited by
                      #15

                      @JonB
                      Thanks for the suggestion. Let me use QVector and check its impact on the code.

                      Edit: QMap works fine whereas QVector behaves in a weird way. At least, I am able to add myTable[ii] elements to the layout with the help of QMap container.

                      “ In order to be irreplaceable, one must always be different” – Coco Chanel

                      jsulmJ JonBJ 2 Replies Last reply
                      0
                      • Swati777999S Swati777999

                        @JonB
                        Thanks for the suggestion. Let me use QVector and check its impact on the code.

                        Edit: QMap works fine whereas QVector behaves in a weird way. At least, I am able to add myTable[ii] elements to the layout with the help of QMap container.

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

                        @Swati777999 said in Setting fixed values for height and width of array of widgets:

                        whereas QVector behaves in a weird way

                        Then it is your code.
                        Without code and more information not possible to say more than that.

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

                        1 Reply Last reply
                        0
                        • Swati777999S Swati777999

                          @JonB
                          Thanks for the suggestion. Let me use QVector and check its impact on the code.

                          Edit: QMap works fine whereas QVector behaves in a weird way. At least, I am able to add myTable[ii] elements to the layout with the help of QMap container.

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

                          @Swati777999 said in Setting fixed values for height and width of array of widgets:

                          Edit: QMap works fine whereas QVector behaves in a weird way. At least, I am able to add myTable[ii] elements to the layout with the help of QMap container.

                          For an array/QVector you must actively create enough room in the array to accommodate elements, through one of:

                          myTables.resize(5);  // allocates room for 5 elements in the vector, numbered 0..4
                          // or
                          myTables.append(new QTableWidget());  // appends one element to the vector
                          

                          I did say to deal with why your code with QMap does not work in the first place before worrying about this....

                          1 Reply Last reply
                          1

                          • Login

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