【问题标题】:PyQt widget absolute position inside QVBoxLayoutPyQt 小部件在 QVBoxLayout 内的绝对位置
【发布时间】:2019-02-24 12:54:09
【问题描述】:

我正在做一些 PyQt5 项目,我来自 Web 开发,我坚持做一些琐碎的任务。 我有一些 QVBoxLayout 并在其中放置 QWidget 只是为了通过 CSS 添加一些背景颜色。之后我想放一些图片,self.img 就在 QWidget 的中心。但我无法管理如何做到这一点。 它只是将我的图像呈现在 QWidget 的下方,仅此而已。

我尝试使用 move(x,y) 方法,尝试使用 QWidget 的背景图像但我失败了。所以我真的坚持了下来。

我试图寻找一些可能的方法来解决它,但我没有找到任何可以帮助我的方法。 如果有人可以帮助我,我将非常感谢您的任何意见。感谢您的帮助。

说实话 - 我是 PyQt 的新手。对不起,如果我问了一些愚蠢的问题,但我真的需要帮助。

这是我的代码

import sys

from PyQt5.QtCore import QPoint
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QPixmap



class MainWindow(QWidget):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.layout  = QVBoxLayout()
        self.layout.addWidget(TopContentBlock(self))
        self.layout.addWidget(BottomContentBlock(self))
        self.setLayout(self.layout)
        self.layout.setContentsMargins(0,0,0,0)
        self.layout.setSpacing(0)
        self.layout.addStretch(-1)
        self.setMinimumSize(640,400)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.pressing = False


class TopContentBlock(QWidget):

    def __init__(self, parent):
        super(TopContentBlock, self).__init__();
        self.parent = parent;
        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)

        self.content = QWidget()
        self.content.setFixedSize(640, 250)
        self.content.setStyleSheet("""
         background-color: #67BEC3;
        """)

        self.img = QLabel()
        pixmap = QPixmap('main_illustration.png')
        self.img.setPixmap(pixmap)
        print(pixmap.width(), pixmap.height())
        self.img.resize(pixmap.width(), pixmap.height())
        #self.img.setFixedSize(pixmap.width(), pixmap.height())

        self.layout.addWidget(self.img)
        self.layout.addWidget(self.content)
        self.setLayout(self.layout)


class BottomContentBlock(QWidget):

    def __init__(self, parent):
        super(BottomContentBlock, self).__init__();
        self.parent = parent;
        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)

        self.content = QWidget()
        self.content.setFixedSize(640, 400)
        self.content.setStyleSheet("""
            background-color: cyan;
        """)
        self.layout.addWidget(self.content)
        self.setLayout(self.layout)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())


import resources_new

【问题讨论】:

    标签: python user-interface pyqt5


    【解决方案1】:

    试试看:

    import sys
    from PyQt5.QtCore    import QPoint, Qt
    from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QLabel,
                                 QPushButton, QVBoxLayout, QWidget)
    from PyQt5.QtGui     import QPixmap
    
    
    class MainWindow(QWidget):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.layout  = QVBoxLayout()
            self.layout.addWidget(TopContentBlock(self))
            self.layout.addWidget(BottomContentBlock(self))
            self.setLayout(self.layout)
            self.layout.setContentsMargins(0,0,0,0)
            self.layout.setSpacing(0)
    #        self.layout.addStretch(1)
            self.setMinimumSize(640,600)                         # 600
            self.setWindowFlags(Qt.FramelessWindowHint)
    #?        self.pressing = False
    
    
    class TopContentBlock(QWidget):
        def __init__(self, parent):
            super(TopContentBlock, self).__init__();
    #?        self.parent = parent;
            self.layout = QVBoxLayout()
            self.layout.setContentsMargins(0, 0, 0, 0)
    
            self.content = QWidget()
            self.content.setFixedSize(640, 200)                  #(640, 250)
            self.content.setStyleSheet("""
             background-color: #67BEC3;
            """)
    
            self.img = QLabel()
            self.img.setAlignment(Qt.AlignCenter)                # +++
    
            pixmap = QPixmap('im.png')                           #('main_illustration.png')
    #        self.img.setPixmap(pixmap)
            self.img.setPixmap(pixmap.scaled(200, 200,           # +++
                                             Qt.IgnoreAspectRatio, 
                                             Qt.FastTransformation))
    #        self.img.resize(pixmap.width(), pixmap.height())
            #self.img.setFixedSize(pixmap.width(), pixmap.height())
    
            self.layout.addWidget(self.img)
            self.layout.addWidget(self.content)
            self.layout.setSpacing(0)                            # +++
            self.setLayout(self.layout)
    
    
    class BottomContentBlock(QWidget):
        def __init__(self, parent):
            super(BottomContentBlock, self).__init__();
            self.parent = parent;
            self.layout = QVBoxLayout()
            self.layout.setContentsMargins(0, 0, 0, 0)
    
            self.content = QWidget()
            self.content.setFixedSize(640, 200)               #(640, 400)
            self.content.setStyleSheet("""
                background-color: cyan;
            """)
            self.layout.addWidget(self.content)
            self.setLayout(self.layout)
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        mw = MainWindow()
        mw.show()
        sys.exit(app.exec_())
    


    更新

    import sys
    from PyQt5.QtCore    import QPoint, Qt
    from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QLabel,
                                 QPushButton, QVBoxLayout, QWidget)
    from PyQt5.QtGui     import QPixmap
    
    
    class MainWindow(QWidget):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.layout  = QVBoxLayout()
            self.layout.addWidget(TopContentBlock(self))
            self.layout.addWidget(BottomContentBlock(self))
            self.setLayout(self.layout)
            self.layout.setContentsMargins(0,0,0,0)
            self.layout.setSpacing(0)
    #        self.layout.addStretch(1)
            self.setMinimumSize(640,400)                         # 400
            self.setWindowFlags(Qt.FramelessWindowHint)
    #?        self.pressing = False
    
    
    class TopContentBlock(QWidget):
        def __init__(self, parent):
            super(TopContentBlock, self).__init__();
    #?        self.parent = parent;
            self.layout = QVBoxLayout()
            self.layout.setContentsMargins(0, 0, 0, 0)
    
    #        self.content = QWidget()                            # --- <---
            self.setFixedSize(640, 200)                          # --- .content              
            self.setStyleSheet("""                               
             background-color: #67BEC3;
            """)                                                 # --- .content 
    
            self.img = QLabel()
            self.img.setAlignment(Qt.AlignCenter)                # +++
    
            pixmap = QPixmap('im.png')                           #('main_illustration.png')
    #        self.img.setPixmap(pixmap)
            self.img.setPixmap(pixmap.scaled(200, 200,           # +++
                                             Qt.IgnoreAspectRatio, 
                                             Qt.FastTransformation))
    #        self.img.resize(pixmap.width(), pixmap.height())
            #self.img.setFixedSize(pixmap.width(), pixmap.height())
    
            self.layout.addWidget(self.img)
    #        self.layout.addWidget(self.content)                 # ---
            self.layout.setSpacing(0)                            # +++
            self.setLayout(self.layout)
    
    
    class BottomContentBlock(QWidget):
        def __init__(self, parent):
            super(BottomContentBlock, self).__init__();
            self.parent = parent;
            self.layout = QVBoxLayout()
            self.layout.setContentsMargins(0, 0, 0, 0)
    
            self.content = QWidget()
            self.content.setFixedSize(640, 200)               #(640, 400)
            self.content.setStyleSheet("""
                background-color: cyan;
            """)
            self.layout.addWidget(self.content)
            self.setLayout(self.layout)
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        mw = MainWindow()
        mw.show()
        sys.exit(app.exec_())
    

    【讨论】:

    • 嗨,S.Nick。谢谢您的帮助。但是图像行为并不像预期的那样。它呈现具有 #67BEC3 bg 颜色的 Widget 上方。我想把这张图片放在彩色小部件的中心。现在这个图像就像白色背景的独立第一行一样。有没有办法让 2 行并将图像放在第一行的中心?
    • 谢谢尼克!它就像一个魅力。我不知道有可能将样式设置为 VBoxLayout。顺便说一句,一个问题:这是否意味着在 PyQt 中我们不能将我们的 QWidgets 绝对定位在其他 QWidgets 中?
    • Qt 布局系统提供了一种简单而强大的方式来自动排列小部件内的子小部件,以确保它们充分利用可用空间。