Well, I have a text editor with a button above it… I want the text editor to be inside a layout so that it increases and decreases its size the the QDialog, but I don’t want to do that same with the button, which, by the way, doesn’t have the size that I want and can’t change it. What can I do?
Code:
from PyQt4 import QtCore, QtGui
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class TextEditorDlg(QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.resize(500, 400)
self.Grid = QtGui.QGridLayout(self)
self.button = QtGui.QPushButton(self)
self.Grid.addWidget(self.button, 0, 0)
self.lineEdit = QtGui.QLineEdit(self)
self.textEdit = QtGui.QTextEdit(self)
self.Grid.addWidget(self.lineEdit, 1, 0)
self.Grid.addWidget(self.textEdit, 2, 0)
if __name__ == '__main__':
app = QApplication(sys.argv)
myapp = TextEditorDlg()
myapp.show()
sys.exit(app.exec_())
See the problem now?
First of all I do not understand why you are using a
QGridLayout. Do you realize that you are not using the grid at all? You should simply use aQVBoxLayout.Anyway, as I mentioned in a comment, you can probably obtain your desired “output” using a
QHBoxLayoutand theaddStretchmethods:You can tune the stretch amount passing an integer to
addStretch.If you wanted to use a
QGridLayoutto place the button in a specific position, then you should have done something like this:You can place the button exactly where you want using the correct row and column indexes and row-span and column-span values.
Edit: If you want to change the size of the
QPushButtonyou must change theQSizePolicyof the button and the policy of theQTextEdit. For example:You’ll see that the button is bigger. You can control how big by changing the row-span.
In the line
self.grid.addWidget(self.button, 0, 0, 3, 1)increasing/decreasing the3will increase/decrease the vertical size(actually the number of rows occupied by the widget) and increasing/decreasing the1will change the horizontal size.Note that if you place the widget at row
0spanning for3rows then you have to be carefully to not create collisions adding new widgets to the layout.