0

In "Rapid GUI Programming with Python and Qt", the author gives this example in Chapter 4.

It seems to me that dial, spinbox, and layout would go out of scope. Why can we use dial instead of self.dial, spinbox instead of self.spinbox and layout instead of self.layout? Is this a good practice or is it better to use self. for all GUI elements?

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

class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        dial = QDial()
        dial.setNotchesVisible(True)
        spinbox = QSpinBox()

        layout = QHBoxLayout()
        layout.addWidget(dial)
        layout.addWidget(spinbox)
        self.setLayout(layout)

        self.connect(dial, SIGNAL("valueChanged(int)"),
                 spinbox.setValue)
        self.connect(spinbox, SIGNAL("valueChanged(int)"),
                 dial.setValue)
        self.setWindowTitle("Signals and Slots")

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

1 Answer 1

1

Yes, those variables go out of scope.

But since there are references to the underlying objects (via layout and self.setLayout) this is not really an issue, i.e. the widgets/objects are not destroyed.

If you don't need to access them directly in other pieces of your code, then it's just fine like that.

Sign up to request clarification or add additional context in comments.

2 Comments

So in general, if there are references to underlying objects, those underlying objects will go out of scope when the object referring to them goes out of scope?Is that how this works in Python?
Yeah, if you delete the layout then the widgets will be removed from memory too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.