proff
proff

Reputation: 23

adding a custom widget in Layout at execution

I'm under Ubuntu 18.04 and using last QtCreator with last Qt5 framework

I'm trying to setup a vertical layout with at top an horizontal nested layout and under a custom made widget (based on clock exemple for the moment)

I setup a fresh widget project but as I dont understand yet how to get acess to nested layout from c++ code I removed automaticaly created mainform and created layout at execution.

If I use my custom widget as window it works if I use a window and add my custom widget it works but if I add a layout, add my custom widget to this layout and call setLayout on window it disappear...

I tried almost all orders : set the layout first or after adding my widget. I tried to call show() on my widget or not befor or after adding it to layout I tried to add the nested layer first or last nothing change I 've read several time exemples and manual about layout and nested one I can see my nested layout but not my widget

here's my main :

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;
    w.setWindowTitle("WOUND : Windings Organiser with Usefull and Neat Drawings");

    QVBoxLayout* hl=new QVBoxLayout;// top vertical layout

    QHBoxLayout* slotqueryLayout=new QHBoxLayout; // nested first horizontal layout
    QLabel *slotqueryLabel = new QLabel("Slot number:");
    QLineEdit *slotqueryEdit = new QLineEdit("48");
    slotqueryLayout->addWidget(slotqueryLabel);
    slotqueryLayout->addWidget(slotqueryEdit);
    hl->addLayout(slotqueryLayout);

    WindingViewer* wv=new WindingViewer(&w); // my widget is just a simple canvas to draw things
    hl->addWidget(wv);
    wv->show(); // dont know if it's needed but if I remove it it dont change anything / tried to do it before or after adding to layout

    w.setLayout(hl); // if called before adding content it dont change

    w.show();
    return a.exec();
}

and here you can find my custom widget:

class WindingViewer : public QWidget
{
    Q_OBJECT
public:
    explicit WindingViewer(QWidget *parent = nullptr);

protected:
    void paintEvent(QPaintEvent *event) override;

signals:
public :
    int SlotNumber;
public slots:
};

and

WindingViewer::WindingViewer(QWidget *parent) : QWidget(parent)
{
    SlotNumber=3;
    resize(200, 200);
}

void WindingViewer::paintEvent(QPaintEvent *)
{

    int side = qMin(width(), height());
    QColor SlotColor(127, 127, 127);
    QPainter painter(this);
    static const QPoint slotpolygonext[4] = {
        QPoint(-2,85),
        QPoint(-3,95),
        QPoint(3, 95),
        QPoint(2, 85)
    };
    static const QPoint slotpolygonint[5] = {
        QPoint(-1,75),
        QPoint(-2,85),
        QPoint(2, 85),
        QPoint(1, 75),
        QPoint(-1,75),
    };
    painter.setRenderHint(QPainter::Antialiasing);
    painter.translate(width() / 2, height() / 2);
    painter.scale(side / 200.0, side / 200.0);
    painter.setPen(SlotColor);
    for (int i = 0; i < SlotNumber; ++i) {
        painter.drawPolyline(slotpolygonext,4);
        painter.drawPolyline(slotpolygonint,5);
        painter.rotate(360.0/SlotNumber);
    }
}

I hope the question is clear enough. I've search for an answer here and over internet before posting. I found few things but nothing totaly related.

Upvotes: 2

Views: 1521

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

The custom widget is part of the window, you could see if manually with the mouse you make the height increase.

And then why is it hidden?

The layouts handle size policies and use the sizeHint() function of the widgets to obtain the default size, and in your case the sizeHint() is not implemented because it is not observed when the window is displayed, the solution is to implement that method:

*.h

public:
    explicit WindingViewer(QWidget *parent = nullptr);
    QSize sizeHint() const override;

*.cpp

QSize WindingViewer::sizeHint() const
{
    return QSize(200, 200);
}

enter image description here

Upvotes: 2

Related Questions