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. Array of widgets and inputting the limit of for-loop iteration from the users
Forum Updated to NodeBB v4.3 + New Features

Array of widgets and inputting the limit of for-loop iteration from the users

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 998 Views 2 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.
  • S Offline
    S Offline
    Swati777999
    wrote on 2 Dec 2021, 09:18 last edited by
    #1

    Hi All,

    I want to know how to make an array of any widgets and keep building the widgets and placing them in a layout.

    For example ,
    debug() <<"Enter the number of widgets to be created";
    // how to input the value for 'n'
    for (int jj=0;jj<n;jj++) {
    QTableWidget *myTable[jj]=new QTableWidget();
    QGridLayout *tableLayout = new QGridLayout();
    tableLayout->addWidget(myTable[jj],jj,0);
    }

    Any suggestion will be highly appreciated.

    Thanks!

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

    J 1 Reply Last reply 2 Dec 2021, 09:26
    0
    • J JonB
      2 Dec 2021, 09:26

      @Swati777999
      You are already creating the number of widgets in the for loop. Just presumably do not create a new QTableWidget nor new QGridLayout within the loop: you don't want these newed each time, do you, you want to create them outside the loop and add the new widgets to a fixed QTableWidget/QGridLayout ? Or maybe your QTableWidget is the widget you want to create, you don't say. But why you want a new layout each time is beyond me.

      // how to input the value for 'n'

      However you want. Prompt the user to enter the n number in a dialog box? Obtain it from, say, a QSpinBox widget somewhere? We don't know.

      QTableWidget *myTable[jj]=new QTableWidget();

      This is not legal C++. You cannot declare a variable which is an indexed reference into an array.

      It's difficult to discern what your actual question is.

      S Offline
      S Offline
      Swati777999
      wrote on 3 Dec 2021, 04:00 last edited by
      #6

      @JonB

       int n=7;
          QMap<int, QWidget *>myTables;
          for (int ii=0;ii<n;ii++)
          {  myTables[ii]=new QTableWidget();
             tableLayout->addWidget(myTables[ii],ii,0);
          }
      

      I got the array implementation using QMap from the above code. Yaay !!

      Now, I've to do the resizing part.

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

      T 1 Reply Last reply 3 Dec 2021, 04:14
      0
      • S Swati777999
        2 Dec 2021, 09:18

        Hi All,

        I want to know how to make an array of any widgets and keep building the widgets and placing them in a layout.

        For example ,
        debug() <<"Enter the number of widgets to be created";
        // how to input the value for 'n'
        for (int jj=0;jj<n;jj++) {
        QTableWidget *myTable[jj]=new QTableWidget();
        QGridLayout *tableLayout = new QGridLayout();
        tableLayout->addWidget(myTable[jj],jj,0);
        }

        Any suggestion will be highly appreciated.

        Thanks!

        J Offline
        J Offline
        JonB
        wrote on 2 Dec 2021, 09:26 last edited by JonB 12 Feb 2021, 19:51
        #2

        @Swati777999
        You are already creating the number of widgets in the for loop. Just presumably do not create a new QTableWidget nor new QGridLayout within the loop: you don't want these newed each time, do you, you want to create them outside the loop and add the new widgets to a fixed QTableWidget/QGridLayout ? Or maybe your QTableWidget is the widget you want to create, you don't say. But why you want a new layout each time is beyond me.

        // how to input the value for 'n'

        However you want. Prompt the user to enter the n number in a dialog box? Obtain it from, say, a QSpinBox widget somewhere? We don't know.

        QTableWidget *myTable[jj]=new QTableWidget();

        This is not legal C++. You cannot declare a variable which is an indexed reference into an array.

        It's difficult to discern what your actual question is.

        S 2 Replies Last reply 3 Dec 2021, 01:34
        4
        • J JonB
          2 Dec 2021, 09:26

          @Swati777999
          You are already creating the number of widgets in the for loop. Just presumably do not create a new QTableWidget nor new QGridLayout within the loop: you don't want these newed each time, do you, you want to create them outside the loop and add the new widgets to a fixed QTableWidget/QGridLayout ? Or maybe your QTableWidget is the widget you want to create, you don't say. But why you want a new layout each time is beyond me.

          // how to input the value for 'n'

          However you want. Prompt the user to enter the n number in a dialog box? Obtain it from, say, a QSpinBox widget somewhere? We don't know.

          QTableWidget *myTable[jj]=new QTableWidget();

          This is not legal C++. You cannot declare a variable which is an indexed reference into an array.

          It's difficult to discern what your actual question is.

          S Offline
          S Offline
          Swati777999
          wrote on 3 Dec 2021, 01:34 last edited by
          #3

          @JonB

          In C++, for inputting values, we write

          int a;
          cout<<"Enter the value for a" ;
          cin>> a;

          Is there any equivalent code of it in Qt? (Somewhat similar in look/appearance)

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

          P 1 Reply Last reply 3 Dec 2021, 02:48
          0
          • S Swati777999
            3 Dec 2021, 01:34

            @JonB

            In C++, for inputting values, we write

            int a;
            cout<<"Enter the value for a" ;
            cin>> a;

            Is there any equivalent code of it in Qt? (Somewhat similar in look/appearance)

            P Offline
            P Offline
            Pl45m4
            wrote on 3 Dec 2021, 02:48 last edited by Pl45m4 12 Mar 2021, 02:52
            #4

            @Swati777999

            Qt is a GUI framework for C++ (and python). There is no Qt programming language.

            If you have a GUI, use a QLineEdit or something similar for your user inputs.
            To promt the user to enter some input, you can use
            QInputDialog
            (Note: It will show a dialog window)

            • https://doc.qt.io/qt-5/qinputdialog.html#details

            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            S 1 Reply Last reply 3 Dec 2021, 03:23
            1
            • P Pl45m4
              3 Dec 2021, 02:48

              @Swati777999

              Qt is a GUI framework for C++ (and python). There is no Qt programming language.

              If you have a GUI, use a QLineEdit or something similar for your user inputs.
              To promt the user to enter some input, you can use
              QInputDialog
              (Note: It will show a dialog window)

              • https://doc.qt.io/qt-5/qinputdialog.html#details
              S Offline
              S Offline
              Swati777999
              wrote on 3 Dec 2021, 03:23 last edited by
              #5

              @Pl45m4

              Thanks for your response.

              I want to add an array of Tables to the main window . How can I do that?

              My approach is as follows:

              QWidget *myWidget = new QWidget();
              QGridLayout *tableLayout = new QGridLayout();
              int n = 6;
                    QList <QTableWidget*> myTables;
                    for(int ii=0;ii<n;ii++)
                    { myTables.append(new QTableWidget());
                      tableLayout->addWidgets(myTables,ii,0);
                    }
              
              

              The following line gives me an error as pointed out by @JonB :

               tableLayout->addWidgets(myTables,ii,0);
              

              How can it be achieved?

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

              1 Reply Last reply
              0
              • J JonB
                2 Dec 2021, 09:26

                @Swati777999
                You are already creating the number of widgets in the for loop. Just presumably do not create a new QTableWidget nor new QGridLayout within the loop: you don't want these newed each time, do you, you want to create them outside the loop and add the new widgets to a fixed QTableWidget/QGridLayout ? Or maybe your QTableWidget is the widget you want to create, you don't say. But why you want a new layout each time is beyond me.

                // how to input the value for 'n'

                However you want. Prompt the user to enter the n number in a dialog box? Obtain it from, say, a QSpinBox widget somewhere? We don't know.

                QTableWidget *myTable[jj]=new QTableWidget();

                This is not legal C++. You cannot declare a variable which is an indexed reference into an array.

                It's difficult to discern what your actual question is.

                S Offline
                S Offline
                Swati777999
                wrote on 3 Dec 2021, 04:00 last edited by
                #6

                @JonB

                 int n=7;
                    QMap<int, QWidget *>myTables;
                    for (int ii=0;ii<n;ii++)
                    {  myTables[ii]=new QTableWidget();
                       tableLayout->addWidget(myTables[ii],ii,0);
                    }
                

                I got the array implementation using QMap from the above code. Yaay !!

                Now, I've to do the resizing part.

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

                T 1 Reply Last reply 3 Dec 2021, 04:14
                0
                • S Swati777999
                  3 Dec 2021, 04:00

                  @JonB

                   int n=7;
                      QMap<int, QWidget *>myTables;
                      for (int ii=0;ii<n;ii++)
                      {  myTables[ii]=new QTableWidget();
                         tableLayout->addWidget(myTables[ii],ii,0);
                      }
                  

                  I got the array implementation using QMap from the above code. Yaay !!

                  Now, I've to do the resizing part.

                  T Offline
                  T Offline
                  Thank You
                  wrote on 3 Dec 2021, 04:14 last edited by
                  #7

                  @Swati777999
                  If it is solved then why don't you mark it as solved.

                  Let's make QT free or It will go forever

                  TRUE AND FALSE <3

                  S 1 Reply Last reply 3 Dec 2021, 04:18
                  1
                  • T Thank You
                    3 Dec 2021, 04:14

                    @Swati777999
                    If it is solved then why don't you mark it as solved.

                    S Offline
                    S Offline
                    Swati777999
                    wrote on 3 Dec 2021, 04:18 last edited by
                    #8

                    @Thank-You
                    I've shared my solution and marked it as the best answer. Thanks.. :)

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

                    1 Reply Last reply
                    0

                    7/8

                    3 Dec 2021, 04:14

                    • Login

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