user2234234
user2234234

Reputation: 349

widgets are not expandig according to window size

what is mistake in this code that prevents widgets from expanding according to window size ?

class FeedbackWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.main_layout = QVBoxLayout(self)
        self.main_widget = QWidget(self)
        self.main_widget.setLayout(self.main_layout)
        self.title_label = QLabel("Tell us what you think:")
        self.feedback_text_editor = QTextEdit()
        self.send_button = QPushButton("Send")

        self.main_layout.addWidget(self.title_label)
        self.main_layout.addWidget(self.feedback_text_editor)
        self.main_layout.addWidget(self.send_button)

        self.setWindowTitle("Feedback")
        self.setGeometry(200,120,300,300)

    if __name__ == "__main__":

        app = QApplication(sys.argv)
        w = FeedbackWindow()
        w.show()
        app.exec_()

the main layout and widget are connected to self, so it should take its dimension.

Upvotes: 0

Views: 59

Answers (2)

falsetru
falsetru

Reputation: 368894

The code does not use self.main_widget. Remove self.main_widget:

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class FeedbackWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.main_layout = QVBoxLayout(self)
        #self.main_widget = QWidget(self)              # main_widget is not used.
        #self.main_widget.setLayout(self.main_layout)
        self.setLayout(self.main_layout)
        self.title_label = QLabel("Tell us what you think:")
        self.feedback_text_editor = QTextEdit()
        self.send_button = QPushButton("Send")

        self.main_layout.addWidget(self.title_label)
        self.main_layout.addWidget(self.feedback_text_editor)
        self.main_layout.addWidget(self.send_button)

        self.setWindowTitle("Feedback")
        self.setGeometry(200,120,300,300)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = FeedbackWindow()
    w.show()
    app.exec_()

Upvotes: 1

Remove

self.main_widget = QWidget(self)
self.main_widget.setLayout(self.main_layout)

You don't need them. In your implementation, the layout is set on self.main_widget which is NOT the main widget. Your main widget is your FeedbackWindows itself. When you call self.main_layout = QVBoxLayout(self), it implicitely apply the layout on the main widget.

Upvotes: 1

Related Questions