QtはQLabelに表示する図案に対して4本の線を使ってピクチャーを裁断します――構想と核心コードを添付します
76642 ワード
文書ディレクトリ1効果 2構想 3コアコード 1効果
2考え方
QLabelは必ず表示される画像と同じ大きさに設定し、QLabelに対する位置を取得しやすくし、順番に画像を正確に切り取る.
QLabelにイベントフィルタをインストールし、イベントフィルタイベントを書き換える
3コアコード
クラス.h
//.cpp
2考え方
QLabelは必ず表示される画像と同じ大きさに設定し、QLabelに対する位置を取得しやすくし、順番に画像を正確に切り取る.
QLabelにイベントフィルタをインストールし、イベントフィルタイベントを書き換える
3コアコード
クラス.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_clicked();
private:
bool eventFilter(QObject *obj, QEvent *event);//
//
int nowX;
int nowY;
//
int y1Pos = 30;
//int y2Pos = 34;
int spacing = 4;//
//
int x1Pos = 50;
int x2Pos = 280;
//
bool isMoveY = false;//
bool isMoveX1 = false;
bool isMoveX2 = false;
int multiple = 4;//
QImage tempTestImg;//
void testImg();//
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
//.cpp
#include "widget.h"
#include "ui_widget.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
testImg();//
// ( 。。。)
[video(video-UtA8d4pa-1591888853573)(type-edu_course)(url-https://edu.csdn.net/course/blogPlay?goods_id=14277&blog_creator=qq_33375598&marketing_id=84)(image-https://img-bss.csdnimg.cn/2020422112656862_91070.png?imageMogr2/auto-orient/thumbnail/400x269!/format/png)(title-150 Python )]
ui->imageLabel->installEventFilter(this);//
// QPainter painter;
// QImage tempImg = tempTestImg;
// painter.begin(&tempImg);
// painter.setPen(QPen(Qt::red, 2, Qt::DashDotLine, Qt::RoundCap));
// // painter.drawRect(x1, y1, x2 - x1, y2 - y1);
// painter.drawLine(0, 20, 10, 40);
// painter.end();
// ui->imageLabel->setPixmap(QPixmap::fromImage(tempImg));
}
Widget::~Widget()
{
delete ui;
}
//
void Widget::testImg(){
QImage imageData;
imageData.load("1.png");
imageData = imageData.scaled(imageData.width()*multiple, imageData.height()*multiple, Qt::KeepAspectRatio, Qt::SmoothTransformation);
tempTestImg = imageData;
QPixmap resImage = QPixmap::fromImage(imageData);
//resImage.scaled(ui->imageLabel->size(), Qt::IgnoreAspectRatio);
ui->imageLabel->setScaledContents(true);
QSize size;
size.setWidth(resImage.width());
size.setHeight(resImage.height());
ui->imageLabel->resize(size);
// ui->imageLabel->setGeometry(40, 60, resImage.width()*6, resImage.height()*6);
ui->imageLabel->setPixmap(resImage);
qDebug()<<"imageLabel:width:"<<ui->imageLabel->width();
qDebug()<<"imageLabel:height:"<<ui->imageLabel->height();
qDebug()<<"imageLabel:x:"<<ui->imageLabel->x();
qDebug()<<"imageLabel:y:"<<ui->imageLabel->y();
QPainter painter;
QImage tempImg = tempTestImg;
painter.begin(&tempImg);
painter.setPen(QPen(Qt::red, 2, Qt::DashDotLine, Qt::RoundCap));//DashDotLine
// painter.drawRect(x1, y1, x2 - x1, y2 - y1);
painter.drawLine(0, y1Pos, tempImg.width(), y1Pos);//
painter.drawLine(0, y1Pos + spacing, tempImg.width(), y1Pos + spacing);//
painter.drawLine(x1Pos, 0, x1Pos, tempImg.height());//
painter.drawLine(x2Pos, 0, x2Pos, tempImg.height());//
painter.end();
ui->imageLabel->setPixmap(QPixmap::fromImage(tempImg));
}
//
bool Widget::eventFilter(QObject *obj, QEvent *event){
if(obj == ui->imageLabel){
if(event->type() == QEvent::MouseButtonPress){
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
if(mouseEvent->button() == Qt::LeftButton){
QPoint pos = QCursor::pos();//
//QPoint sPoint1 = mouseEvent->globalPos();//
QPoint pos2 = ui->imageLabel->mapFromGlobal(pos);//
nowX = pos2.x();
nowY = pos2.y();
qDebug()<<" x :"<<nowX;
qDebug()<<" y :"<<nowY;
if(nowY == y1Pos || nowY == y1Pos + 1 || nowY == y1Pos - 1 ||
nowY == y1Pos + spacing -1 || nowY == y1Pos + spacing || nowY == y1Pos + spacing + 1){
isMoveY = true;
}else if(nowX == x1Pos || nowX == x1Pos - 1 || nowX == x1Pos + 1){
isMoveX1 = true;
}else if(nowX == x2Pos || nowX == x2Pos - 1 || nowX == x2Pos + 1){
isMoveX2 = true;
}
return true;
}else{
return false;
}
}else if(event->type() == QEvent::MouseMove){
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
if(mouseEvent->buttons() == Qt::LeftButton){
if(isMoveY == true){
QPoint pos = QCursor::pos();//
//QPoint sPoint1 = mouseEvent->globalPos();//
QPoint pos2 = ui->imageLabel->mapFromGlobal(pos);//
y1Pos = pos2.y();
QPainter painter;
QImage tempImg = tempTestImg;
painter.begin(&tempImg);
painter.setPen(QPen(Qt::red, 2, Qt::DashDotLine, Qt::RoundCap));//DashDotLine
// painter.drawRect(x1, y1, x2 - x1, y2 - y1);
painter.drawLine(0, y1Pos, tempImg.width(), y1Pos);//
painter.drawLine(0, y1Pos + spacing , tempImg.width(), y1Pos + spacing );//
painter.drawLine(x1Pos, 0, x1Pos, tempImg.height());//
painter.drawLine(x2Pos, 0, x2Pos, tempImg.height());//
painter.end();
ui->imageLabel->setPixmap(QPixmap::fromImage(tempImg));
}else if(isMoveX1 == true){
QPoint pos = QCursor::pos();//
//QPoint sPoint1 = mouseEvent->globalPos();//
QPoint pos2 = ui->imageLabel->mapFromGlobal(pos);//
x1Pos = pos2.x();
QPainter painter;
QImage tempImg = tempTestImg;
painter.begin(&tempImg);
painter.setPen(QPen(Qt::red, 2, Qt::DashDotLine, Qt::RoundCap));//DashDotLine
// painter.drawRect(x1, y1, x2 - x1, y2 - y1);
painter.drawLine(0, y1Pos, tempImg.width(), y1Pos);//
painter.drawLine(0, y1Pos + spacing, tempImg.width(), y1Pos + spacing);//
painter.drawLine(x1Pos, 0, x1Pos, tempImg.height());//
painter.drawLine(x2Pos, 0, x2Pos, tempImg.height());//
painter.end();
ui->imageLabel->setPixmap(QPixmap::fromImage(tempImg));
}else if(isMoveX2 == true){
QPoint pos = QCursor::pos();//
//QPoint sPoint1 = mouseEvent->globalPos();//
QPoint pos2 = ui->imageLabel->mapFromGlobal(pos);//
x2Pos = pos2.x();
QPainter painter;
QImage tempImg = tempTestImg;
painter.begin(&tempImg);
painter.setPen(QPen(Qt::red, 2, Qt::DashDotLine, Qt::RoundCap));//DashDotLine
// painter.drawRect(x1, y1, x2 - x1, y2 - y1);
painter.drawLine(0, y1Pos, tempImg.width(), y1Pos);//
painter.drawLine(0, y1Pos + spacing, tempImg.width(), y1Pos + spacing);//
painter.drawLine(x1Pos, 0, x1Pos, tempImg.height());//
painter.drawLine(x2Pos, 0, x2Pos, tempImg.height());//
painter.end();
ui->imageLabel->setPixmap(QPixmap::fromImage(tempImg));
}
return true;
}else{
return false;
}
}
else if(event->type() == QEvent::MouseButtonRelease){
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
if(mouseEvent->button() == Qt::LeftButton){
if(isMoveY == true){
QPoint pos = QCursor::pos();//
//QPoint sPoint1 = mouseEvent->globalPos();//
QPoint pos2 = ui->imageLabel->mapFromGlobal(pos);//
y1Pos = pos2.y();
QPainter painter;
QImage tempImg = tempTestImg;
painter.begin(&tempImg);
painter.setPen(QPen(Qt::red, 2, Qt::DashDotLine, Qt::RoundCap));//DashDotLine
// painter.drawRect(x1, y1, x2 - x1, y2 - y1);
painter.drawLine(0, y1Pos, tempImg.width(), y1Pos);//
painter.drawLine(0, y1Pos + spacing, tempImg.width(), y1Pos + spacing);//
painter.drawLine(x1Pos, 0, x1Pos, tempImg.height());//
painter.drawLine(x2Pos, 0, x2Pos, tempImg.height());//
painter.end();
ui->imageLabel->setPixmap(QPixmap::fromImage(tempImg));
isMoveY = false;
}else if(isMoveX1 == true){
QPoint pos = QCursor::pos();//
//QPoint sPoint1 = mouseEvent->globalPos();//
QPoint pos2 = ui->imageLabel->mapFromGlobal(pos);//
x1Pos = pos2.x();
QPainter painter;
QImage tempImg = tempTestImg;
painter.begin(&tempImg);
painter.setPen(QPen(Qt::red, 2, Qt::DashDotLine, Qt::RoundCap));//DashDotLine
// painter.drawRect(x1, y1, x2 - x1, y2 - y1);
painter.drawLine(0, y1Pos, tempImg.width(), y1Pos);//
painter.drawLine(0, y1Pos + spacing, tempImg.width(), y1Pos + spacing);//
painter.drawLine(x1Pos, 0, x1Pos, tempImg.height());//
painter.drawLine(x2Pos, 0, x2Pos, tempImg.height());//
painter.end();
ui->imageLabel->setPixmap(QPixmap::fromImage(tempImg));
isMoveX1 = false;
}else if(isMoveX2 == true){
QPoint pos = QCursor::pos();//
//QPoint sPoint1 = mouseEvent->globalPos();//
QPoint pos2 = ui->imageLabel->mapFromGlobal(pos);//
x2Pos = pos2.x();
QPainter painter;
QImage tempImg = tempTestImg;
painter.begin(&tempImg);
painter.setPen(QPen(Qt::red, 2, Qt::DashDotLine, Qt::RoundCap));//DashDotLine
// painter.drawRect(x1, y1, x2 - x1, y2 - y1);
painter.drawLine(0, y1Pos, tempImg.width(), y1Pos);//
painter.drawLine(0, y1Pos + spacing, tempImg.width(), y1Pos + spacing);//
painter.drawLine(x1Pos, 0, x1Pos, tempImg.height());//
painter.drawLine(x2Pos, 0, x2Pos, tempImg.height());//
painter.end();
ui->imageLabel->setPixmap(QPixmap::fromImage(tempImg));
isMoveX2 = false;
}
return true;
}else{
return false;
}
}
else{
return false;
}
}
else {
return QWidget::eventFilter(obj, event);
}
}
// button
void Widget::on_pushButton_clicked()
{
qDebug()<<"x1:"<<x1Pos<<"y1:"<<y1Pos<<"width:"<<x2Pos - x1Pos<<"height:"<<4;
QImage tempImg = tempTestImg.copy(x1Pos, y1Pos, x2Pos - x1Pos, 4);
ui->imageLabel->resize(tempImg.width(),tempImg.height());
ui->imageLabel->setScaledContents(true);
ui->imageLabel->setPixmap(QPixmap::fromImage(tempImg));
qDebug()<<" :"<<tempImg.width()<<" :"<<tempImg.height();
}