简体   繁体   English

执行时在布局中添加自定义小部件

[英]adding a custom widget in Layout at execution

I'm under Ubuntu 18.04 and using last QtCreator with last Qt5 framework我在 Ubuntu 18.04 下使用最后一个 QtCreator 和最后一个 Qt5 框架

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.我设置了一个新的小部件项目,但由于我还不明白如何从 C++ 代码访问嵌套布局,我删除了自动创建的主窗体并在执行时创建了布局。

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...如果我使用我的自定义小部件作为窗口它可以工作如果我使用一个窗口并添加我的自定义小部件它可以工作但是如果我添加一个布局,将我的自定义小部件添加到这个布局并在窗口上调用 setLayout 它会消失......

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我尝试在我的小部件上调用 show() 之前或之后或在将其添加到布局之后我尝试首先添加嵌套层或最后没有任何更改我已经阅读了几次关于布局和嵌套的示例和手册我可以看到我的嵌套布局但不是我的小部件

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.我发现的东西很少,但没有什么完全相关的。

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:布局处理大小策略并使用小部件的sizeHint()函数获取默认大小,在您的情况下未实现sizeHint()因为在显示窗口时未观察到它,解决方案是实现该方法:

*.h *。H

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

*.cpp *.cpp

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM