Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9082907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:39:45+00:00 2026-06-16T20:39:45+00:00

Well, I have a text editor with a button above it… I want the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:39:46+00:00Added an answer on June 16, 2026 at 8:39 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have a free text-editor that works well in Windows 7? I'm currently
I have an online text editor, very well made however it does not currently
Well, I have a UITextField. Inside it is a property UITextField.text. Is it ok
I have a text like: I've got a date with this fellow tomorrow. Well
Well, here is what I wanna do: I have a text field and a
I have a well div, and I would like to attach a small text/image
Well i have a div tag that i show as a modal dialog this
well I have that problem, im using a lightbox srcipt and im opening an
I have been working on a simple text editor in Cocoa/Objective-C for a practice
I'm masochistically writing an open-source text editor for Mac and have finally reached the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.