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
Servers for Qt installer are currently down

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.
  • S Offline
    S Offline
    Swati777999
    wrote on 3 Dec 2021, 08:01 last edited by
    #1

    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

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

    J J 2 Replies Last reply 3 Dec 2021, 08:04
    0
    • S Swati777999
      3 Dec 2021, 08:01

      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

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 3 Dec 2021, 08:04 last edited by
      #2

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

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

      If your app crashes then the first thing is to find out why instead of searching for something else.
      Use debugger to see where exactly it crashes and why.
      How many entries does myTable have?

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

      S 1 Reply Last reply 3 Dec 2021, 08:27
      2
      • J jsulm
        3 Dec 2021, 08:04

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

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

        If your app crashes then the first thing is to find out why instead of searching for something else.
        Use debugger to see where exactly it crashes and why.
        How many entries does myTable have?

        S Offline
        S Offline
        Swati777999
        wrote on 3 Dec 2021, 08:27 last edited by
        #3

        @jsulm
        It crashes for any value greater than 2.
        I checked it in the debugger and found out the faulty point ,i.e n=2.

        As mentioned above, for n=1, the widget covers the central portion instead of the top left part.

        Any suggestion about how to achieve it?

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

        J 1 Reply Last reply 3 Dec 2021, 08:29
        0
        • S Swati777999
          3 Dec 2021, 08:27

          @jsulm
          It crashes for any value greater than 2.
          I checked it in the debugger and found out the faulty point ,i.e n=2.

          As mentioned above, for n=1, the widget covers the central portion instead of the top left part.

          Any suggestion about how to achieve it?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 3 Dec 2021, 08:29 last edited by
          #4

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

          Any suggestion about how to achieve it?

          Can you please answer my question I asked above?

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

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 3 Dec 2021, 08:29 last edited by
            #5

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

            Any suggestion about how to achieve it?

            Check if your myTable - container really contains more than two entries.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • S Swati777999
              3 Dec 2021, 08:01

              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

              J Offline
              J Offline
              JonB
              wrote on 3 Dec 2021, 08:56 last edited by JonB 12 Mar 2021, 08:57
              #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?

              J 1 Reply Last reply 3 Dec 2021, 08:58
              0
              • J JonB
                3 Dec 2021, 08:56

                @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?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 3 Dec 2021, 08:58 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

                J S 2 Replies Last reply 3 Dec 2021, 08:59
                2
                • J jsulm
                  3 Dec 2021, 08:58

                  @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...

                  J Offline
                  J Offline
                  JonB
                  wrote on 3 Dec 2021, 08:59 last edited by JonB 12 Mar 2021, 09:06
                  #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)?

                  S 1 Reply Last reply 6 Dec 2021, 04:58
                  2
                  • J jsulm
                    3 Dec 2021, 08:58

                    @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...

                    S Offline
                    S Offline
                    Swati777999
                    wrote on 6 Dec 2021, 04:25 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

                    J 1 Reply Last reply 6 Dec 2021, 06:26
                    0
                    • J JonB
                      3 Dec 2021, 08:59

                      @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)?

                      S Offline
                      S Offline
                      Swati777999
                      wrote on 6 Dec 2021, 04:58 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
                      • S Swati777999
                        6 Dec 2021, 04:25

                        @jsulm

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

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 6 Dec 2021, 06:26 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

                        S 1 Reply Last reply 6 Dec 2021, 07:25
                        2
                        • J jsulm
                          6 Dec 2021, 06:26

                          @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++)
                          ...
                          
                          S Offline
                          S Offline
                          Swati777999
                          wrote on 6 Dec 2021, 07:25 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

                          J J 2 Replies Last reply 6 Dec 2021, 07:29
                          0
                          • S Swati777999
                            6 Dec 2021, 07:25

                            @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?

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 6 Dec 2021, 07:29 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
                            • S Swati777999
                              6 Dec 2021, 07:25

                              @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?

                              J Offline
                              J Offline
                              JonB
                              wrote on 6 Dec 2021, 08:12 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?

                              S 1 Reply Last reply 6 Dec 2021, 08:43
                              1
                              • J JonB
                                6 Dec 2021, 08:12

                                @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?

                                S Offline
                                S Offline
                                Swati777999
                                wrote on 6 Dec 2021, 08:43 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

                                J J 2 Replies Last reply 6 Dec 2021, 08:45
                                0
                                • S Swati777999
                                  6 Dec 2021, 08:43

                                  @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.

                                  J Offline
                                  J Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 6 Dec 2021, 08:45 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
                                  • S Swati777999
                                    6 Dec 2021, 08:43

                                    @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.

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 6 Dec 2021, 08:52 last edited by JonB 12 Jun 2021, 08:52
                                    #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

                                    1/17

                                    3 Dec 2021, 08:01

                                    • Login

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