QT5 窗体绘图实验

 QT5 窗体绘图实验:点击按钮绘制对应图形

实验要求:

用Qt编写支持中文的mainwindow窗体,在窗体上使用5个按钮分别画坐标系,画方框,椭圆,输出字符串和显示位图。

实验设计:

选用绘图实现方法:在QLabel控件上绘图。

(网上这样的案例似乎没有多少,本人只是一个菜菜的在校生,代码还有很多可以改进的地方,直接放源码,希望可以给你一些实现的思路。)

先看看实验效果

代码如下:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QVector>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();


private:
    Ui::MainWindow *ui;

protected:
    void myDraw(QLabel *label);
    bool eventFilter(QObject *watched, QEvent *event);
    int flag[10]={0};
    QVector<QLabel *> labels;
};

#endif // MAINWINDOW_H

mainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QPainter>
#include <QVector>
#include <QPen>
#include <QRect>
#include <QBrush>
#include <QFont>
#include <QBitmap>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    labels.push_back(ui->axisArea);
    labels.push_back(ui->ellipseArea);
    labels.push_back(ui->rectArea);
    labels.push_back(ui->strArea);
    labels.push_back(ui->imgArea);
    connect(ui->axisButton,&QPushButton::clicked,this,[&]{
        flag[0] = (flag[0]+1)%2;
        update();
    });
    connect(ui->ellipseButton,&QPushButton::clicked,this,[&]{
        flag[1] = (flag[1]+1)%2;
        update();
    });
    connect(ui->rectButton,&QPushButton::clicked,this,[&]{
        flag[2] = (flag[2]+1)%2;
        update();
    });
    connect(ui->strButton,&QPushButton::clicked,this,[&]{
        flag[3] = (flag[3]+1)%2;
        update();
    });
    connect(ui->imgButton,&QPushButton::clicked,this,[&]{
        flag[4] = (flag[4]+1)%2;
        update();
    });


}

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

void MainWindow::myDraw(QLabel *label){
    QPainter painter(label);
    int x1=30;
    int y1=40;
    int x2=30;
    int y2=350;
    int x3=350;
    int y3=350;
    int x,y,width,height;
    if(label == ui->axisArea){
        //坐标轴
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setRenderHint(QPainter::TextAntialiasing);
        painter.drawLine(x1, y1, x2, y2);
        painter.drawLine(x2, y2, x3, y3);

        QFont font1("宋体",13,QFont::Bold,true);
        painter.setFont(font1);
        painter.setPen(Qt::black);
        painter.drawText(x3-50,y3-10,tr("横坐标"));
        painter.rotate(90);
        painter.drawText(x1,-y1-10,tr("纵坐标"));
    }
    else if(label == ui->ellipseArea){
        //椭圆
        x=x1+60;
        y=y1+100;
        width =70;
        height = 130;
        QRect rect2(x,y,width,height);
        QPen pen2;
        pen2.setColor(Qt::white);
        pen2.setWidth(5);
        painter.setPen(pen2);
        QBrush brush;
        brush.setColor(Qt::blue);
        brush.setStyle(Qt::SolidPattern);
        painter.setBrush(brush);
        painter.drawEllipse(rect2);
    }
    else if(label == ui->rectArea){
        //矩形
        x=x1+60;
        y=y1;
        width=130;
        height=70;
        QRect rect1(x, y, width, height);
        QPen pen1;
        pen1.setColor(Qt::red);
        painter.setPen(pen1);
        painter.drawRect(rect1);
    }
    else if(label == ui->strArea){
        //文字
        painter.setPen(Qt::blue);
        painter.drawText(x1+210,y1+20,tr("QT欢迎"));
    }
    else if(label == ui->imgArea){
        //图片
        x = x1+210;
        y = y1+100;
        width = 70;
        height = 100;
        QRect rect3(x, y, width, height);
        QBitmap image(":/img/windows.jpg");
        painter.drawPixmap(rect3,image);
    }

}

bool MainWindow::eventFilter(QObject *watched, QEvent *event){
    if(event->type() == QEvent::Paint)//绘图事件
        {
            for(int i=0; i<labels.size();i++){
                if(watched == labels[i]){
                    if(1==flag[i]){
                        myDraw(labels[i]);

                    }
                }
            }
            return QMainWindow::eventFilter(watched,event);
        }
    else
        return QMainWindow::eventFilter(watched,event);
}

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    a.installEventFilter(&w);
    return a.exec();
}

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>690</width>
    <height>514</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QPushButton" name="axisButton">
    <property name="geometry">
     <rect>
      <x>540</x>
      <y>20</y>
      <width>100</width>
      <height>33</height>
     </rect>
    </property>
    <property name="text">
     <string>坐标轴</string>
    </property>
   </widget>
   <widget class="QLabel" name="axisArea">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>531</width>
      <height>441</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
   <widget class="QPushButton" name="ellipseButton">
    <property name="geometry">
     <rect>
      <x>540</x>
      <y>60</y>
      <width>100</width>
      <height>33</height>
     </rect>
    </property>
    <property name="text">
     <string>椭圆</string>
    </property>
   </widget>
   <widget class="QLabel" name="ellipseArea">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>531</width>
      <height>441</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
   <widget class="QPushButton" name="rectButton">
    <property name="geometry">
     <rect>
      <x>540</x>
      <y>110</y>
      <width>100</width>
      <height>33</height>
     </rect>
    </property>
    <property name="text">
     <string>矩形</string>
    </property>
   </widget>
   <widget class="QPushButton" name="strButton">
    <property name="geometry">
     <rect>
      <x>540</x>
      <y>150</y>
      <width>100</width>
      <height>33</height>
     </rect>
    </property>
    <property name="text">
     <string>字符串</string>
    </property>
   </widget>
   <widget class="QPushButton" name="imgButton">
    <property name="geometry">
     <rect>
      <x>540</x>
      <y>200</y>
      <width>100</width>
      <height>33</height>
     </rect>
    </property>
    <property name="text">
     <string>图片</string>
    </property>
   </widget>
   <widget class="QLabel" name="rectArea">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>531</width>
      <height>441</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
   <widget class="QLabel" name="strArea">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>531</width>
      <height>441</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
   <widget class="QLabel" name="imgArea">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>531</width>
      <height>441</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>690</width>
     <height>29</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值