0

I wanted to draw a triangle. But I face too many errors.

It doesn’t recognize poly, qpoint and *e.

How can I fix this?

#include "dialog.h"
#include "ui_dialog.h"
#include <QtCore>
#include <QtGui>
#include <QGraphicsScene>
#include <QGraphicsEllipseItem>
#include <qpainter.h>
#include <QPolygon>
#include <QPoint>
#include <QPainter>
#include <QPaintEvent>

Dialog::Dialog(int a1, int b1, int a2, int b2, int a3,
               int b3, char t, QWidget *parent):
    QDialog(parent),

    ui(new Ui::Dialog),
    x1(a1), x2(a2), x3(a3), y1(b1), y2(b2), y3(b3), type(t)
{
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);

    QBrush redBrush (Qt::red);
    QPen blackpen(Qt::black);
    blackpen.setWidth(1);
    if(type == 'c')
        ellipse = scene->addEllipse(0, 0, 10, 10, blackpen, redBrush);
    else if(type == 'l')
    {
        // Draw line
    }
    else
    {
        // Draw traingle
        paintEvent(QPaintEvent *e);
    }

}

void Dialog::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);

    QPolygon poly;
    poly «QPoint(100, 150);

    poly «QPoint(90, 130);
    poly «QPoint(120, 130);

    QPen linepen;
    linepen.setWidth(8);
    linepen.setColor(Qt::red);
    linepen.setJoinStyle(Qt::MiterJoin);
    painter.setPen(linepen);

    QBrush fillbrush;
    fillbrush.setColor(Qt::red);
    fillbrush.setStyle(Qt::SolidPattern);

    QPainterPath path;
    path.addPolygon(poly);

    painter.drawPolygon(poly);
    painter.fillPath(path, fillbrush);
}

Dialog::~Dialog() {
    delete ui;
}

Errors:

error: 'e' was not declared in this scope
error: stray '\302' in program
poly « QPoint(120, 130);
     ^
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dreamer1375
  • 53
  • 1
  • 10
  • dont call `paintEvent` yourself, it is done by the framework when it needs to. generally the line with `paintEvent(QPaintEvent *e);` is just incorrect – Zaiborg May 18 '16 at 08:20
  • so how i can call it in else loop? – dreamer1375 May 18 '16 at 08:24
  • you dont want to. you need to store whatever you want to be drawn and store it. in your `paintEvent` you only access this data for the painting – Zaiborg May 18 '16 at 08:27
  • The main OP's question has answers [here](https://stackoverflow.com/questions/3965895/how-to-draw-a-triangle-by-using-qgraphicsviews-qgraphicsitem-class), `paintEvent` should not be used, and the way the triangle is being drawn will result in it drawn on the `Dialog`, not `ui->graphicsView`, just use this [answer](https://stackoverflow.com/a/3966079/17726418) instead of `paintEvent(QPaintEvent *e);` in your last `else`, and replace `pScene` with your `scene`. – Abderrahmene Rayene Mihoub May 01 '23 at 16:11

0 Answers0