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.6k 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 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

                          15/17

                          6 Dec 2021, 08:43

                          • Login

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