Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placeholder for a custom widget

I am loading a QMainWIndow base from a *.ui file. Also, i have a custom widget i want to place somewhere on the form. Currently i put in the .ui file an empty QVBoxLayout named placeholder, and in the QMainWindow subclass do self.placeholder.addWidget(my_custom_widget)

The only thing i don't like in this approach is that the empty layout does not have its own size. I can have a layout with one cell and with a dummy widget (QLabel for example) with the size i want, and replace this widget and then add my custom widget, but the method seems too much for me.

What is your approach for such a task?

I am using Python (PyQt4)

like image 627
warvariuc Avatar asked Jan 23 '13 13:01

warvariuc


1 Answers

Here is an easy little tutorial on how to promote a widget:

  1. Right click on the widget you are going to use as placeholder and select Promote To....

    Image Promote

  2. Fill in the Promoted Clases pop-up dialog fields:
    • Base Class Name: QWidget in this case.
    • Promoted Class Name: The class name you used to define the widget for which you are creating the placeholder, here it is MyWidget
    • Header File: /path/to/MyWidget.py is the path to the file in which MyWidget is placed. Image Path
  3. Once you click Add, the class is created and displayed, select it and click Promote. You are done promoting. Image Add
  4. Here is what you should see in your Object Inspector panel, the name of the class is no longer QWidget, it's MyWidget instead.

    Image Promoted

  5. In the file at /path/to/MyWidget.py I have a class named MyWidget, and the content is something like this:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    from PyQt4 import QtGui
    
    class MyWidget(QtGui.QWidget):
        def __init__(self, parent=None):
            super(MyWidget, self).__init__(parent)
    
            self.labelHello = QtGui.QLabel(self)
            self.labelHello.setText("This is My Widget")
    
            self.layout = QtGui.QHBoxLayout(self)
            self.layout.addWidget(self.labelHello)
    
like image 94
user1006989 Avatar answered Nov 11 '22 20:11

user1006989