Reputation: 45
I have 2 .ui files created using Qt Designer
main_window.ui
- class QMainWindow (name MyWindow) containing only 1 QTabWidgetmy_widget.ui
- class QWidget (name MyWidget) containing a QLabel and QPushButton eachI have a configuration file which reads something like this
ProcessA Tab1 ; ProcessB Tab2 ; ProcessC Tab1
As clear from above, I need A and C to be in Tab1 and B in Tab2. I have been able to create the desired tabs. I want to create instances of the 2nd UI and populate the tabs.
I have been able to use QPushButton class instead of the MyWidget Class and got the following output (which is correct)
When I try to use the MyWidget Class, I end up getting blank tabs.
Overview of code -
MyWindow::setupTabs(std::string fileName)
{
Read file
for each process in file:
MyWidget *temp = new MyWidget();
Fill text in label and button
Create std::vector<std::vector<MyWidget*> > and fill according to tabs
for each tab call populateTab()
}
MyWindow::populateTab(processList, tabName)
{
QFrame* group = new QFrame();
QVBoxLayout* vbox = new QVBoxLayout();
vbox->setSpacing(0);
group->setLayout(vbox);
QScrollArea* scrollArea = new QScrollArea();
for (size_t i = 0; i < processList.size(); ++i)
vbox->addWidget(processList[i]); //vbox->addWidget(new QPushButton); works fine
scrollArea->setWidget(group);
ui->tabWidget->addTab(scrollArea, tabName);
}
The setupTabs function performs exactly as desired and thus I have mentioned the code very briefly.
MyWidget.h looks like this
namespace Ui {
class MyWidget;
}
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget();
private:
Ui::MyWidget *ui;
};
Please let me know what I am doing wrong.
Upvotes: 0
Views: 1204
Reputation:
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
Upvotes: 1