1
votes

I am trying to build a GUI which will:

  1. Load a file with parameters which describe certain type of problem.
  2. Based on the parameters of the file, show only certain tab in QTabwidget (of many predefined in Qt Designer .ui)

I plan to make a QTabwidget with, say 10 tabs, but only one should be visible based on the parameters loaded. Enabling certain tab is not an option since it takes to many space and the disabled tabs are grey. I do not want to see disabled tabs.

Removing tab could be an option but the index is not related to a specific tab so I have to take care of the shift in the indices. And furthermore if user loads another file with different parameters, a good tab should be added and the current one removed.

My questions are:

  1. How to do this effectively?
  2. Is it better to use any other type of widget?
  3. In Qt designer, is it possible to define many widgets one over another and then just push the good one in front. If yes, how? And how to edit and change any of them?
  4. If using RemoveTab, how to use pointers on tabs, rather than indices?

I use PyQt4

2
Can you just create a single QTabwidget and add/remove tabs as needed? You seem to be asking if this is possible and yes it is certainly possible. You should read about QTabwidgets here pyqt.sourceforge.net/Docs/PyQt4/qtabwidget.html. You can insert/remove tabs with the insertTab() and removeTab() methods. - derricw
@ ballsdotballs: Thanks for sharing the link. Normally I study all that I can found before asking a question. From my post it is evident that I considered adding and removing as a non elegant solution due to indices and I asked for advice how to save a pointer on removed tab for adding later. - Stjepan
If you only want one tab visible at a time, and the user never see other tabs, why use a tabwidget at all? Why not just have N different widgets and only show the one you want the user to see. Or @ekhumoro's solution below, which separates the widgets into pages. But you still have to add/remove pages, which you don't seem like you want to do. - derricw
I choose Qtabwidget because it was easy to edit tabs one by one: I would like to avoid tabwidget but I do not know how to make (or edit) overlapped widgets in Qt designer and show in front the one I want: - Stjepan

2 Answers

5
votes

Use a QStackedWidget, which is exactly the same as a tab-widget, but without the tab-bar (which you don't need).

This widget is available in Qt Designer. The context menu has several commands for adding/removing pages and so forth. Note that the arrow buttons in the top-right corner are just there for convenience: they won't appear in your application.

Pages can be added/removed at runtime with addWidget/removeWidget:

index = self.stack.addWidget(self.page1)
self.stack.removeWidget(self.page1)

You can access the pages using either indexes or widget references.

0
votes

I see that this thread is kinda old. But I hope this will still help. You can use the remove() method to "hide" the tab. There's no way to really hide them in pyqt4. when you remove it, it's gone from the ui. But in the back end, the tab object with all your settings still exist. I'm sure you can find a way to improvise it back. Give it a try!