2

I'm working with a QTabWidget right now displaying just one QWidget with different elements (labels, buttons, ...). Working with Ubuntu and Qt5.5.

QTabWidget *tw;
QString title = "1";
ui->tw->addTab(&tab, title); // tab is my QWidget

I'd like to show the same QWidget in more than one tab with different values. Is there a "clean" for it?

Micha

MichaW.
  • 37
  • 5

1 Answers1

1

No, there is not a "clean" way to do that. The QTabWidget uses a stack, so you will need to have separate widgets for each tab. It is said in the documentation :

Each tab is associated with a different widget (called a page).

The only way is to instantiate several instances of QWidget and add them to your QTabWidget.

QTabWidget *tw;
QString title  = "1";
QString title2 = "2";
ui->tw->addTab(&tab, title);   // tab is your QWidget
ui->tw->addTab(&tab2, title2); // tab2 is another QWidget

If you want to use a QTabBar, just put your widget within it (using a QVBoxLayout for example). Then connect to the QTabBar's currentChanged signal to change your widget according to your needs.

IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
  • Thanks for your quick reply. Too bad that I can't just insert the same widget several times.. Do you have any experiences regarding a QTabBar instead of QTabWidget? – MichaW. Jun 03 '16 at 06:15
  • Do you have an example you could share? I'm not completely sure how to simply work with the QTabBar.. :/ – MichaW. Jun 03 '16 at 10:01
  • Did it! Thanks so much for your help. Couldn't think of an easier solution though. :) Marked as solved! – MichaW. Jun 06 '16 at 06:38