Jordan Larson
Jordan Larson

Reputation: 29

Can I have a tab control in a widget contained in a layout?

I am trying to make a programmable sequencer where I can configure objects that I've programmed and create a list of them to run. That requires me to instantiate that object by defining it's properties and I'd like to make a tab control on the blue widget shown below that has an inputs and outputs tab. enter image description here

Anytime I try to implement this, it replaces all of the other widgets: enter image description here

Surely that has to be a way to use a standard tab control in a widget right?

Code below:

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGridLayout, QLabel, QMainWindow, QWidget, QTabWidget, QVBoxLayout
from PyQt5.QtGui import QColor, QPalette


# Placeholder widget for setting up layout
class Color(QWidget):
    def __init__(self, color):
        super().__init__()
        self.setAutoFillBackground(True)
        palette = self.palette()
        palette.setColor(QPalette.Window, QColor(color))
        self.setPalette(palette)


# Creating tab widgets
class MyTabWidget(QTabWidget):
    def __init__(self):
        super().__init__()
        self.setTabPosition(QTabWidget.North)
        for n, color in enumerate(['blue', 'purple']):
            self.addTab(Color(color), color)



class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("My App")
        layout = QGridLayout()
        # 1
        layout.addWidget(Color("red"), 0, 0, 2, 1)
        # 2
        layout.addWidget(Color("yellow"), 2, 0, 3, 1)
        # 3
        layout.addWidget(Color("green"), 0, 1, 5, 4)


        # 4
        # layout.addWidget(Color("blue"), 0, 5, 5, 1)
        tabs = MyTabWidget()
        layout.addWidget(tabs, 0, 5, 5, 1)

        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

Upvotes: 0

Views: 124

Answers (1)

eyllanesc
eyllanesc

Reputation: 244301

The size policy of the QTabWidget is set to stretch and since the stretch factors of the QGridLayout items is 0 then it will cause the items to be compressed. The solution is to set the stretch factors to 1:

widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)

for column in range(layout.columnCount()):
    layout.setColumnStretch(column, 1)
for row in range(layout.rowCount()):
    layout.setRowStretch(row, 1)

enter image description here

Upvotes: 2

Related Questions