How to add my UI in new tab to qtabwidget?
-
import sys from PyQt5 import QtCore, QtGui, QtWidgets,uic class TestGUI(QtWidgets.QMainWindow): def __init__(self): super(TestGUI, self).__init__() self.myui = uic.loadUi('SPTMain.ui', self) self.NewTab.clicked.connect(self.handleAddTab) def handleAddTab(self): contents = QtWidgets.QWidget(self.myui) layout = QtWidgets.QVBoxLayout(contents) self.tabWidget.addTab(contents, 'Tab One') if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) window = TestGUI() window.show() sys.exit(app.exec_())
Actually It adds blank tab... Kindly guide me how can I add new tab as my ui (duplicate of my .ui file in new tab)
-
import sys from PyQt5 import QtCore, QtGui, QtWidgets,uic class TestGUI(QtWidgets.QMainWindow): def __init__(self): super(TestGUI, self).__init__() self.myui = uic.loadUi('SPTMain.ui', self) self.NewTab.clicked.connect(self.handleAddTab) def handleAddTab(self): contents = QtWidgets.QWidget(self.myui) layout = QtWidgets.QVBoxLayout(contents) self.tabWidget.addTab(contents, 'Tab One') if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) window = TestGUI() window.show() sys.exit(app.exec_())
Actually It adds blank tab... Kindly guide me how can I add new tab as my ui (duplicate of my .ui file in new tab)
@aadil50 said in How to add my UI in new tab to qtabwidget?:
contents = QtWidgets.QWidget(self.myui)
What is this supposed to do? It creates a new, empty
QWidget
withself.myui
as its parent, hence a "blank tab".I don't understand the architecture of your application. It has a
QMainWindow
, which is what youshow()
. Meanwhile that main window creates someself.myui
withSPTMain.ui
loaded into itself. Yet at the same time you want it loaded into some other widget to use for tab within the main window. -
So how can I improve it to achieve my task.. As you know i have shown what I want to do and what i have done.. Thanks...
@aadil50 said in How to add my UI in new tab to qtabwidget?:
As you know i have shown what I want to do
Maybe someone else understands what your
SPTMain.ui
is, whether it is the content for the main window or for some widget you want to add as a tab to somewhere, or what, and what your variable namedself.myui
is for --- but I don't. So hopefully someone else will answer you.[Maybe you should have no
contents = QtWidgets.QWidget(self.myui)
,self.myui
should be renamedself.tab1
and justself.tabWidget.addTab(self.tab1, 'Tab One')
. I don't know.] -
@aadil50 said in How to add my UI in new tab to qtabwidget?:
self.myui
It is my ui which contain multliple Widgets.. I just want to show it in my tab.. By inheriting my class of ui and adding to addtab funtion hangs and windows disappear.. So I have done all my tries what i can and know.. But still cannot get idea how can i show my ui in new tab aka duplicate my ui in new tab...
-
@aadil50 said in How to add my UI in new tab to qtabwidget?:
self.myui
It is my ui which contain multliple Widgets.. I just want to show it in my tab.. By inheriting my class of ui and adding to addtab funtion hangs and windows disappear.. So I have done all my tries what i can and know.. But still cannot get idea how can i show my ui in new tab aka duplicate my ui in new tab...
-
@aadil50 said in How to add my UI in new tab to qtabwidget?:
self.myui
It is my ui which contain multliple Widgets.. I just want to show it in my tab.. By inheriting my class of ui and adding to addtab funtion hangs and windows disappear.. So I have done all my tries what i can and know.. But still cannot get idea how can i show my ui in new tab aka duplicate my ui in new tab...
@aadil50 said in How to add my UI in new tab to qtabwidget?:
By inheriting my class of ui and adding to addtab funtion hangs and windows disappear..
So did you try what I suggested?
Yes....
This happens.. Kindly can you please explain your code.. maybe i am interpreting it wrong.. Or should i try something else you will suggest..[I am new user so cannot post reply under 600 seconds aka 10 minutes.. No reputation yet ]
-
@aadil50 said in How to add my UI in new tab to qtabwidget?:
By inheriting my class of ui and adding to addtab funtion hangs and windows disappear..
So did you try what I suggested?
Yes....
This happens.. Kindly can you please explain your code.. maybe i am interpreting it wrong.. Or should i try something else you will suggest..[I am new user so cannot post reply under 600 seconds aka 10 minutes.. No reputation yet ]
@aadil50 said in How to add my UI in new tab to qtabwidget?:
Kindly can you please explain your code
contents = QtWidgets.QWidget(self.myui)
You do not want this, I explained what it does earlier.self.myui
should be renamedself.tab1
The.ui
file seems to describe the content of a tab, so it's a name which means something.self.tabWidget.addTab(self.tab1, 'Tab One')
Intended to addself.tab1
as a tab.
I'm a little confused by two different patterns I see for
uic.loadUi()
, which is a PyQt-only thing. Try this:# in `__init__()` self.tab1 = uic.loadUi('SPTMain.ui') # in `handleAddTab()` self.tabWidget.addTab(self.tab1, 'Tab One')
-
@aadil50
The following works for me. I had to hack together a couple of things (including changing from main window to a generic widget) since I do not have (nor want!) whatever you have.import sys from PyQt5 import QtCore, QtGui, QtWidgets,uic class TestGUI(QtWidgets.QWidget): def __init__(self): super(TestGUI, self).__init__() layout = QtWidgets.QVBoxLayout(self) self.NewTab = QtWidgets.QPushButton("Create Tab", self) layout.addWidget(self.NewTab) self.tabWidget = QtWidgets.QTabWidget(self) layout.addWidget(self.tabWidget) self.tab1 = uic.loadUi('/path/to/somefile.ui') self.NewTab.clicked.connect(self.handleAddTab) def handleAddTab(self): self.tabWidget.addTab(self.tab1, 'Tab One') if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) window = TestGUI() window.show() sys.exit(app.exec_())