QT简易画笔

Posted 一颗大白鲸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT简易画笔相关的知识,希望对你有一定的参考价值。

 

 

左键按住绘画,右键清空

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QPoint>
#include <string>
#include <list>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT
     class mesages{
    public:
        int x;
        int y;
        QString msg;

     };
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void mousePressEvent(QMouseEvent *) ;
    void keyPressEvent(QKeyEvent *) ;
    bool EventFilter(QObject *,QEvent *);
    void paintEvent(QPaintEvent  *);
    QPainter *p;
    void getTex();
    QList<mesages> mytextlist;
     void mouseMoveEvent(QMouseEvent *);
private slots:
    void on_pushButton_clicked();

    void on_widget_customContextMenuRequested(const QPoint &pos);

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

主要是鼠标事件绘图事件

#include "widget.h"
#include "ui_widget.h"
#include <QEvent>
#include <qdebug.h>
#include <string>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //    connect(this,SIGNAL(returnPressed()),this,SLOT(on_pushButton_clicked()));
    ui->pushButton->installEventFilter(ui->pushButton);
}

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


void Widget::on_pushButton_clicked()
{
    //    QObject::connect()
    ui->textEdit1->clear();
    //    ui->textEdit1->changeEvent()
    ui->Ui_Widget::pushButton->setText("ssssss");



    //    this->close();
}


void Widget::keyPressEvent(QKeyEvent *event)
{
    int key=event->key();

    qDebug()<<(char)key;
    if (event->key() == Qt::Key_Return && (event->modifiers() & Qt::ControlModifier))
    {

        ui->textEdit1->clear();
        ui->Ui_Widget::pushButton->setText("回车");


    }

}

bool Widget::EventFilter(QObject *q,QEvent *e){

    if(q==(QObject*)ui->pushButton&&((static_cast<QKeyEvent*>(e)->key() == Qt::Key_Return))){

        return  true;
    }
    return QWidget::eventFilter(q,e);
}

void Widget::paintEvent(QPaintEvent  *)
{
        QPainter painter(this);
 p = new QPainter(this);
        painter.drawText(QPoint(300,50),"this is my widget");
       getTex();
//    //绘制一行文字
//    p = new QPainter(this);
//    p->drawText(QPoint(300,50),"this is my widget");
}

void Widget::getTex(){

    foreach (mesages msg, mytextlist) {
        p->drawText(QPoint(msg.x,msg.y),msg.msg);
    }
    p->renderHints();
    this->update();
};

void Widget::mousePressEvent(QMouseEvent *ev)
{
    if( ev->button()==Qt::LeftButton){
        ui->textEdit1->clear();
        ui->Ui_Widget::pushButton->setText("左键");
        mesages msg;
        msg.x=ev->x();
        msg.y=ev->y();
        msg.msg="this is my 左键";
        mytextlist.push_back(msg);
    };

    if( ev->button()==Qt::RightButton){

        ui->textEdit1->clear();
        ui->Ui_Widget::pushButton->setText("右键");
        mytextlist.clear();
        if(ev->modifiers()==Qt::EnterKeyReturn){
            ui->textEdit1->clear();
            ui->Ui_Widget::pushButton->setText("回车");
        }
    }


}

void Widget::mouseMoveEvent(QMouseEvent *ev){

//    if( ev->button()==Qt::LeftButton){};
        ui->textEdit1->clear();
        ui->Ui_Widget::pushButton->setText("左键move");
        mesages msg;
        msg.x=ev->x();
        msg.y=ev->y();
        msg.msg="this is my 左键move";
        mytextlist.push_back(msg);

}

 

 

 

 

以上是关于QT简易画笔的主要内容,如果未能解决你的问题,请参考以下文章

QT 实用代码片段

Qt的简易日志库实现及封装

Qt学习笔记-设计简易的截图工具软件

Qt Widget 之简易串口助手(QSerialPort)

Qt Widget 之简易串口助手(QSerialPort)

OpenCV in QtCreator 简易的OpenCV+Qt 图形框架