I have a python gui in qt. the gui is buit using designer. It contains tabs. I want to add a plot.
My plot object is a class (called MyPlot) that inherits from QtCore.QObject. To add it to the gui, I can simply do inside my class MainWindow that inherits from QtGui.QMainWindow:
self.Myplot(self)
self.MyPlot.plot()
But I don’t know how to tell python to put the plot only in one tab. I suppose I have to do inside MainWindow something like:
self.tab(0).Myplot(self)
But I can’t find exactly what.
Someone has any clue?
Thanks!
EDIT:
Trying to have a tab that reads an ui file generated by designer in the following code fails (I am just trying to put in a tab the things I build with designer, not to add a plot yet):
import sys
from PyQt4 import QtCore, QtGui, uic
class MyWidget(QtGui.QWidget):
def __init__(self, parent = None):
uic.loadUi('mainwindow.ui', self)
class TabWidget(QtGui.QTabWidget):
def __init__(self, parent=None):
super (TabWidget, self).__init__(parent)
self.setTabsClosable(True)
self.tabCloseRequested.connect(self.removeTab)
self.inside = MyWidget()
def tabInserted(self, index):
self.tabBar().setVisible(self.count() > 1)
def tabRemoved(self, index):
self.tabBar().setVisible(self.count() > 1)
class MainWindow(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QMainWindow.__init__(self, parent)
def main():
qApp = QtGui.QApplication(sys.argv)
tab = TabWidget()
button = QtGui.QPushButton('Hello')
@button.clicked.connect
def clicked():
tab.addTab(QtGui.QLabel('Hello'), 'Hello')
tab.addTab(button, 'Button')
layout = QtGui.QHBoxLayout()
layout.addWidget(tab)
window = QtGui.QWidget()
window.setLayout(layout)
window.resize(600, 400)
window.show()
qApp.exec_()
if __name__ == "__main__":
main()
The normal way to use
QTabWidgetis to do the following:QTabWidget.QWidgetfor each of the pages in the tab dialog, but do not specify parent widgets for them.addTab()orinsertTab()to put the page widgets into the tab widget, giving each tab a suitable label with an optional keyboard shortcut.http://qt-project.org/doc/qt-5.0/qtabwidget.html#details
UPDATE:
I created a file
untitled.uiin Qt Designer with a single checkbox. I also added call to parent__init__inMyWidget:And it works.