Nominate our 2022 Qt Champions!

Subclassing QWidget and hiding some internal widgets to derived classes

  • Hi all,
    I can't find a way to sublass QWidget to have a QGridLayout arranging a main (big) widget and a label (status bar). I would like the label
    to be hidden to any derived classes. Each derived class should be able to create its own layouts and widgets, which should be children
    of the base class' main widget.
    I included some simplified code to better explain. The problem with this code is that class B uses A as a whole widget, instead of using A.m_pFormWidget.
    I need to access it without intermediate functions.
    #UPDATE# What if I don't add layout and widgets to class A and override paintEvent()? But in this case I should only paint the contents of my status bar when needed, and leave the widget content, written by class B, unchanged. I don't believe this is possible either...
    Thanks in advance.

    class A : public Widget
    	A(QWidget* pParent = 0) : QWidget(pParent)
    		m_gridLayout = new QGridLayout;	
    		m_pStatusBar = new QLabel("Status bar");
    		m_pFormWidget = new QWidget;
    	QGridLayout* m_gridLayout;
    	QLabel*		m_pStatusBar;
    	QWidget*	m_pFormWidget;
    class B : public A
    	B(QWidget* pParent = 0) : A(pParent)
    		QGridLayout *pHLayoutMain = new QGridLayout(this);
    		pHLayoutMain->addWidget(new QLabel("Label 1"), 0,0);
    		pHLayoutMain->addWidget(new QLabel("Label 2"), 0,1);

  • Lifetime Qt Champion


    What about following QMainWindow's design ? Add a setCentalWidget to your widget that must be used to add custom widgets.

    Hope it helps

  • Thanks for replying SGaist but I need to explain a bit better. The interface exposed by class A cannot be modified: it's part of a library whose code I cannot access, and I'm trying to reimplement it for accessing a new device. It inherits QWidget, plus it has a SetStatusBarText method. I'm not sure how it was implemented, but it writes text to a status bar at the bottom of the widget itself. Class B inherits from class A and adds a layout and various widgets to the inherited widget. I wonder how they managed to write on the widget's bottom in class A .... If I add a layout and a main widget plus a status bar widget to class A (above code illustrates this situation) I won't be able to add layouts and widgets in class B! I guess the only way would be painting the text privately inside class A, but I dont know how, or putting the status bar widget in the container class (i.e. class C) and let that class set the appropriate text with a signal/slot

  • Lifetime Qt Champion

    So both class A and class B are from this library ?

Log in to reply