A
A
Arthur2019-11-21 12:53:22
Qt
Arthur, 2019-11-21 12:53:22

How to add multiple QTablewidgets to a QSrollArea?

When adding multiple QTablewidgets to a QSrollArea , the QSrollArea was expected to have scrollbars and the QTablewidgets would not shrink, but instead the QTablewidget instances shrink and have scrollbars on each. How to fix it?
Here is the minimal code in which the error is repeated:
compiled main_window.iu:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1389, 955)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
        self.scrollArea.setGeometry(QtCore.QRect(10, 10, 451, 881))
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 449, 879))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.verticalLayoutWidget = QtWidgets.QWidget(self.scrollAreaWidgetContents)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 20, 421, 851))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", " Sender"))

Main file:
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import Qt
from main_window import Ui_MainWindow  # импорт нашего сгенерированного файла
import sys


class MySender(QtWidgets.QMainWindow):
    def __init__(self):
        super(MySender, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        for i in range(7):
            self.add_table_widget()

    def add_table_widget(self):
        self.rowNum = 7
        # создаем QTableWidget
        tableWidget = QtWidgets.QTableWidget(self)
        tableWidget.setHorizontalHeaderLabels(["field","value","tuning"])
        tableWidget.setColumnCount(3)
        tableWidget.setRowCount(self.rowNum)

        # формируем и добавляем элементы в QTableWidget
        for n, signal in enumerate(['sig1','sig2','sig3','sig4','sig5','sig6','sig7']):

            tableWidget.setItem(n,0,QtWidgets.QTableWidgetItem(signal))
            qsignal_value = QtWidgets.QTableWidgetItem()
            qsignal_value.setData(Qt.EditRole,0)
            tableWidget.setItem(n,1,qsignal_value)

            btn = QtWidgets.QPushButton(tableWidget)
            btn.setText("tune")
            tableWidget.setCellWidget(n, 2, btn)

        tableWidget.resizeColumnsToContents()
        self.ui.verticalLayout.addWidget(tableWidget)


app = QtWidgets.QApplication([])
application = MySender()
application.show()

sys.exit(app.exec())

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question