Nikolas
Nikolas

Reputation: 70

Issue with QTableView widget

I am having an issue with splitting the screen size between a QTableView widget and a QWebEngine widget which are both sharing a QHorizontalLayout.

Here is a screenshot: enter image description here

As you can see, the QTableView is taking up SO much space and giving almost nothing to the QWebEngineView.

Here is the relevant code:

def init_pdf_view(self) -> None:
        self.pdf_view = WEB.QWebEngineView()
        self.pdf_view.setObjectName("pdf_view")    
        self.load_file("/path/to/pdf")

        self.horizontalLayout_2.addWidget(self.pdf_view)
        self.horizontalLayout_2.setStretch(1, 1)

def init_table_view(self, sheet) -> None:
        if not self.table:
            self.table = QtWidgets.QTableWidget(self.centralwidget)
            self.table.setObjectName("table")
            self.horizontalLayout_2.insertWidget(0, self.table)
        else:
            self.table.clear()
        self.load_input_sheet(sheet)

def load_input_sheet(self, file) -> None:
        wb = xlrd.open_workbook(file, formatting_info=True)
        df = pd.read_excel(wb)
        self.input_sheet = df[df.columns[:3]]
        self.input_instructions = df[df.columns[3:]]
        self.input_sheet.columns = ["", *self.input_sheet.columns[1:]]
        self.input_sheet = self.input_sheet.T.reset_index().T.reset_index(drop=True)
        self.input_sheet.fillna('', inplace=True)

        self.prepare_table()
        self.populate_Table(wb)

I have tried moving the self.horizontalLayout_2.setStretch(1, 1) to the init_table_view method or even have it in both, but that just switches the sizing so that the QWebEngine takes up all the space instead of the QTableView.

Additionally, I would like to make it so that the first column of the table in the table view always shows no matter what and that the rest of the columns will either stretch to fit (up to half the size of the screen) or be horizontally scrollable if too many columns.

I appreciate any help and let me know if you need more information to adequately answer my problem.

Upvotes: 0

Views: 82

Answers (1)

Refugnic Eternium
Refugnic Eternium

Reputation: 4291

Your use of setStrecht is incorrect. setStretch takes two parameters, the first one being the index, the second one being the 'weight' of the component identified by the index.

This means, that by calling setStretch(1, 1) you are giving the widget at position '1' (second in line) a weight of '1', with all others 'unstretched' widgets having a weight of '0'.

This tells the layout manager to prioritize the widget at position '1' when it comes to distributing the size.

Hence there are two different approaches to solve your problem:

  • Don't use stretch at all (the layout manager will then give all widgets the same space)
  • Apply stretch to both of your widgets. This will make the layout manager divide all available space between the chosen widgets and shrinking all other widgets to their minimum size.

In this context, it's a good idea to apply the stretch only after all items have been added to the layout, so the indices have actually been set. Please note that the indices refer to the order the items were added to the layout manager.

Assuming you want to stretch both widgets, your code would look something like this:

def init_pdf_view(self) -> None:
        self.pdf_view = WEB.QWebEngineView()
        self.pdf_view.setObjectName("pdf_view")    
        self.load_file("/path/to/pdf")

        self.horizontalLayout_2.addWidget(self.pdf_view)

def init_table_view(self, sheet) -> None:
        if not self.table:
            self.table = QtWidgets.QTableWidget(self.centralwidget)
            self.table.setObjectName("table")
            self.horizontalLayout_2.insertWidget(0, self.table)
        else:
            self.table.clear()
        self.load_input_sheet(sheet)

#Somewhere in your main
init_pdf_view(self)
init_table_view(self)

self.horizontalLayout_2.setStretch(0, 1)
self.horizontalLayout_2.setStretch(1, 1)

Upvotes: 1

Related Questions