簡體   English   中英

執行時在布局中添加自定義小部件

[英]adding a custom widget in Layout at execution

我在 Ubuntu 18.04 下使用最后一個 QtCreator 和最后一個 Qt5 框架

我正在嘗試設置垂直布局,頂部是水平嵌套布局,在定制小部件下方(目前基於時鍾示例)

我設置了一個新的小部件項目,但由於我還不明白如何從 C++ 代碼訪問嵌套布局,我刪除了自動創建的主窗體並在執行時創建了布局。

如果我使用我的自定義小部件作為窗口它可以工作如果我使用一個窗口並添加我的自定義小部件它可以工作但是如果我添加一個布局,將我的自定義小部件添加到這個布局並在窗口上調用 setLayout 它會消失......

我嘗試了幾乎所有訂單:首先或在添加我的小部件之后設置布局。 我嘗試在我的小部件上調用 show() 之前或之后或在將其添加到布局之后我嘗試首先添加嵌套層或最后沒有任何更改我已經閱讀了幾次關於布局和嵌套的示例和手冊我可以看到我的嵌套布局但不是我的小部件

這是我的主要內容:

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();
}

在這里你可以找到我的自定義小部件:

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

protected:
    void paintEvent(QPaintEvent *event) override;

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

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);
    }
}

我希望這個問題足夠清楚。 在發布之前,我已經在這里和互聯網上搜索了答案。 我發現的東西很少,但沒有什么完全相關的。

自定義小部件是窗口的一部分,您可以查看是否使用鼠標手動增加高度。

那為什么要隱藏呢?

布局處理大小策略並使用小部件的sizeHint()函數獲取默認大小,在您的情況下未實現sizeHint()因為在顯示窗口時未觀察到它,解決方案是實現該方法:

*。H

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

*.cpp

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM