Qt 坐标轴 平滑曲线 贝塞尔曲线

来源:互联网 发布:java简单图书系统代码 编辑:程序博客网 时间:2024/05/17 03:04


ui头文件

/********************************************************************************** Form generated from reading UI file 'mainwindow.ui'**** Created by: Qt User Interface Compiler version 5.4.1**** WARNING! All changes made in this file will be lost when recompiling UI file!********************************************************************************/#ifndef UI_MAINWINDOW_H#define UI_MAINWINDOW_H#include <QtCore/QVariant>#include <QtWidgets/QAction>#include <QtWidgets/QApplication>#include <QtWidgets/QButtonGroup>#include <QtWidgets/QGraphicsView>#include <QtWidgets/QHeaderView>#include <QtWidgets/QLabel>#include <QtWidgets/QLineEdit>#include <QtWidgets/QMainWindow>#include <QtWidgets/QPushButton>#include <QtWidgets/QTextEdit>#include <QtWidgets/QVBoxLayout>#include <QtWidgets/QWidget>QT_BEGIN_NAMESPACEclass Ui_MainWindow{public:    QWidget *centralWidget;    QPushButton *pushButton;    QPushButton *pushButton_2;    QPushButton *pushButton_3;    QTextEdit *textEdit;    QLabel *label;    QLabel *label_2;    QLineEdit *lineEdit;    QLineEdit *lineEdit_2;    QPushButton *pushButton_4;    QWidget *verticalLayoutWidget;    QVBoxLayout *verticalLayout;    QGraphicsView *graphicsView;    void setupUi(QMainWindow *MainWindow)    {        if (MainWindow->objectName().isEmpty())            MainWindow->setObjectName(QStringLiteral("MainWindow"));        MainWindow->resize(711, 420);        MainWindow->setMinimumSize(QSize(711, 420));        MainWindow->setMaximumSize(QSize(711, 420));        MainWindow->setSizeIncrement(QSize(711, 420));        MainWindow->setBaseSize(QSize(711, 420));        centralWidget = new QWidget(MainWindow);        centralWidget->setObjectName(QStringLiteral("centralWidget"));        pushButton = new QPushButton(centralWidget);        pushButton->setObjectName(QStringLiteral("pushButton"));        pushButton->setGeometry(QRect(20, 340, 21, 23));        pushButton_2 = new QPushButton(centralWidget);        pushButton_2->setObjectName(QStringLiteral("pushButton_2"));        pushButton_2->setGeometry(QRect(50, 340, 21, 23));        pushButton_3 = new QPushButton(centralWidget);        pushButton_3->setObjectName(QStringLiteral("pushButton_3"));        pushButton_3->setGeometry(QRect(20, 380, 51, 23));        textEdit = new QTextEdit(centralWidget);        textEdit->setObjectName(QStringLiteral("textEdit"));        textEdit->setGeometry(QRect(290, 330, 421, 91));        label = new QLabel(centralWidget);        label->setObjectName(QStringLiteral("label"));        label->setGeometry(QRect(121, 340, 16, 16));        label_2 = new QLabel(centralWidget);        label_2->setObjectName(QStringLiteral("label_2"));        label_2->setGeometry(QRect(121, 357, 16, 16));        lineEdit = new QLineEdit(centralWidget);        lineEdit->setObjectName(QStringLiteral("lineEdit"));        lineEdit->setGeometry(QRect(139, 340, 91, 20));        lineEdit_2 = new QLineEdit(centralWidget);        lineEdit_2->setObjectName(QStringLiteral("lineEdit_2"));        lineEdit_2->setGeometry(QRect(139, 357, 91, 20));        pushButton_4 = new QPushButton(centralWidget);        pushButton_4->setObjectName(QStringLiteral("pushButton_4"));        pushButton_4->setGeometry(QRect(140, 380, 75, 23));        verticalLayoutWidget = new QWidget(centralWidget);        verticalLayoutWidget->setObjectName(QStringLiteral("verticalLayoutWidget"));        verticalLayoutWidget->setGeometry(QRect(0, 0, 711, 331));        verticalLayout = new QVBoxLayout(verticalLayoutWidget);        verticalLayout->setSpacing(6);        verticalLayout->setContentsMargins(11, 11, 11, 11);        verticalLayout->setObjectName(QStringLiteral("verticalLayout"));        verticalLayout->setContentsMargins(0, 0, 0, 0);        graphicsView = new QGraphicsView(verticalLayoutWidget);        graphicsView->setObjectName(QStringLiteral("graphicsView"));        verticalLayout->addWidget(graphicsView);        MainWindow->setCentralWidget(centralWidget);        retranslateUi(MainWindow);        QMetaObject::connectSlotsByName(MainWindow);    } // setupUi    void retranslateUi(QMainWindow *MainWindow)    {        MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0));        pushButton->setText(QApplication::translate("MainWindow", "-", 0));        pushButton_2->setText(QApplication::translate("MainWindow", "+", 0));        pushButton_3->setText(QApplication::translate("MainWindow", "build", 0));        label->setText(QApplication::translate("MainWindow", "x:", 0));        label_2->setText(QApplication::translate("MainWindow", "y:", 0));        pushButton_4->setText(QApplication::translate("MainWindow", "addpoint", 0));    } // retranslateUi};namespace Ui {    class MainWindow: public Ui_MainWindow {};} // namespace UiQT_END_NAMESPACE#endif // UI_MAINWINDOW_H
窗体头文件:
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include"ui_mainwindow.h"namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{    Q_OBJECTpublic:    explicit MainWindow(QWidget *parent = 0);    //void paintEvent(QPaintEvent *e);    ~MainWindow();private slots:    void on_pushButton_4_clicked();    void on_pushButton_2_clicked();    void on_pushButton_clicked();    void on_pushButton_3_clicked();private:    Ui::MainWindow *ui;protected:    //void paintEvent ( QPaintEvent * event );    //virtual void paintEvent(QPaintEvent *event);};/*class testView : public QGraphicsView{    Q_OBJECTpublic:    testView(QWidget* parent = NULL);    ~testView();private slots:    void slot_update();};*/class testScene : public QGraphicsScene{    Q_OBJECTpublic:    testScene();    ~testScene();private slots:    void slot_update();protected:    virtual void drawBackground(QPainter *painter, const QRectF &rect);    virtual void drawForeground(QPainter *painter, const QRectF &rect);private:    QPainterPath mPath;};#endif // MAINWINDOW_H
储存点的头文件
#ifndef POINTSAVE#define POINTSAVEclass Pointtt{public:    struct po    {        double x;        double y;        struct po *next;    };private:     struct po *cc;     struct po head;public:     Pointtt();     struct po rehead();    void AddPoint(double xx,double yy);};#endif // POINTSAVE
窗体源文件
#include "pointsave.h"#include "mainwindow.h"#include "ui_mainwindow.h"#include <QString>#include<QGraphicsView>#include<QGraphicsScene>#include<QGraphicsLineItem>#include<QGraphicsPathItem>#include <QtGui>#include <QCompleter>#include <QTest>using   namespace   std;MainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::MainWindow){    ui->setupUi(this);    ui->verticalLayout->addWidget(ui->graphicsView);    testScene* scene = new testScene();    connect(ui->pushButton_3, SIGNAL(clicked()), scene, SLOT(slot_update()));    //connect(ui->pushButton_3, SIGNAL(clicked()), ui->graphicsView, SLOT(slot_update()));    ui->graphicsView->setScene(scene);}/*MainWindow::paintEvent(QPaintEvent *event){    QWidget::paintEvent(event);   // QMainWindow}*/MainWindow::~MainWindow(){    delete ui;}testScene::testScene(){    setSceneRect(QRectF(0,0,2000,600));}testScene::~testScene(){}void testScene::drawBackground( QPainter *painter, const QRectF &rect ){    painter->save();    painter->setBrush(Qt::darkCyan);    painter->drawRect(rect);    painter->setPen(Qt::yellow);    const double w = sceneRect().width();    const double h = sceneRect().height();    QLineF line(QPointF(1,h),QPointF(w,h));    painter->drawLine(line);    QLineF line1(QPointF(1,h),QPointF(1,0));    painter->drawLine(line1);    for(int i=h; i>0; i-=100)    {        QLineF line(QPointF(1,i),QPointF(w,i));        painter->drawLine(line);    }     for(int i=1;i<w;i+=100)     {         QLineF line(QPointF(i,0),QPointF(i,h));         painter->drawLine(line);     }    painter->restore();}void testScene::drawForeground( QPainter *painter, const QRectF &rect ){    //qDebug()<<"drawForeground";    painter->save();    painter->setPen(QPen(Qt::red,5));    painter->drawPath(mPath);    painter->restore();}void testScene::slot_update(){      //qsrand(QTime::currentTime().msec());    extern Pointtt point;        Pointtt::po *dd;        Pointtt::po *ddd;        dd=&point.rehead();        dd=dd->next;    mPath = QPainterPath();    mPath.moveTo(dd->x,sceneRect().height()-dd->y);    for(ddd=dd->next;ddd!=nullptr;ddd=ddd->next)    {        QPoint c1((dd->x+ddd->x)/2,sceneRect().height()-dd->y);        QPoint c2((dd->x+ddd->x)/2,sceneRect().height()-ddd->y);        QPoint end(ddd->x,sceneRect().height()-ddd->y);           mPath.cubicTo(c1 ,c2 ,end );         //ui->graphicsView->scene()->addLine((-340+dd->x),(150-dd->y),(-340+ddd->x),(150-ddd->y));         dd=dd->next;    }    double w = sceneRect().width();    double h = sceneRect().height();    double step = w/10;    for(int i=0; i<w; i+=step)    {        update (i,0,step,h);     // QTest::qSleep(100);     // //  qDebug()<<"scene update()"<<i;   }}/*void testView::slot_update(){    //double w = rect().width();    //double h = rect().height();    //double step = w/10;    //for(int i=0; i<w; i+=step)    //{    //  update (i,0,step,h);    //  //QTest::qSleep(1000);    //  qDebug()<<"view update()"<<i;    //}}*///testView::testView( QWidget* parent /*= NULL*/ )  /*  : QGraphicsView(parent){    centerOn(0,0);    setCacheMode(QGraphicsView::CacheBackground);    setRenderHint(QPainter::Antialiasing, true);    //setRenderHint(QPainter::TextAntialiasing, true);    setViewportUpdateMode(QGraphicsView::FullViewportUpdate);}testView::~testView(){}*/void MainWindow::on_pushButton_4_clicked(){   extern Pointtt point;    point.AddPoint(ui->lineEdit->text().toDouble(),ui->lineEdit_2->text().toDouble());    ui->textEdit->moveCursor(QTextCursor::End);    ui->textEdit->textCursor().insertText(ui->lineEdit->text()+","+ui->lineEdit_2->text()+";   \t");    //ui->textEdit->append(ui->lineEdit->text()+","+ui->lineEdit_2->text()+";");      ui->lineEdit->setText("");      ui->lineEdit_2->setText("");    Pointtt::po  *pdd;     pdd=&point.rehead();    // ui->textEdit->setText(QString::number(pdd->next->x));}void MainWindow::on_pushButton_2_clicked(){    ui->graphicsView->scale(1.25,1.25);}void MainWindow::on_pushButton_clicked(){    ui->graphicsView->scale(0.75,0.75);}/*void MainWindow::paintEvent(QPaintEvent *e){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing,true);QPen pen;pen.setColor(Qt::red);pen.setWidth(2);pen.setStyle(Qt::DashDotLine);painter.setPen(pen);//define startpos endposQPoint start_pos(300,50);QPoint end_pos(100,200);//drawPath;QPainterPath path(start_pos);QPoint c1((start_pos+end_pos).x()/2,start_pos.y());QPoint c2((start_pos+end_pos).x()/2,end_pos.y());path.cubicTo(c1 ,c2 ,end_pos );painter.drawPath(path);QPen penshaper;penshaper.setStyle(Qt::SolidLine);penshaper.setColor(Qt::darkRed);painter.setPen(penshaper);//#define the rect size;QSize rect_size(60,60);QRect rect_shape1(start_pos-QPoint(rect_size.height()/2,rect_size.width()/2),rect_size);QRect rect_shape2(end_pos-QPoint(rect_size.height()/2,rect_size.width()/2),rect_size);QBrush brush(Qt::darkGray,Qt::CrossPattern);painter.fillRect(rect_shape1,brush);painter.fillRect(rect_shape2,brush);painter.drawRoundedRect(rect_shape1,10,10);painter.drawRoundedRect(rect_shape2,10,10);QPen dotpen;dotpen.setWidth(8);dotpen.setColor(Qt::red);brush.setStyle(Qt::SolidPattern);painter.setPen(dotpen);painter.drawPoint(start_pos);painter.drawPoint(end_pos);}*/void MainWindow::on_pushButton_3_clicked(){    /*extern Pointtt point;    extern QGraphicsScene scene;    Pointtt::po *dd;    Pointtt::po *ddd;    //ui->graphicsView->scene()->clear();    dd=&point.rehead();    for(ddd=dd->next;ddd!=nullptr;ddd=ddd->next)    {         ui->graphicsView->scene()->addLine((-340+dd->x),(150-dd->y),(-340+ddd->x),(150-ddd->y));         dd=dd->next;    }*/}
储存点源文件
#include"pointsave.h"Pointtt point;void Pointtt ::AddPoint(double xx,double yy){    po *p;    p=new  po;    p->x =xx;    p->y=yy;     po *f;    f=&head;    for(cc=head.next;cc!=nullptr;cc=cc->next)    {        if(p->x<cc->x)        {             po *u;            u=f->next;            f->next=p;            p->next=u;            return ;        }        f=f->next;    }    f->next=p;    p->next=nullptr;}Pointtt::Pointtt(){    this->head.x=0;    this->head.y=0;    this->head.next=nullptr;}Pointtt::po Pointtt::rehead(){    return this->head;}

0 0
原创粉丝点击