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 965 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.
  • Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on 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

    JonBJ 1 Reply Last reply
    0
    • JonBJ JonB

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

      Swati777999S Offline
      Swati777999S Offline
      Swati777999
      wrote on 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

      Thank YouT 1 Reply Last reply
      0
      • Swati777999S Swati777999

        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!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #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.

        Swati777999S 2 Replies Last reply
        4
        • JonBJ JonB

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

          Swati777999S Offline
          Swati777999S Offline
          Swati777999
          wrote on 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

          Pl45m4P 1 Reply Last reply
          0
          • Swati777999S Swati777999

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

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #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

            Swati777999S 1 Reply Last reply
            1
            • Pl45m4P Pl45m4

              @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
              Swati777999S Offline
              Swati777999S Offline
              Swati777999
              wrote on 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
              • JonBJ JonB

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

                Swati777999S Offline
                Swati777999S Offline
                Swati777999
                wrote on 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

                Thank YouT 1 Reply Last reply
                0
                • Swati777999S Swati777999

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

                  Thank YouT Offline
                  Thank YouT Offline
                  Thank You
                  wrote on 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

                  Swati777999S 1 Reply Last reply
                  1
                  • Thank YouT Thank You

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

                    Swati777999S Offline
                    Swati777999S Offline
                    Swati777999
                    wrote on 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

                    • Login

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