Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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!



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



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



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



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



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



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



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



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


Log in to reply