I am trying to adapt a project, which is normally displayed on an external monitor, to my laptop monitor. As you can see from the diagram below, there are 40 rows in the main table. When working from my laptop, I cannot see the last 10-15 rows and the two tables below. Using a user's suggestion ( [https://stackoverflow.com/questions/34637099/python-qt-automatically-resizing-main-window-to-fit-content][1]), I can resize the tables but not the label or the pushbuttons on the right.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow
import sys
class MainApp(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.theApp(self)
def theApp(self, MainWindow):
self.original_width = 1100
self.original_height = 810
MainWindow.setGeometry(200, 50, 1100, 810)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget_w = 1001 ####<--------
self.tableWidget_h = 441 ####<--------
self.tableWidget.setGeometry(QtCore.QRect(20, 20, self.tableWidget_w, self.tableWidget_h))
self.tableWidget.setRowCount(40)
self.tableWidget.setColumnCount(34)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.horizontalHeader().setVisible(True)
self.tableWidget.horizontalHeader().setDefaultSectionSize(30)
self.tableWidget.verticalHeader().setVisible(True)
self.tableWidget.verticalHeader().setDefaultSectionSize(25)
self.tableWidget_2_w = 1001 ####<--------
self.tableWidget_2_h = 81 ####<--------
self.tableWidget_2 = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget_2.setGeometry(QtCore.QRect(20, 480, self.tableWidget_2_w, self.tableWidget_2_h))
self.tableWidget_2.setRowCount(4)
self.tableWidget_2.setColumnCount(32)
self.tableWidget_2.setObjectName("tableWidget_2")
self.tableWidget_2.horizontalHeader().setVisible(False)
self.tableWidget_2.horizontalHeader().setDefaultSectionSize(30)
self.tableWidget_2.verticalHeader().setVisible(False)
self.tableWidget_2.verticalHeader().setDefaultSectionSize(19)
self.tableWidget_2.verticalHeader().setMinimumSectionSize(15)
self.tableWidget_3_w = 971 ####<--------
self.tableWidget_3_h = 81 ####<--------
self.tableWidget_3 = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget_3.setGeometry(QtCore.QRect(50, 570, self.tableWidget_3_w, self.tableWidget_3_h))
self.tableWidget_3.setRowCount(4)
self.tableWidget_3.setColumnCount(32)
self.tableWidget_3.setObjectName("tableWidget_3")
self.tableWidget_3.horizontalHeader().setVisible(False)
self.tableWidget_3.horizontalHeader().setDefaultSectionSize(30)
self.tableWidget_3.verticalHeader().setVisible(False)
self.tableWidget_3.verticalHeader().setDefaultSectionSize(19)
self.tableWidget_3.verticalHeader().setMinimumSectionSize(15)
self.label_w = 60 ####<--------
self.label_h = 15 ####<--------
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(1030, 10, self.label_w, self.label_h))
self.label.setText("Label")
self.label.setObjectName("label")
self.pushButton_w = 60 ####<--------
self.pushButton_h = 23 ####<--------
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(1030, 40, self.pushButton_w, self.pushButton_h))
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("Push")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(1030, 70, 60, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(1030, 100, 60, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(1030, 130, 60, 23))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(1030, 160, 60, 23))
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_6.setGeometry(QtCore.QRect(1030, 190, 60, 23))
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_7.setGeometry(QtCore.QRect(1030, 220, 60, 23))
self.pushButton_7.setObjectName("pushButton_7")
MainWindow.setCentralWidget(self.centralwidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.show()
def resizeEvent(self, event):
width = self.frameGeometry().width()
height = self.frameGeometry().height()
w_proportion = width / self.original_width
h_proportion = height / self.original_height
self.tableWidget.resize(int(self.tableWidget_w*w_proportion), int(self.tableWidget_h*h_proportion))
self.tableWidget_2.resize(int(self.tableWidget_2_w*w_proportion), int(self.tableWidget_2_h*h_proportion))
self.tableWidget_3.resize(int(self.tableWidget_3_w*w_proportion), int(self.tableWidget_3_h*h_proportion))
self.label.resize(int(self.label_w*w_proportion), int(self.label_h*h_proportion))
self.pushButton.resize(int(self.pushButton_w*w_proportion), int(self.pushButton_h*h_proportion))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MainApp()
sys.exit(app.exec_())
[1]: https:///https://stackoverflow.com/questions/34637099/python-qt-automatically-resizing-main-window-to-fit-content
You only resize the size of the widgets (label
, pushButton
...) in resizeEvent
, but do not change their position (move()
). Therefore, when the window shrinks, the old position of the buttons remains the same => causing them to slide out.
You need to resize and reposition (move) the widgets to the new ratio.
I have updated resizeEvent() function:
def resizeEvent(self, event):
width = self.frameGeometry().width()
height = self.frameGeometry().height()
w_proportion = width / self.original_width
h_proportion = height / self.original_height
# Resize table widgets
self.tableWidget.setGeometry(
QtCore.QRect(
int(20 * w_proportion),
int(20 * h_proportion),
int(self.tableWidget_w * w_proportion),
int(self.tableWidget_h * h_proportion)
)
)
self.tableWidget_2.setGeometry(
QtCore.QRect(
int(20 * w_proportion),
int(480 * h_proportion),
int(self.tableWidget_2_w * w_proportion),
int(self.tableWidget_2_h * h_proportion)
)
)
self.tableWidget_3.setGeometry(
QtCore.QRect(
int(50 * w_proportion),
int(570 * h_proportion),
int(self.tableWidget_3_w * w_proportion),
int(self.tableWidget_3_h * h_proportion)
)
)
# Resize + reposition label
self.label.setGeometry(
QtCore.QRect(
int(1030 * w_proportion),
int(10 * h_proportion),
int(self.label_w * w_proportion),
int(self.label_h * h_proportion)
)
)
# Resize + reposition các pushButton
for i, btn in enumerate([
self.pushButton, self.pushButton_2, self.pushButton_3,
self.pushButton_4, self.pushButton_5, self.pushButton_6,
self.pushButton_7
]):
btn.setGeometry(
QtCore.QRect(
int(1030 * w_proportion),
int((40 + i*30) * h_proportion),
int(self.pushButton_w * w_proportion),
int(self.pushButton_h * h_proportion)
)
)