maurobio maurobio - 3 months ago 21
Python Question

PyQt - Tabbed dialog not displaying embedded widgets

I am trying to create a dialog with three tabs using PyQt. However, I am annoyed because although the dialog is displayed, the embedded widgets are not displayed!. I suppose this is a very simple problem with a correspondingly very simple solution, but I am struck! Can anyone give a hint? Thanks in advance!

Here is my code so far:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class TabbedDialog(QDialog):
def __init__(self, parent = None):
super(TabbedDialog, self).__init__(parent)

self.tabWidget = QTabWidget()
self.tabWidget.tab1 = QWidget()
self.tabWidget.tab2 = QWidget()
self.tabWidget.tab3 = QWidget()

self.tabWidget.addTab(self.tabWidget.tab1,"Tab 1")
self.tabWidget.addTab(self.tabWidget.tab2,"Tab 2")
self.tabWidget.addTab(self.tabWidget.tab3,"Tab 3")
self.tab1UI()
self.tab2UI()
self.tab3UI()
self.setWindowTitle("tab demo")

def tab1UI(self):
layout = QFormLayout()
layout.addRow("Name",QLineEdit())
layout.addRow("Address",QLineEdit())
self.tabWidget.setTabText(0,"Contact Details")
self.tabWidget.tab1.setLayout(layout)

def tab2UI(self):
layout = QFormLayout()
sex = QHBoxLayout()
sex.addWidget(QRadioButton("Male"))
sex.addWidget(QRadioButton("Female"))
layout.addRow(QLabel("Sex"),sex)
layout.addRow("Date of Birth",QLineEdit())
self.tabWidget.setTabText(1,"Personal Details")
self.tabWidget.tab2.setLayout(layout)

def tab3UI(self):
layout = QHBoxLayout()
layout.addWidget(QLabel("subjects"))
layout.addWidget(QCheckBox("Physics"))
layout.addWidget(QCheckBox("Maths"))
self.tabWidget.setTabText(2,"Education Details")
self.tabWidget.tab3.setLayout(layout)

if __name__ == '__main__':
app = QApplication(sys.argv)
form = TabbedDialog()
retval = form.exec_()

Answer

here is my solution to the problem

On the init method, I declared a layout, then added the 'tabWidget' widget to that layout and set that layout as the layout of your QDialog.

def __init__(self, parent = None):
  super(TabbedDialog, self).__init__(parent)

  self.tabWidget = QTabWidget()
  self.tabWidget.tab1 = QWidget()
  self.tabWidget.tab2 = QWidget()
  self.tabWidget.tab3 = QWidget()

  self.tabWidget.addTab(self.tabWidget.tab1,"Tab 1")
  self.tabWidget.addTab(self.tabWidget.tab2,"Tab 2")
  self.tabWidget.addTab(self.tabWidget.tab3,"Tab 3")
  self.tab1UI()
  self.tab2UI()
  self.tab3UI()
  self.setWindowTitle("tab demo")

  # Here is the addition to the code.
  mainLayout = QVBoxLayout()
  mainLayout.addWidget(self.tabWidget)
  self.setLayout(mainLayout)