Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to make QWidget fill parent QWidget?



  • I'm having trouble getting a child QWidget to fill it's parent. I've created a form using QtDesigner and added some QWidgets to it - the idea is that I want to fill these empty rectangles with custom QWidgets. In the constructor for my form, I am creating an instance of my child widget and adding it and a layout to the placeholder QWidget I have in my form, but no matter what I try my child widget is only filling a small portion in the top left corner of the widget I want it to fill entirely. How do I fix this?

    sequencereditor.h

    #ifndef __SEQUENCER_EDITOR_H__
    #define __SEQUENCER_EDITOR_H__
    
    #include "ui_sequencereditor.h"
    
    class SequencerNames;
    
    class SequencerEditor : public QDockWidget, public Ui::SequencerEditor
    {
        Q_OBJECT
    
        Score* _score;
        SequencerNames* _seqNames;
    
    public:
        explicit SequencerEditor(QWidget *parent = nullptr);
        ~SequencerEditor();
    };
    
    #endif // __SEQUENCER_EDITOR_H__
    

    sequenceredtior.cpp

    #include "sequencereditor.h"
    
    #include "sequencernames.h"
    
    SequencerEditor::SequencerEditor(QWidget *parent) :
        QDockWidget(qApp->translate("Sequencer", "Sequencer"), parent)
    {
        setupUi(this);
    
        QHBoxLayout* nameAreaLayout = new QHBoxLayout(nameArea);
    
        _seqNames = new SequencerNames(nameArea);
    
    //    _seqNames->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    
        nameAreaLayout->addWidget(_seqNames);
    }
    
    SequencerEditor::~SequencerEditor()
    {
    }
    
    

    sequencernames.h

    #ifndef __SEQUENCER_NAMES_H__
    #define __SEQUENCER_NAMES_H__
    
    
    
    class SequencerNames : public QWidget
    {
        Q_OBJECT
    
    public:
        SequencerNames(QWidget* parent = nullptr);
    
    private:
        virtual void paintEvent(QPaintEvent*);
    
    };
    
    #endif // SEQUENCERNAMES_H
    
    

    sequencernames.cpp

    #include "sequencernames.h"
    
    SequencerNames::SequencerNames(QWidget* parent) :
        QWidget(parent)
    {
    
    }
    
    //---------------------------------------------------------
    //   paintEvent
    //---------------------------------------------------------
    
    void SequencerNames::paintEvent(QPaintEvent* /*event*/)
    {
        QPainter p(this);
        p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing);
    
        p.setPen(Qt::red);
        p.drawEllipse(0, 0, width(), height());
    }
    
    


  • What is "nameArea"?
    I think you need to post sequencereditor.ui, too.
    Also, although I'm not familiar with QDockWidget, but I think the usual way is not subclassing it, but calling setWidget()


Log in to reply