It's a bit hard to tell from your pseudo code, but it seems like you're always adding the same widgets (MyWidget) to the layouts. Is your last tab containing the widgets, but the others don't?
Best regards
Minimal example:
mainwindow.hpp
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_HPP
mainwindow.cpp (just a QMainWindow containing a single QTabWidget named tabWidget)
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include "form.hpp"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tabWidget->clear(); // Clear the two Tabs that are shown by default
QStringList fileEntries(QStringList() << "widget1" << "widget2" << "widget3" << "widget4"); // A QStringList simulating the file entries
int count = 1;
foreach (QString entry, fileEntries) { // Loop through all the entries and add the custom widget on the go
Form *myWidget = new Form(this);
myWidget->set(entry + "Label", entry + "Button");
ui->tabWidget->addTab(myWidget, "New Tab " + QString::number(count++));
}
}
MainWindow::~MainWindow()
{
delete ui;
}
form.hpp (should represent your MyWidget). It contains a QLabel and a QPushButton in a QVBoxLayout:
#ifndef FORM_HPP
#define FORM_HPP
#include <QWidget>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = 0);
~Form();
void set(QString const&labelText, QString const&buttonText);
private:
Ui::Form *ui;
};
#endif // FORM_HPP
form.cpp:
#include "form.hpp"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
void Form::set(const QString &labelText, const QString &buttonText)
{
ui->label->setText(labelText);
ui->pushButton->setText(buttonText);
}
form.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
Hope that helps,
Best regards
Second edit, same example as above, but form.ui has no layout on the Form (QWidget) anymore (looks at least like the problem in your description):
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>737</width>
<height>523</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>480</x>
<y>350</y>
<width>160</width>
<height>80</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
Best regards