Important: Please read the Qt Code of Conduct -

Multiple Widget Containers

  • I'm relatively new to Qt and C++ programming, I've got the framework of what a program I want to build looks like, however I'm wondering if it's possible to create a number of widget containers on the go? ie If I tell it to create 4 it will do that for me, without having to recompile the program each time?

    I'd like to load a CSV file, read the number of columns and create that number of widget containers to display different data.

    It's for a race tracking program, each column will be a different competitor and show their current laptimes and previous laptimes

    Thanks for the help :)

  • Hi and welcome!

    Sure you can, the ideal container would most probably be QVector, in which you would store pointers on your widgets.

    QVector< QWidget* > container;
    for ( unsigned i( 0u ); i < N; ++i )
        container.append( new MyWidget() );

    Assuming N is either provided via command-line argument of deduced from the CSV file you're referring to.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Since you want to show things in columns, what about using a QTableWidget or QTableView + a model ?

  • Thanks for the suggestions guys, will have an investigation this afternoon when I get on my computer.

    Would either way work with displaying different information?

    I'm trying to visualise how it will give each a different name. Is it possible to append each widget name with a number? If I call the widget bikeinfo, Say bikeinfo1 for bike 1, bikeinfo2 for bike 2 etc?

  • Lifetime Qt Champion


    Hi both ways would work with different information.
    It is possible to give each widget a name with number if needed.

    But if you are aiming for something that would be a table then
    it is far more work to create a widget for each column than to
    use a QTableWidget as sgaist suggests.

        ui->table->setRowCount ( 1 );
        ui->table->setColumnCount ( 3 );
        ui->table->setSizePolicy ( QSizePolicy::Expanding, QSizePolicy::Expanding );
        ui->table->setHorizontalHeaderLabels ( QString ( "Name;Track Time;Prev Track Time" ).split ( ";" ) );
        //Add Table items here
        ui->table->setItem ( 0, 0, new QTableWidgetItem ( "Michael Schumacher" ) );
        ui->table->setItem ( 0, 1, new QTableWidgetItem ( "22.55" ) );
        ui->table->setItem ( 0, 2, new QTableWidgetItem ( "44.3" ) ); 

Log in to reply