Adding a .ui file to TabWidget



  • Hey all,

    My program I am working on has two .ui files. The first of these (browser.ui) creates a layout with a TabWidget and a button. When I click the button, it adds another tab to the TabWidget. I would like the new tab to be filled with my second ui file (TabContent.ui). How can I do this?

    code:
    browser.cpp:
    @#include "browser.h"
    #include "ui_browser.h"
    #include "ui_TabContent.h"

    browser::browser(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::browser)
    {
    ui->setupUi(this);
    }

    browser::~browser()
    {
    delete ui;
    }

    void browser::on_NewTabButton_clicked()
    {
    !!NEEDS TO BE FILLED WITH TABCONTENT.UI!!

    }
    @



  • If I understand what you said, do the following:

    1. Go to QTCreator
    2. In toolbar click in "File"
    3. See: File > New File or Project (or Ctrl+N)
    4. See: Chosse a template > Files and classes > C++ > C++ Class
    5. Now will open a window named "C++ Class Wizard".
    6. In "Class Name:" enter with text: "MyCustomWidget"
    7. In "Base class:" enter with text: "QTabWidget" (or outher widget type)
    8. Click in "Next" > click in "Finish".
    9. 3 files were created: mycustomwidget.h, mycustomwidget.cpp, mycustomwidget.cpp (you must have all 3 files so you can work with other classes)

    your browser.cpp should look like:

    @#include "browser.h"
    #include "ui_browser.h"
    #include "ui_TabContent.h"

    browser::browser(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::browser)
    {
    ui->setupUi(this);
    }

    browser::~browser()
    {
    delete ui;
    }

    void browser::on_NewTabButton_clicked()
    {
    MyCustomWidget *a = new MyCustomWidget(this);//or new MyCustomWidget;
    ui->centralWidget->layout()->addWidget(a);
    }@

    bq. Note: centralWidget is a standard widget in all projects created from scratch


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.