如何借助布局在QGraphicWidget中排列小部件和形状?

时间:2018-08-24 15:36:23

标签: qt layout graphics paint qgraphicswidget

我有一个自定义的QGraphicsWidgets,它绘制了一些项目,还包含一些子代理小部件:

//Constructor adds a child proxy button
TestWidget::TestWidget(QJsonObject testDefinition, QJsonObject testStatus, QString parentSequenceID, QGraphicsWidget *parent):
    m_parentID(parentSequenceID),
    m_testStatus(testStatus),
    m_testDefinition(testDefinition)
{
    Q_UNUSED(parent);

    m_name = m_testDefinition[displayNameStr].toString();
    m_status = m_testStatus[resultStr].toObject()[resultStr].toString();
    m_id = m_testDefinition[idStr].toString();

    m_layout = new QGraphicsLinearLayout(Qt::Vertical, this);

    //Add the pause/resume button
    auto *proxyPauseAbortButton = new QGraphicsProxyWidget(this);

    proxyPauseAbortButton->setWidget(new QPushButton(tr("Pause")));
    proxyPauseAbortButton->moveBy(20, 40);

    //Add the progress bar
    auto * proxyTestProgressBar = new QGraphicsProxyWidget(this);
    QProgressBar * testProgressBar = new QProgressBar();
    testProgressBar->setOrientation(Qt::Horizontal);
    testProgressBar->setRange(0, 100); // Let's say it goes from 0 to 100
    testProgressBar->setValue(10); // With a current value of 10

    proxyTestProgressBar->setWidget(testProgressBar);
    proxyTestProgressBar->moveBy(20, 80);

    //Add the html view
    auto * proxyTestHtmlView = new QGraphicsProxyWidget(this);
    QWebEngineView * testHtmlView = new QWebEngineView();
    testHtmlView->load(QUrl(QStringLiteral("https://www.google.com/")));

    proxyTestHtmlView->setWidget(testHtmlView);
    proxyTestHtmlView->moveBy(20, 120);

}

//Paint function draws a border, shapes, and text
void TestWidget::paint(QPainter *painter,
    const QStyleOptionGraphicsItem *option, QWidget *widget /*= 0*/)
{
    Q_UNUSED(widget);
    Q_UNUSED(option);

    QRectF frame(QPointF(0,0), geometry().size());
    QGradientStops stops;   

    //Draw border
    painter->drawRoundedRect(boundingRect(), 5.0, 5.0);
    //Name of the test
    painter->drawText(40, 20, m_name);

    //Status of test
    QFont font = painter->font() ;
    font.setPointSize(14);
    painter->setFont(font);
    painter->drawText(600, 20, m_status);

    //Arrow button
    QPolygonF poly;
    poly << QPointF(5, 10) << QPointF(25, 10) << QPointF(15, 20 )<< QPointF(5, 10);
    painter->setBrush(Qt::black);
    painter->drawPolygon(poly, Qt::OddEvenFill);

}

我的问题是,您如何将每个组件(图形/窗口小部件)添加到布局中?什么时候合适?小部件一词指的是QPushButton,QProgressBar和QWebEngineView。绘图是指绘画功能中的形状和文本。

这是我希望自定义小部件的外观的草图: enter image description here

0 个答案:

没有答案