OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

PyQt6 - allow streching for a particular widget [duplicate]

  • Thread starter Thread starter spam me
  • Start date Start date
S

spam me

Guest
I'm a newbie to PyQt6. My problem is, I would like to have a window with different tabs and in the first tab "tab_1" the space is divided into 2 columns. The 1st column contains some options (for example filter, boxes etc.) and this column width should remain unchanged with the window size. The 2nd column inhibits a tabular and the number of visible columns differs with the window size.

Code:
import sys

from PyQt6 import QtWidgets, QtGui
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPalette, QColor


class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()
        self.setWindowTitle("My App")

        self.tabs = QtWidgets.QTabWidget()
        self.tabs.setTabPosition(QtWidgets.QTabWidget.TabPosition.North)
        self.tabs.setMovable(False)

        # Tab 1
        self.tab_1 = QtWidgets.QWidget()
        self.tabs.addTab(self.tab_1, "tab_1")
        self.tab_1_func()


        self.setCentralWidget(self.tabs)

    def tab_1_func(self):
        """ Overview """
        
        grid_layout = QtWidgets.QGridLayout()
        page_layout = QtWidgets.QHBoxLayout()

        self.tab_1.stacklayout = QtWidgets.QStackedLayout()
       
        page_layout.addLayout(grid_layout)
        page_layout.addLayout(self.tab_1.stacklayout)
        #page_layout.addStretch(0)
        #page_layout.insertStretch(1,0)
        
        #
        # Left Column - Buttons, fitlers etc
        #

        widget_box = QtWidgets.QComboBox()
        widget_box.addItems(['Country', 'Capital', 'Street'])
        grid_layout.addWidget(widget_box, 0, 0)
        
        # Widget 2: Line Input for Filter
        widget_filter = QtWidgets.QLineEdit()
        widget_filter.setPlaceholderText("Enter your text")
        grid_layout.addWidget(widget_filter, 0, 1)

        #grid_layout.setColumnMinimumWidth(1,300)
        grid_layout.setColumnStretch(3,1)

        
        # Widget 3: Data
        data = [
            ('France', 'Paris', 'Rue de X'),
            ('United Kingdom', 'London', 'Road X'),
            ('Italy', 'Rome', 'Strade X'),
            ('Germany', 'Berlin', 'Straße X')
        ]
        
        self.tableWidget = QtWidgets.QTableWidget(self)
        N_row = 4
        self.tableWidget.setRowCount(N_row) 

        N_column = 3
        self.tableWidget.setColumnCount(N_column)
        self.tableWidget.setHorizontalHeaderLabels(['Country', 'Capital', 'Street'])
        for row in range(N_row):
            for column in range(N_column):
                item = QtWidgets.QTableWidgetItem(data[row][column])
                self.tableWidget.setItem(row, column, item)

        # add the widget and connecting widgets together directly
        page_layout.addWidget(self.tableWidget)
        page_layout.addStretch(1)


        
        self.tab_1.setLayout(page_layout)

Currently, the only option I found to fix the 1st column is to set page_layout.addStretch(1) near the end. But this fix the size of tabular as well. It would be great to get a hint, what's my error here. Thanks.
<p>I'm a newbie to PyQt6. My problem is, I would like to have a window with different tabs and in the first tab "tab_1" the space is divided into 2 columns. The 1st column contains some options (for example filter, boxes etc.) and this column width should remain unchanged with the window size. The 2nd column inhibits a tabular and the number of visible columns differs with the window size.</p>
<pre><code>import sys

from PyQt6 import QtWidgets, QtGui
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPalette, QColor


class MainWindow(QtWidgets.QMainWindow):

def __init__(self):
super().__init__()
self.setWindowTitle("My App")

self.tabs = QtWidgets.QTabWidget()
self.tabs.setTabPosition(QtWidgets.QTabWidget.TabPosition.North)
self.tabs.setMovable(False)

# Tab 1
self.tab_1 = QtWidgets.QWidget()
self.tabs.addTab(self.tab_1, "tab_1")
self.tab_1_func()


self.setCentralWidget(self.tabs)

def tab_1_func(self):
""" Overview """

grid_layout = QtWidgets.QGridLayout()
page_layout = QtWidgets.QHBoxLayout()

self.tab_1.stacklayout = QtWidgets.QStackedLayout()

page_layout.addLayout(grid_layout)
page_layout.addLayout(self.tab_1.stacklayout)
#page_layout.addStretch(0)
#page_layout.insertStretch(1,0)

#
# Left Column - Buttons, fitlers etc
#

widget_box = QtWidgets.QComboBox()
widget_box.addItems(['Country', 'Capital', 'Street'])
grid_layout.addWidget(widget_box, 0, 0)

# Widget 2: Line Input for Filter
widget_filter = QtWidgets.QLineEdit()
widget_filter.setPlaceholderText("Enter your text")
grid_layout.addWidget(widget_filter, 0, 1)

#grid_layout.setColumnMinimumWidth(1,300)
grid_layout.setColumnStretch(3,1)


# Widget 3: Data
data = [
('France', 'Paris', 'Rue de X'),
('United Kingdom', 'London', 'Road X'),
('Italy', 'Rome', 'Strade X'),
('Germany', 'Berlin', 'Straße X')
]

self.tableWidget = QtWidgets.QTableWidget(self)
N_row = 4
self.tableWidget.setRowCount(N_row)

N_column = 3
self.tableWidget.setColumnCount(N_column)
self.tableWidget.setHorizontalHeaderLabels(['Country', 'Capital', 'Street'])
for row in range(N_row):
for column in range(N_column):
item = QtWidgets.QTableWidgetItem(data[row][column])
self.tableWidget.setItem(row, column, item)

# add the widget and connecting widgets together directly
page_layout.addWidget(self.tableWidget)
page_layout.addStretch(1)



self.tab_1.setLayout(page_layout)
</code></pre>
<p>Currently, the only option I found to fix the 1st column is to set <code>page_layout.addStretch(1)</code> near the end. But this fix the size of tabular as well. It would be great to get a hint, what's my error here. Thanks.</p>
 

Latest posts

J
Replies
0
Views
1
Jeff P Chacko
J
Top