Reputation: 2319
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?
Upvotes: 1
Views: 13653
Reputation: 101929
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 a QVBoxLayout
.
Anyway, as I mentioned in a comment, you can probably obtain your desired "output" using a QHBoxLayout
and the addStretch
methods:
import sys
from PyQt4 import QtCore, QtGui
class TextEditorDlg(QtGui.QDialog):
def __init__(self, parent=None):
super(TextEditorDlg, self).__init__(parent)
self.resize(500, 400)
self.button = QtGui.QPushButton(self)
self.lineEdit = QtGui.QLineEdit(self)
self.textEdit = QtGui.QTextEdit(self)
self.button_layout = QtGui.QHBoxLayout()
self.button_layout.addStretch()
self.button_layout.addWidget(self.button)
self.button_layout.addStretch()
self.grid = QtGui.QGridLayout(self)
self.grid.addLayout(self.button_layout, 0, 0)
self.grid.addWidget(self.lineEdit, 1, 0)
self.grid.addWidget(self.textEdit, 2, 0)
# Alternative using QVBoxLayout:
#self.layout = QtGui.QVBoxLayout(self)
#self.layout.addLayout(self.button_layout)
#self.layout.addWidget(self.line_edit)
#self.layout.addWidget(self.text_edit)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = TextEditorDlg()
myapp.show()
sys.exit(app.exec_())
You can tune the stretch amount passing an integer to addStretch
.
If you wanted to use a QGridLayout
to place the button in a specific position, then you should have done something like this:
import sys
from PyQt4 import QtCore, QtGui
class TextEditorDlg(QtGui.QDialog):
def __init__(self, parent=None):
super(TextEditorDlg, self).__init__(parent)
self.resize(500, 400)
self.button = QtGui.QPushButton(self)
self.lineEdit = QtGui.QLineEdit(self)
self.textEdit = QtGui.QTextEdit(self)
self.grid = QtGui.QGridLayout(self)
# Use the row-span and column-span arguments!
self.grid.addWidget(self.button, 0, 0, 1, 1)
self.grid.addWidget(self.lineEdit, 1, 0, 1, 3)
self.grid.addWidget(self.textEdit, 2, 0, 1, 3)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = TextEditorDlg()
myapp.show()
sys.exit(app.exec_())
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 QPushButton
you must change the QSizePolicy
of the button and the policy of the QTextEdit
. For example:
import sys
from PyQt4 import QtCore, QtGui
class TextEditorDlg(QtGui.QDialog):
def __init__(self, parent=None):
super(TextEditorDlg, self).__init__(parent)
self.resize(500, 400)
self.button = QtGui.QPushButton(self)
self.button.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Expanding)
self.lineEdit = QtGui.QLineEdit(self)
self.textEdit = QtGui.QTextEdit(self)
self.textEdit.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Minimum)
self.grid = QtGui.QGridLayout(self)
# Use the row-span and column-span arguments!
self.grid.addWidget(self.button, 0, 0, 3, 1)
self.grid.addWidget(self.lineEdit, 3, 0, 1, 3)
self.grid.addWidget(self.textEdit, 4, 0, 1, 3)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = TextEditorDlg()
myapp.show()
sys.exit(app.exec_())
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 the 3
will increase/decrease the vertical size(actually the number of rows occupied by the widget) and increasing/decreasing the 1
will change the horizontal size.
Note that if you place the widget at row 0
spanning for 3
rows then you have to be carefully to not create collisions adding new widgets to the layout.
Upvotes: 8