Stjepan
Stjepan

Reputation: 111

PyQT Qtabwidget add, remove, hide, show certain tab

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

Upvotes: 1

Views: 9084

Answers (2)

Lumi Wang
Lumi Wang

Reputation: 73

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!

Upvotes: 0

ekhumoro
ekhumoro

Reputation: 120578

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.

Upvotes: 5

Related Questions