Velidan
Velidan

Reputation: 6004

PyQt widget absolute position inside QVBoxLayout

I'm working on some PyQt5 project, I came from Web Development and I stuck with some trivial task. I have some QVBoxLayout and inside it I placed QWidget just to add it some background color via CSS. After that I wanted to put some image, self.img just right at the center of QWidget. But I couldn't manage how to do it. It just renders my Image below of the QWidget nad that's all.

I tried to use move(x,y) method, tried to use background image for QWidget but I failed. So I really stuck with it.

I tried to search some possible ways to solve it but I didn't found anything which could help me. If someone could help me I'll be very appreciate for any input. Thanks for any help.

To be honest - I'm new in PyQt. Sorry if I asked some dummy question but I really need help.

Here is my code

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

Upvotes: 1

Views: 3529

Answers (1)

S. Nick
S. Nick

Reputation: 13701

Try it:

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_())

enter image description here


Update

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_())

enter image description here

Upvotes: 1

Related Questions