Search code examples
qtdynamicqmlqtabwidgetqt5.1

dynamically adding a tab qt 5.1


I am having a difficult time figuring out how i should do this. I have a QWidget main window and its has a QTabWidget inside a VBoxLayout. The main function of the window is to hold the chats of people who are chatting with our software.

So what I would need is to be able to add tabs dynamically when people open a new chat. for example, if I'm chatting with person A and I click on person B from my chat list then if the window is opened then add another tab to the QTabWidget in the window, If not opened then just create a new window.

Now in each tab is quite a complicated layout as I have smileys and many other various things to deal with so I would rather not write all that in code.

Is there a way that I can subclass a QTab and just reuse that class for each tab? And second question: I come from an android background in programming so I am used to being able to have a reusable XML layout for each instance of the tab subclass(in this case) , is there anyway I can do a similar sort of thing with QML for each subclass?

If I am going about this the complete wrong way I would really appreciate the help.


Solution

  • You don't need to subclass QTab for the sake of adding it. QTabWidget has a convenient method int QTabWidget::addTab(QWidget * page, const QString & label). You can add your widgets to QTabWidget directly.

    Link to description: http://qt-project.org/doc/qt-5/qtabwidget.html#addTab