Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Subclassing QWidget and hiding some internal widgets to derived classes
QtWS25 Last Chance

Subclassing QWidget and hiding some internal widgets to derived classes

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maxxximo
    wrote on last edited by Maxxximo
    #1

    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
    {
    public:
    	A(QWidget* pParent = 0) : QWidget(pParent)
    	{
    		m_gridLayout = new QGridLayout;	
    		m_pStatusBar = new QLabel("Status bar");
    
    		m_pFormWidget = new QWidget;
    
    		m_gridLayout->addWidget(m_pFormWidget,0,0);
    		m_gridLayout->addWidget(m_pStatusBar,1,0);
    		setLayout(m_gridLayout);
    	}
    private:
    	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);
    		//....
    	}
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Maxxximo
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved