Carlj901
Carlj901

Reputation: 1411

Create tab with UI programmatically

This is my UI:

enter image description here

This is tab created programmatically:

ui->tabWidget->addTab(new QWidget(), "Tab 2");

enter image description here

I want the newly created tab to have the exact same layout as the "Chat room" tab has. Any advice on how to do this would be great.

Upvotes: 2

Views: 1157

Answers (3)

Phlucious
Phlucious

Reputation: 3843

The easiest way to do this (in Designer) is to create a new UI Form Class called something like ChatTab and base it on QWidget.

Move (i.e., cut and paste) your chat room widgets and layout from your MainWindow UI form to the ChatTab form, but leave the QTabWidget container in your MainWindow form. If you want a chat room to be in your main window when it first opens, promote its first QWidget to a ChatTab from inside Designer.

Any additional tabs should be added programatically like so:

ui->tabWidget->addTab(new ChatTab(), "Tab 2");

Add fancy stuff to the ChatTab constructor if you want to make your life easier.

Upvotes: 3

helsinki
helsinki

Reputation: 763

Use your current chatRoom class as a base class and do some layout stuff in it;

Next time you want to create a new tab, just do something like:

ui->tabWidget->addTab(new chatRoom(), "Tab 3");

Hope to help.

Upvotes: 0

linjunhalida
linjunhalida

Reputation: 4655

You should create a custom QTabWidget for this layout, as name ChatWidget, and make a factory API like this:

ChatWidget * ChatWidget::creater(TabWidget * tw, ChatData * cd) {
    ChatWidget * cw = ChatWidget.create();
    tw.addTab(cast<QWidget>cw, 0);
    // some init
    ...
    return cw;
}

Long time not use Qt and C++, may be this give you a hint.

Upvotes: 1

Related Questions