2

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.

TheMan68
  • 1,429
  • 6
  • 26
  • 48

2 Answers2

4

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

Anton Poznyakovskiy
  • 2,109
  • 1
  • 20
  • 38
3

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.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • Thank you very much for your help. I'm very new to Qt Quick Designer lol, but i am progressing in qt fast. AS far as the tabs i really think they need to be sub classed for eacch tab so it would be something like: QTabWidget->addTab(new CustomTabClass()); as there is lots of things that will be happening in that class for each tab. for eg. each of the tabs will have their own QThread class that is handing populating the chat window. All of these things would be done a lot easier if it was sub classed, but once again I'm open to suggestions as I've never made an app to this level before. – TheMan68 Jan 21 '14 at 13:35
  • CustomTabClass can be a subclass of QWidget. But you dont need to subclass QTabWidget – UmNyobe Jan 21 '14 at 15:35
  • thank you. after reading your answer i tryed it and it works like a charm. – TheMan68 Jan 22 '14 at 07:44