我试图将两个小部件集中在窗口中间,用于简单的漫画阅读器,例如书籍的页面展开,同时仍然将它们扩展到窗口的高度或宽度,并保持他们的宽高比。
以下是一些说明问题的示例代码。青色和洋红色框代表页面。
import sys
from PyQt5.QtGui import QImage, QPainter
from PyQt5.QtWidgets import QDialog, QHBoxLayout, QVBoxLayout, QPushButton, QSizePolicy
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtWidgets import QApplication
class page_viewer(QPushButton):
def __init__(self, parent=None, flags=None):
super(page_viewer, self).__init__(parent)
self.image = QImage()
self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)
def set_image(self, image=QImage()):
self.image = image
self.update()
def paintEvent(self, event):
painter = QPainter(self)
image = self.image.scaled(self.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
painter.drawImage(0, 0, image)
def sizeHint(self):
return QSize(4096, 4096)
class my_page_viewer(QDialog):
pageIndex = 0
spread = True
def __init__(self):
super().__init__()
self.setModal(False)
self.setWindowTitle('Untitled')
self.setWindowFlags(
Qt.WindowTitleHint |
Qt.WindowMinimizeButtonHint |
Qt.WindowMaximizeButtonHint |
Qt.WindowCloseButtonHint
)
self.setMinimumSize(256, 256)
self.setLayout(QVBoxLayout())
self.page_layout = QHBoxLayout()
self.layout().addLayout(self.page_layout)
#self.page_layout.addStretch(1)
self.left_viewer = page_viewer()
left_image = QImage(QSize(70, 100), QImage.Format_ARGB32)
left_image.fill(Qt.GlobalColor(10))
self.left_viewer.set_image(left_image)
self.page_layout.addWidget(self.left_viewer)
self.right_viewer = page_viewer()
right_image = QImage(QSize(70, 100), QImage.Format_ARGB32)
right_image.fill(Qt.GlobalColor(11))
self.right_viewer.set_image(right_image)
self.page_layout.addWidget(self.right_viewer)
#self.page_layout.addStretch(1)
if __name__ == '__main__':
app = QApplication(sys.argv)
page_viewer_dialog = my_page_viewer()
page_viewer_dialog.show()
sys.exit(app.exec_())
基本上,当你让窗户变宽时,我希望窗口中央的青色和洋红色盒子彼此相邻。现在的方式,青色盒子一直向左对齐,而洋红色盒子的左侧与窗口中心对齐。
答案 0 :(得分:0)