Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Move label in QVBoxLayout()



  • Hello everyone, i am adding widgets (labels) to QVBoxLayout() is any way to organise them inside QVBoxLayout()? Move to position i need. When i try to use move or resize it is not working.


  • Lifetime Qt Champion

    Hi,

    If you want to reorder them your can use replaceWidget and then insertItem with the item returned by replaceWidget. Or takeAt.



  • @Samuel-Bachorik
    Not sure whether you mean what @SGaist has answered --- i.e. you are talking about inserting & removing the labels within the QVBoxLayout --- or whether you are talking about actually moving labels to a particular position, which you should be doing via adding sub-layouts/stretches/alignment etc.?



  • I am talking about moving my labels to exact position.

    As you can see here, these labels are wide as my QVBoxLayout() or Qwidget. How can i make these labels smaller?
    Also how can i move this labels from left side of window. I dont understand how this is wokring. Simply i add label and i can not do anything with it ? Iam leaving also code.

    2818c5ed-43ef-4096-a0d3-586a159c2411-image.png

    from PyQt5.QtCore import Qt, QSize
    from PyQt5 import QtWidgets, uic
    import sys
    from PyQt5.QtGui import QIcon, QPixmap
    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import *
    import sys
    
    
    
    class MainWindow(QMainWindow):
    
        def __init__(self):
            super().__init__()
            self.setGeometry(200, 250, 900, 600)
            self.setWindowTitle("Hello")
    
            self.scroll = QScrollArea()
            self.scroll.setFixedWidth(350)
            self.scroll.setFixedHeight(500)
            self.widget = QWidget()
            self.widget.setFixedWidth(350)
    
            self.vbox = QVBoxLayout()
    
    
            self.namer2 = ["a","b","c","d","e","b","c","d","e","b","c","d","e"]
            self.counter2 = 0
    
            for i in range(50):
                self.namer2[self.counter2] = QLabel("Hello  \nall\nguys")  # AS YOU CAN SEE HERE IAM ADDING LABEL BUT HOW TO MOVE AND MAKE IT SMALLER ? 
                self.namer2[self.counter2].setStyleSheet("background-color: lightgreen")
                self.namer2[self.counter2].resize(20, 50)
                self.namer2[self.counter2].setFont(QFont('Arial', 12))
    
                self.vbox.addWidget(self.namer2[self.counter2])
                self.vbox.addSpacing(65)
    
    
            self.widget.setLayout(self.vbox)
    
            #Scroll Area Properties
            self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
            self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
            self.scroll.setWidgetResizable(True)
            self.scroll.setWidget(self.widget)
    
    
    
            self.setCentralWidget(self.scroll)
    
    
            self.show()
    
    
    
    def main():
        app = QtWidgets.QApplication(sys.argv)
        main = MainWindow()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    

  • Lifetime Qt Champion

    You can set their maximum size.

    What exactly are you after ?



  • Iam sorry i think iam not using right tools to achieve what i need.


  • Lifetime Qt Champion

    @Samuel-Bachorik said in Move label in QVBoxLayout():

    Iam sorry i think iam not using right tools to achieve what i need.

    Maybe, maybe not. If you explain what you are trying to achieve, then we might be able to assist a bit more.



  • @SGaist I am trying to make something like chat app. I made this in photoshop just o show how i want this to looks like. Labels like this. Of course text on them. And scroll down and up. These labels should be messages. I just can not fit something like this in QScrollArea().

    b8bb92bf-0594-4d4f-8602-03e3faf97788-image.png


  • Lifetime Qt Champion

    In that case you should rather use a QListView and a custom QStyledItemDelegate.



  • Thank you i will try tomorrow soon as possible.


Log in to reply