Question

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.

Was it helpful?

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

OTHER TIPS

Is there a way that I can subclass a QTab and just reuse that class for each tab

Yes you can subclass QTabWidget. Nothing prevent you for doing that. But if it is only to setup the UI, I usually do

QLayout* setupViewLayout()
{
   //add bunch of stuff to layout
}

QTabWidget * tabwidget = new QTabWidget ;
QLayout* tablayout = setupViewLayout();
QWidget* widget = new QWidget;

widget->setLayout(tablayout );
tabwidget->addTab(widget);

And I dont need to create one class for each kind of view I want.

Is there anyway I can do a similar sort of thing with QML for each subclass?

Yes, you have qt quick designer for QML and Qt designer for creating ui files which will compile as c/c++. You can create graphically some template UI which provide an initial setup and then add specific customization in the codeIt can be handful if only few sub-widgets change between different tabs. I did something similar with a UI which was tab- based too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top