基于qt draw的输入坐标轴显示图像的demo
Posted 狗蛋儿l
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于qt draw的输入坐标轴显示图像的demo相关的知识,希望对你有一定的参考价值。
ui把外框放到最大
然后添加这些元素
头文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QTimeLine>
#include <QPainter>
#include <QDateTime>
#include <string>
#include<QTextEdit>
#include <QMainWindow>
#include <QWidget>
#include <QPaintEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_rectangle_clicked();
void on_pushButton_circle_clicked();
void on_pushButton_triangle_clicked();
void on_pushButton_line_clicked();
protected:
void paintEvent(QPaintEvent *);
private:
Ui::MainWindow *ui;
int w=100,h=100;
int x=20,y=20;
bool crcle=0;//标志是否点击按钮
bool squer=0;
bool Polygon=0;
bool line=0;
};
#endif // MAINWINDOW_H
窗口实现:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QTime>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
if(squer==1)
{
painter.drawRect(x,y,w,h);
squer=0;
}
if(crcle==1)
{
painter.drawEllipse(x,y,w,h);
crcle=0;
}
if(Polygon==1)
{
QPointF points[4] = {QPointF(x, y+h), QPointF((x+w)/2, y),QPointF(x+w, y+h)};
painter.drawPolygon(points,3);
Polygon=0;
}
if(line==1)
{
// 反走样 防止绘制出来的线条出现锯齿
painter.setRenderHint(QPainter::Antialiasing, true);
// 绘制直线
painter.drawLine(QPointF(x, y), QPointF(x+w, y+w));
line=0;
}
}
void MainWindow::on_pushButton_rectangle_clicked()
{
x=(ui->lineEdit_x->text()).toInt();
y=(ui->lineEdit_y->text()).toInt();
w=(ui->lineEdit_w->text()).toInt();
h=(ui->lineEdit_h->text()).toInt();
squer=1;
QWidget::update();
}
void MainWindow::on_pushButton_circle_clicked()
{
x=(ui->lineEdit_x->text()).toInt();
y=(ui->lineEdit_y->text()).toInt();
w=(ui->lineEdit_w->text()).toInt();
h=(ui->lineEdit_h->text()).toInt();
crcle=1;
QWidget::update();
}
void MainWindow::on_pushButton_triangle_clicked()
{
x=(ui->lineEdit_x->text()).toInt();
y=(ui->lineEdit_y->text()).toInt();
w=(ui->lineEdit_w->text()).toInt();
h=(ui->lineEdit_h->text()).toInt();
Polygon=1;
QWidget::update();
}
void MainWindow::on_pushButton_line_clicked()
{
x=(ui->lineEdit_x->text()).toInt();
y=(ui->lineEdit_y->text()).toInt();
w=(ui->lineEdit_w->text()).toInt();
h=(ui->lineEdit_h->text()).toInt();
line=1;
QWidget::update();
}
main主文件不变
最后成品:
码云代码
以上是关于基于qt draw的输入坐标轴显示图像的demo的主要内容,如果未能解决你的问题,请参考以下文章
Unity 实现《物理画线》Draw Physics Line 总览