Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I find this a little more complicated than just using <code>layout.setAlignment()</code>. It kept not working for me until just now, when I figured out that if you have expanding widgets that you set a maximum height for, then that widget will not be aligned the way you want. </p> <p>Here is example code that does <em>not</em> top align the <code>QTextBrowser()</code> widget even though I call <code>layout.setAlignment(Qt.AlignTop)</code>. Sorry that it is in Python, but it is pretty easy to translate to C++ (I have gone the other way many times).</p> <pre><code>from PyQt4.QtCore import * from PyQt4.QtGui import * class MyWidget(QWidget): """ Create a widget that aligns its contents to the top. """ def __init__(self, parent=None): QWidget.__init__(self, parent) layout = QVBoxLayout() label = QLabel('label:') layout.addWidget(label) info = QTextBrowser(self) info.setMinimumHeight(100) info.setMaximumHeight(200) layout.addWidget(info) # Uncomment the next line to get this to align top. # layout.setAlignment(info, Qt.AlignTop) # Create a progress bar layout. button = QPushButton('Button 1') layout.addWidget(button) # This will align all the widgets to the top except # for the QTextBrowser() since it has a maximum size set. layout.setAlignment(Qt.AlignTop) self.setLayout(layout) if __name__ == '__main__': import sys app = QApplication(sys.argv) widget = MyWidget() widget.show() widget.resize(QSize(900, 400)) app.exec_() </code></pre> <p>The following explicitly calls <code>layout.setAlignment(info, Qt.AlignTop)</code> to get the expending text widget to work.</p> <pre><code>from PyQt4.QtCore import * from PyQt4.QtGui import * class MyWidget(QWidget): """ Create a widget that aligns its contents to the top. """ def __init__(self, parent=None): QWidget.__init__(self, parent) layout = QVBoxLayout() label = QLabel('label:') layout.addWidget(label) info = QTextBrowser(self) info.setMinimumHeight(100) info.setMaximumHeight(200) layout.addWidget(info) # Uncomment the next line to get this to align top. layout.setAlignment(info, Qt.AlignTop) # Create a progress bar layout. button = QPushButton('Button 1') layout.addWidget(button) # This will align all the widgets to the top except # for the QTextBrowser() since it has a maximum size set. layout.setAlignment(Qt.AlignTop) self.setLayout(layout) if __name__ == '__main__': import sys app = QApplication(sys.argv) widget = MyWidget() widget.show() widget.resize(QSize(900, 400)) app.exec_() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload