Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

Not able to create 3 similar Triangle object



  • Took RenderArea class from basicdrawing and insert into my project. Not able to create a sequence of Triangle object

    ////////////////////////////////////////////////////////////////////////
        RenderArea* renderArea;
    //////////////////////////////////////////////////////////////
        renderArea = new RenderArea;
    
        RenderArea::Shape shape = RenderArea::Shape(4);
        renderArea->setShape(shape);
    
        Layout_page2->addWidget(renderArea);
    

  • Moderators

    @alvinNew Looking at source it seems you need to use a polygon which is at index 3 and set points count to 3 in renderarea.cpp here painter.drawPolygon(points, 3);



  • Did few experiment with renderarea class, cannot get it to work. It's alright.
    Any idea, How to create 3 triangle and blind it in horizontal layout?


    | / | / | /
    |/ |/ |/


  • Moderators

    @alvinNew Create a custom widget by subclassing QWidget and override paintEvent to drawing custom shapes. Following is an example:

    //triangle.cpp
    Triangle::Triangle(QWidget *parent) :
        QWidget(parent)
    {}
    
    void Triangle::paintEvent(QPaintEvent *e)
    {
        static const QPoint points[3] = {
            QPoint(40, 80),
            QPoint(20, 10),
            QPoint(60, 10),
        };
        QPainter p(this);
        p.drawPolygon(points,3);
    }
    

    and then add it from your main code for eg. from dialog.cpp or mainwindow.cpp

    //dialog.cpp
    #include "triangle.h"
    
    Triangle *a = new Triangle(this);
    Triangle *b = new Triangle(this);
    Triangle *c = new Triangle(this);
    
    //add it to horizontal or vertical layout, add stretch as required.
    ui->horizontalLayout->addWidget(a);
    ui->horizontalLayout->addWidget(b);
    ui->horizontalLayout->addWidget(c);
    

    Try playing with the above code. Make it more dynamic by calculating points depending upon the widget size.
    Hope this helps...



  • It works, i got the 3 triangle.
    But when i addwidget with other layout and widget.
    There is a huge spacing(row).
    I want the 3 triangle directly after my page2_line, best it is joined.

    =======mainwindow =======

    PictureWidget *widget = new PictureWidget();
    
    Layout_page2->addWidget(page2_line);
    Layout_page2->addWidget(widget);
    Layout_page2->addLayout(WordLayout);
    Layout_page2->addWidget(gifmoving);

  • Moderators

    @alvinNew How does it look now? Can post post a screenshot ?
    Besides, Layouts are meant for that. They equally space widgets inside them.



  • @p3c0
    in picpaste
    http://picpaste.com/picpaste-bazEpaOE.jpg

    want to add the page2_line in Widget, but i felt logically this isn;t correct


  • Moderators

    @alvinNew Try setting maximum and minimum size to the widget which is above the triangle widget.


Log in to reply