Qt基礎編程 繪製座標軸

<img src="https://img-blog.csdn.net/20150425172635867?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlxdWFuMDAxNzg5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
//mainwindow.cpp
#include "pointsave.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
<span style="font-family: Arial, Helvetica, sans-serif;">#include <QString></span>
#include<QGraphicsView>
#include<QGraphicsScene>
#include<QGraphicsLineItem>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
 //   QGraphicsLineItem *item = new QGraphicsLineItem(-100,100,0,0);

    QGraphicsScene *scene=new QGraphicsScene();
scene->addLine(-340,150,-340,-150);
scene->addLine(-340,-150,-343,-146);
scene->addLine(-340,-150,-337,-146);
scene->addLine(-340,150,340,150);
scene->addLine(340,150,336,147);
scene->addLine(340,150,336,153);
for(int i=-240;i<350;i+=100)
{
    scene->addLine(i,150,i,-150);


}
for(int i=150;i>-150;i-=100)
{
    scene->addLine(-340,i,340,i);
}

    scene->setSceneRect(-(ui->graphicsView->width()/2),-(ui->graphicsView->height()),ui->graphicsView->width()*2,ui->graphicsView->height()*2);

    ui->graphicsView->setScene(scene);

}

MainWindow::~MainWindow()
{
    delete ui;

}

void MainWindow::on_pushButton_4_clicked()
{
   extern Pointtt point;
    point.AddPoint(ui->lineEdit->text().toInt(),ui->lineEdit_2->text().toInt());
    ui->textEdit->append(ui->lineEdit->text()+","+ui->lineEdit_2->text()+";");
     Pointtt::po  *pdd;
     pdd=&point.rehead();

    // ui->textEdit->setText(QString::number(pdd->next->x));

}

void MainWindow::on_pushButton_2_clicked()
{
    ui->graphicsView->scale(1.5,1.5);
}



void MainWindow::on_pushButton_clicked()
{
    ui->graphicsView->scale(0.5,0.5);
}

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;
    }
}
<pre name="code" class="cpp">//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~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;
};

#endif // MAINWINDOW_H



//pointsave.h
#ifndef POINTSAVE
#define POINTSAVE

class Pointtt
{
public:
    struct po
    {
        int x;
        int y;
        struct po *next;
    };
private:
     struct po *cc;
     struct po head;


public:
     Pointtt();
     struct po rehead();
    void AddPoint(int xx,int yy);

};

#endif // POINTSAVE


//pointsave.cpp
#include"pointsave.h"
Pointtt point;

void Pointtt ::AddPoint(int xx,int 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;
}


//mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>711</width>
    <height>420</height>
   </rect>
  </property>
  <property name="minimumSize">
   <size>
    <width>711</width>
    <height>420</height>
   </size>
  </property>
  <property name="maximumSize">
   <size>
    <width>711</width>
    <height>420</height>
   </size>
  </property>
  <property name="sizeIncrement">
   <size>
    <width>711</width>
    <height>420</height>
   </size>
  </property>
  <property name="baseSize">
   <size>
    <width>711</width>
    <height>420</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QGraphicsView" name="graphicsView">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>711</width>
      <height>331</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>340</y>
      <width>21</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>-</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>340</y>
      <width>21</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>+</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_3">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>380</y>
      <width>51</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>build</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="textEdit">
    <property name="geometry">
     <rect>
      <x>250</x>
      <y>330</y>
      <width>461</width>
      <height>91</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>111</x>
      <y>340</y>
      <width>16</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>x:</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>111</x>
      <y>357</y>
      <width>16</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>y:</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>129</x>
      <y>340</y>
      <width>91</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit_2">
    <property name="geometry">
     <rect>
      <x>129</x>
      <y>357</y>
      <width>91</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_4">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>380</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>addpoint</string>
    </property>
   </widget>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章