13

How can I set just Widget size?

My code:

from PySide.QtGui import QApplication, QWidget, QLabel
import sys
app = QApplication(sys.argv)

mainWindow = QWidget()
gameWidget = QWidget(mainWindow)

#gameWidget.setGeometry(gameWidth, gameHeight) <-- I want to set size of gameWidget such like this. How can I do this.
gameWidget.move(100,0)
gameLabel = QLabel("This is game widget", gameWidget)

mainWindow.show()

Output:

enter image description here

Description:

This will create Window that contain Widget. I want to set this Widget size. I know There is a method Widget.setGeometry, but it takes 4 parameter (x, y, width, height). I want a method like Widget.setGeometry which takes just size parameter (width, height).

P.S. Feel free to modify my question. Because I'm always learning English!!

Thanks.

1
  • Depending on what you want to have in the end using a layout (QLayout derived) might be a very comfortable way to automatically have widgets of the right size. Commented Jun 30, 2014 at 13:57

1 Answer 1

21

Just use QWidget.resize(width, height). This is equivalent to QWidget.resize(PySide.QtCore.QSize(width, height)).

For example:

gameLabel.resize(200, 100);

Besides, you can also use QWidget.sizeHint() to get a proper size automatically, for example:

gameLabel.resize(gameLabel.sizeHint());
2
  • 1
    It worked. And Additional info about sizeHint seems very helpful. Thanks so much. Commented Jun 26, 2014 at 4:24
  • 2
    To add to @Tay2510's answer. You could use SizePolicy.
    – Shadow9043
    Commented Jun 26, 2014 at 14:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.