Как нарисовать "пустотелый" треугольник в qt?
Как правильно нарисовать треугольник в qt ?
я могу нарисовать его двумя вариантами:
первый вариант
// Start point of bottom line
qreal startPointX1 = 60.0;
qreal startPointY1 = 60.0;
// End point of bottom line
qreal endPointX1 = 60.0;
qreal endPointY1 = 120.0;
// Start point of top line
qreal startPointX2 = 60.0;
qreal startPointY2 = 60.0;
// End point of top line
qreal endPointX2 = 80.0;
qreal endPointY2 = 120.0;
QPainterPath path;
// Set pen to this point.
path.moveTo (startPointX2, startPointY2);
// Draw line from pen point to this point.
path.lineTo (endPointX1, endPointY1);
//path.moveTo (endPointX1, endPointY1); // <- no need to move
path.lineTo (endPointX2, endPointY2);
//path.moveTo (endPointX2, endPointY2); // <- no need to move
path.lineTo (startPointX1, startPointY1);
QPainter painter(this);
painter.setPen (Qt :: NoPen);
painter.fillPath (path, QBrush (QColor ("blue")));
Второй более предпочтительней :
QRectF rect = QRectF(0, 0, 100, 100);
QPainterPath path;
path.moveTo(rect.left() + (rect.width() / 2), rect.top());
path.lineTo(rect.bottomLeft());
path.lineTo(rect.bottomRight());
path.lineTo(rect.left() + (rect.width() / 2), rect.top());
QPainter painter(this);
painter.fillPath(path, QBrush(QColor ("blue")));
Вот весь код последней версии в которую поставил фотографию :
treygol.pro
QT += core gui widgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = treygol
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
DISTFILES += \
treygol.jpg
mainwindows.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMainWindow>
#include <QDebug>
#include <QPainter>
#include <QPen>
#include <QFont>
#include <QFontMetrics>
#include <QPainterPath>
#include <QRectF>
#include <QKeyEvent>
struct texnStructur
{
texnStructur() // сразу же заполняем его нулями
{
m_pitch = 0; m_t_pitch = 0; razm_lin = 0; razm_text = 0;
razm_shrift = 0; razm_stroki = 0;
for(int i=0; i < sizeof(m_t_razm)/ sizeof(m_t_razm[0]); i++)
m_t_razm[i] = 0;
}
float m_t_razm[5];
int m_t_pitch; // техническая переменная
int m_pitch; // переменная для положения на градусной ленте
float razm_lin; // переменная размера градулированной линии копаса
float razm_text; // переменная размера текста (цифр) копаса
int razm_shrift; // переменная размера шрифта текста копаса
int razm_stroki;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void paintEvent(QPaintEvent *event);
// void resizeEvent/*(*/QResizeEvent *event);
void keyPressEvent(QKeyEvent *event);
texnStructur _myTexpr; // обьявляем его
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindows.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}
MainWindow::~MainWindow()
{
}
void MainWindow::paintEvent(QPaintEvent *)
{
QRectF rect = QRectF(190, 0, 500, 400);
QPixmap img("treygol.jpg");
QPainterPath path;
path.moveTo(rect.left() + (rect.width() / 2), rect.top());
path.lineTo(rect.bottomLeft());
path.lineTo(rect.bottomRight());
path.lineTo(rect.left() + (rect.width() / 2), rect.top());
QPainter painter(this);
QPen pen_abris(Qt::black, 5, Qt::SolidLine, Qt::FlatCap); // кисть абриса (компаса)
painter.setPen(QPen(pen_abris));
QBrush br (Qt::SolidPattern,img);
painter.setBrush(br);
painter.drawPath(path);
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Down:
// _myTexpr.m_pitch++;
_myTexpr.m_pitch += 1.0;
break;
case Qt::Key_Up:
// _myTexpr.m_pitch--;
_myTexpr.m_pitch -= 1.0;
break;
default:
QWidget::keyPressEvent(event);
break;
}
update();
}
Почитал о том что QBrush Можно сделать что был треугольник с обводами "пустотелый". Но как я не пытался QBrush настроить так мне и не удалось :(
Ответы (1 шт):
Автор решения: timob256
→ Ссылка
Ух разобрался :)
Надо было в mainwindow.hдобавить строку #include <QBrush>
вот код из mainwindow.cpp :
void MainWindow::paintEvent(QPaintEvent *)
{
QRectF rect = QRectF(190, 0, 500, 400);
QPainterPath path;
path.moveTo(rect.left() + (rect.width() / 2), rect.top());
path.lineTo(rect.bottomLeft());
path.lineTo(rect.bottomRight());
path.lineTo(rect.left() + (rect.width() / 2), rect.top());
QPainter painter(this);
QPen pen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);
painter.setPen(pen);
QBrush br (Qt::NoBrush);
painter.setBrush(br);
painter.drawPath(path);
}
вот результат :



