QT中在QLabel顯示圖片并且利用鼠標(biāo)點(diǎn)擊畫線問題
在QLabel顯示圖片并且利用鼠標(biāo)點(diǎn)擊畫線
最近在做在Label上顯示圖片并且通過鼠標(biāo)點(diǎn)擊畫線,在網(wǎng)上查了很多零零散散的東西,收獲也多
很多初學(xué)者更希望直接貼代碼,這樣可以模仿來寫,我下面直接貼出我的項(xiàng)目中自己寫的maLabel類
(如果只是實(shí)現(xiàn)利用鼠標(biāo)繪制, 重寫void paintEvent(QPaintEvent *event);void mousePressEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e);即可,其他函數(shù)是我項(xiàng)目需求所以多寫的,可以忽略)
申明myLabel類,繼承QLabel,生成myLabel.h和myLabel.cpp文件
以下為我的代碼,供參考
我只是實(shí)現(xiàn)了畫一條直線,如果要畫多條,可以用vector將之前若干條的信息干存下來,每次都繪制
myLabel.h
#ifndef MYLABEL_H
#define MYLABEL_H
#include <QLabel>
#include <QPoint>
#include <QColor>
#include <QPaintEvent>
#include <QImage>
#include <QPixmap>
class myLabel : public QLabel
{
//Q_OBJECT
public:
myLabel();
//~myLabel();
//繪制線條
virtual void paintEvent(QPaintEvent *event) override;
//鼠標(biāo)按下
void mousePressEvent(QMouseEvent *e);
//鼠標(biāo)移動(dòng)
void mouseMoveEvent(QMouseEvent *e);
//鼠標(biāo)抬起
void mouseReleaseEvent(QMouseEvent *e);
//設(shè)置所畫線條屬性
void setLineColor(const QColor lineColor);
void setLineSize(const int lineSize);
//得到畫線的起點(diǎn)和終點(diǎn)
QPoint getStartPoint();
QPoint getEndPoint();
void clear();
private:
QPoint lineStartPoint; //畫線起點(diǎn)
QPoint lineEndPoint; //畫線終點(diǎn)
QColor lineColor; //線條顏色
int lineSize; //5種線型
bool isPressed;
};
#endif // MYLABEL_HmyLabel.cpp
#include "myLabel.h"
#include <QPen>
#include<QPainter>
myLabel::myLabel()
{
this->lineStartPoint = QPoint(0,0);
this->lineEndPoint = QPoint(0,0);
this->lineColor = QColor(Qt::black);
this->lineSize = 3;
}
//繪制線條
void myLabel::paintEvent(QPaintEvent *event)
{
QLabel::paintEvent(event);//必須有,才能讓背景圖片顯示出來
QPainter painter(this);
QPen pen;
pen.setColor(lineColor);
pen.setWidth(lineSize);
painter.setPen(pen);
painter.drawLine(lineStartPoint,lineEndPoint);
}
//鼠標(biāo)按下
void myLabel::mousePressEvent(QMouseEvent *e)
{
lineStartPoint = e->pos();
lineEndPoint = e->pos();
//在圖片上繪制
isPressed = true;
}
//鼠標(biāo)移動(dòng)
void myLabel::mouseMoveEvent(QMouseEvent *e)
{
if(isPressed)
{
lineEndPoint=e->pos();
update();
}
}
//鼠標(biāo)抬起
void myLabel::mouseReleaseEvent(QMouseEvent *e)
{
isPressed=false;
update();
}
void myLabel::setLineColor(const QColor lineColor)
{
this->lineColor = lineColor;
}
void myLabel::setLineSize(const int lineSize)
{
this->lineSize = lineSize;
}
QPoint myLabel::getStartPoint()
{
return lineStartPoint;
}
QPoint myLabel::getEndPoint()
{
return lineEndPoint;
}
void myLabel::clear()
{
lineStartPoint = QPoint(0,0);
lineEndPoint = QPoint(0,0);
update();
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C++實(shí)現(xiàn)LeetCode數(shù)組練習(xí)題
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode的幾道數(shù)組練習(xí)題,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C語(yǔ)言中分支和循環(huán)的6種實(shí)現(xiàn)形式總結(jié)
C語(yǔ)言時(shí)一門結(jié)構(gòu)化的程序設(shè)計(jì)語(yǔ)言,這篇文章主要介紹了C語(yǔ)言中的分支和循環(huán)的6種實(shí)現(xiàn)形式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04
C語(yǔ)言實(shí)現(xiàn)最大間隙問題實(shí)例
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)最大間隙問題的方法,是一個(gè)比較經(jīng)典的算法設(shè)計(jì)問題,對(duì)于學(xué)習(xí)算法設(shè)計(jì)有一定的借鑒價(jià)值,需要的朋友可以參考下2014-09-09
C語(yǔ)言實(shí)現(xiàn)倉(cāng)庫(kù)物資管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)倉(cāng)庫(kù)物資管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
C++中std::ios_base::floatfield報(bào)錯(cuò)已解決
在C++編程中,設(shè)置浮點(diǎn)數(shù)輸出格式時(shí)可能遇到std::ios_base::floatfield錯(cuò)誤,解決方法包括使用正確的格式化標(biāo)志組合,避免沖突的格式化設(shè)置,以及檢查流狀態(tài)標(biāo)志是否正確,通過這些方法可以有效避免浮點(diǎn)數(shù)格式化錯(cuò)誤,并確保輸出精確2024-09-09

