【问题标题】:adding a custom widget in Layout at execution在执行时在布局中添加自定义小部件
【发布时间】:2019-01-31 23:09:04
【问题描述】:

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

我希望问题足够清楚。在发布之前,我已经在这里和互联网上搜索了答案。我发现的东西很少,但没有完全相关的。

【问题讨论】:

    标签: c++ qt qt5 qlayout


    【解决方案1】:

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

    那为什么要隐藏呢?

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

    *.h

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

    *.cpp

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

    【讨论】:

    • 我可以问一个相关的问题吗?如果我在窗口 QtDesigner 的 QVBoxLayout 中设置了 2 个嵌套的 QHBoxLayout,如何将我的自定义小部件添加到嵌套布局中?换句话说,我如何在执行时找到嵌套布局参考?
    • @proff,如果您有相关问题,请发布一个新问题并与此问题相关以澄清。
    • @proff 应该有从 QtDesigner 创建的头文件/源文件。检查它们并在QHBoxLayoutQVBoxLayout 中搜索变量名称。 (我相信他们的名字可以在设计器中指定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 2012-03-07
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    相关资源
    最近更新 更多