更新文档

master
feiyangqingyun 2024-04-02 11:04:42 +08:00
parent 71d20a72ac
commit 2cc581ec69
2 changed files with 134 additions and 117 deletions

View File

@ -4,7 +4,7 @@
2. **视频主页:[https://space.bilibili.com/687803542](https://space.bilibili.com/687803542)** 2. **视频主页:[https://space.bilibili.com/687803542](https://space.bilibili.com/687803542)**
3. **网店地址:[https://shop244026315.taobao.com](https://shop244026315.taobao.com)** 3. **网店地址:[https://shop244026315.taobao.com](https://shop244026315.taobao.com)**
4. **联系方式QQ517216493微信feiyangqingyun推荐加微信。** 4. **联系方式QQ517216493微信feiyangqingyun推荐加微信。**
5. **公 众 号Qt实战(本人)/Qt教程民间/Qt软件官方** 5. **公 众 号Qt实战/Qt入门和进阶/Qt教程/Qt软件**
6. **版本支持所有项目已经全部支持Qt4/5/6所有版本以及后续版本。** 6. **版本支持所有项目已经全部支持Qt4/5/6所有版本以及后续版本。**
7. 监控作品体验:[https://pan.baidu.com/s/1d7TH_GEYl5nOecuNlWJJ7g](https://pan.baidu.com/s/1d7TH_GEYl5nOecuNlWJJ7g) 提取码01jf 7. 监控作品体验:[https://pan.baidu.com/s/1d7TH_GEYl5nOecuNlWJJ7g](https://pan.baidu.com/s/1d7TH_GEYl5nOecuNlWJJ7g) 提取码01jf
8. 其他作品体验:[https://pan.baidu.com/s/1ZxG-oyUKe286LPMPxOrO2A](https://pan.baidu.com/s/1ZxG-oyUKe286LPMPxOrO2A) 提取码o05q 8. 其他作品体验:[https://pan.baidu.com/s/1ZxG-oyUKe286LPMPxOrO2A](https://pan.baidu.com/s/1ZxG-oyUKe286LPMPxOrO2A) 提取码o05q

View File

@ -1,11 +1,11 @@
#include "imagecropperdemo.h" #include "imagecropperdemo.h"
#include <QFormLayout> #include <QFormLayout>
#include <QColorDialog> #include <QColorDialog>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
ImageCropperDemo::ImageCropperDemo(QWidget* parent) : ImageCropperDemo::ImageCropperDemo(QWidget *parent) :
QDialog(parent) QDialog(parent)
{ {
setupLayout(); setupLayout();
@ -16,7 +16,8 @@ ImageCropperDemo::ImageCropperDemo(QWidget* parent) :
} }
void ImageCropperDemo::setupLayout() { void ImageCropperDemo::setupLayout()
{
imgCropperLabel = new ImageCropperLabel(600, 500, this); imgCropperLabel = new ImageCropperLabel(600, 500, this);
imgCropperLabel->setFrameStyle(1); imgCropperLabel->setFrameStyle(1);
@ -27,19 +28,19 @@ void ImageCropperDemo::setupLayout() {
editOriginalImagePath = new QLineEdit(this); editOriginalImagePath = new QLineEdit(this);
btnChooseOriginalImagePath = new QPushButton(this); btnChooseOriginalImagePath = new QPushButton(this);
QHBoxLayout* hOriginalImagePathLayout = new QHBoxLayout(); QHBoxLayout *hOriginalImagePathLayout = new QHBoxLayout();
hOriginalImagePathLayout->addWidget(editOriginalImagePath); hOriginalImagePathLayout->addWidget(editOriginalImagePath);
hOriginalImagePathLayout->addWidget(btnChooseOriginalImagePath); hOriginalImagePathLayout->addWidget(btnChooseOriginalImagePath);
editCropperFixedWidth = new QLineEdit(this); editCropperFixedWidth = new QLineEdit(this);
editCropperFixedHeight = new QLineEdit(this); editCropperFixedHeight = new QLineEdit(this);
QHBoxLayout* hCropperFixedSizeLayout = new QHBoxLayout(); QHBoxLayout *hCropperFixedSizeLayout = new QHBoxLayout();
hCropperFixedSizeLayout->addWidget(editCropperFixedWidth); hCropperFixedSizeLayout->addWidget(editCropperFixedWidth);
hCropperFixedSizeLayout->addWidget(editCropperFixedHeight); hCropperFixedSizeLayout->addWidget(editCropperFixedHeight);
editCropperMinWidth = new QLineEdit("8", this); editCropperMinWidth = new QLineEdit("8", this);
editCropperMinHeight = new QLineEdit("8", this); editCropperMinHeight = new QLineEdit("8", this);
QHBoxLayout* hCropperMinSizeLayout = new QHBoxLayout(); QHBoxLayout *hCropperMinSizeLayout = new QHBoxLayout();
hCropperMinSizeLayout->addWidget(editCropperMinWidth); hCropperMinSizeLayout->addWidget(editCropperMinWidth);
hCropperMinSizeLayout->addWidget(editCropperMinHeight); hCropperMinSizeLayout->addWidget(editCropperMinHeight);
@ -52,17 +53,17 @@ void ImageCropperDemo::setupLayout() {
labelRectBorderColor = new QLabel(this); labelRectBorderColor = new QLabel(this);
btnChooseRectBorderCorlor = new QPushButton(this); btnChooseRectBorderCorlor = new QPushButton(this);
QHBoxLayout* hRectBorderColorLayout = new QHBoxLayout(); QHBoxLayout *hRectBorderColorLayout = new QHBoxLayout();
hRectBorderColorLayout->addWidget(labelRectBorderColor); hRectBorderColorLayout->addWidget(labelRectBorderColor);
hRectBorderColorLayout->addWidget(btnChooseRectBorderCorlor); hRectBorderColorLayout->addWidget(btnChooseRectBorderCorlor);
labelDragSquareColor = new QLabel(this); labelDragSquareColor = new QLabel(this);
btnChooseDragSquareColor = new QPushButton(this); btnChooseDragSquareColor = new QPushButton(this);
QHBoxLayout* hDragSquareColorLayout = new QHBoxLayout(); QHBoxLayout *hDragSquareColorLayout = new QHBoxLayout();
hDragSquareColorLayout->addWidget(labelDragSquareColor); hDragSquareColorLayout->addWidget(labelDragSquareColor);
hDragSquareColorLayout->addWidget(btnChooseDragSquareColor); hDragSquareColorLayout->addWidget(btnChooseDragSquareColor);
QFormLayout* formLayout1 = new QFormLayout(); QFormLayout *formLayout1 = new QFormLayout();
formLayout1->addRow(new QLabel("Preview:"), labelPreviewImage); formLayout1->addRow(new QLabel("Preview:"), labelPreviewImage);
formLayout1->addRow(new QLabel("OriginalImage:", this), hOriginalImagePathLayout); formLayout1->addRow(new QLabel("OriginalImage:", this), hOriginalImagePathLayout);
formLayout1->addRow(new QLabel("OutputShape:", this), comboOutputShape); formLayout1->addRow(new QLabel("OutputShape:", this), comboOutputShape);
@ -70,29 +71,29 @@ void ImageCropperDemo::setupLayout() {
formLayout1->addRow(new QLabel("FixedSize:", this), hCropperFixedSizeLayout); formLayout1->addRow(new QLabel("FixedSize:", this), hCropperFixedSizeLayout);
formLayout1->addRow(new QLabel("MinimumSize:", this), hCropperMinSizeLayout); formLayout1->addRow(new QLabel("MinimumSize:", this), hCropperMinSizeLayout);
QFormLayout* formLayout2 = new QFormLayout(); QFormLayout *formLayout2 = new QFormLayout();
formLayout2->addRow(new QLabel("EnableOpacity:", this), checkEnableOpacity); formLayout2->addRow(new QLabel("EnableOpacity:", this), checkEnableOpacity);
formLayout2->addRow(new QLabel("Opacity:", this), sliderOpacity); formLayout2->addRow(new QLabel("Opacity:", this), sliderOpacity);
QFormLayout* formLayout3 = new QFormLayout(); QFormLayout *formLayout3 = new QFormLayout();
formLayout3->addRow(new QLabel("ShowDragSquare:", this), checkShowDragSquare); formLayout3->addRow(new QLabel("ShowDragSquare:", this), checkShowDragSquare);
formLayout3->addRow(new QLabel("DragSquareEdge:", this), editDragSquareEdge); formLayout3->addRow(new QLabel("DragSquareEdge:", this), editDragSquareEdge);
formLayout3->addRow(new QLabel("DragSquareColor:", this), hDragSquareColorLayout); formLayout3->addRow(new QLabel("DragSquareColor:", this), hDragSquareColorLayout);
QFormLayout* formLayout4 = new QFormLayout(); QFormLayout *formLayout4 = new QFormLayout();
formLayout4->addRow(new QLabel("ShowRectBorder:", this), checkShowRectBorder); formLayout4->addRow(new QLabel("ShowRectBorder:", this), checkShowRectBorder);
formLayout4->addRow(new QLabel("RectBorderColor:", this), hRectBorderColorLayout); formLayout4->addRow(new QLabel("RectBorderColor:", this), hRectBorderColorLayout);
btnSavePreview = new QPushButton("Save", this); btnSavePreview = new QPushButton("Save", this);
btnQuit = new QPushButton("Quit", this); btnQuit = new QPushButton("Quit", this);
QHBoxLayout* btnLayout = new QHBoxLayout(); QHBoxLayout *btnLayout = new QHBoxLayout();
btnLayout->addStretch(); btnLayout->addStretch();
btnLayout->addWidget(btnSavePreview); btnLayout->addWidget(btnSavePreview);
btnLayout->addStretch(); btnLayout->addStretch();
btnLayout->addWidget(btnQuit); btnLayout->addWidget(btnQuit);
btnLayout->addStretch(); btnLayout->addStretch();
QVBoxLayout* vLayout = new QVBoxLayout(); QVBoxLayout *vLayout = new QVBoxLayout();
vLayout->addLayout(formLayout1); vLayout->addLayout(formLayout1);
vLayout->addStretch(); vLayout->addStretch();
vLayout->addLayout(formLayout2); vLayout->addLayout(formLayout2);
@ -108,7 +109,8 @@ void ImageCropperDemo::setupLayout() {
mainLayout->addLayout(vLayout); mainLayout->addLayout(vLayout);
} }
void ImageCropperDemo::init() { void ImageCropperDemo::init()
{
imgCropperLabel->setRectCropper(); imgCropperLabel->setRectCropper();
editCropperFixedWidth->setEnabled(false); editCropperFixedWidth->setEnabled(false);
editCropperFixedHeight->setEnabled(false); editCropperFixedHeight->setEnabled(false);
@ -195,11 +197,13 @@ void ImageCropperDemo::init() {
* *
*****************************************************************************/ *****************************************************************************/
void ImageCropperDemo::onChooseOriginalImage() { void ImageCropperDemo::onChooseOriginalImage()
{
QString filename = QFileDialog::getOpenFileName(this, "Select a picture", "", QString filename = QFileDialog::getOpenFileName(this, "Select a picture", "",
"picture (*.jpg *.png *.bmp)"); "picture (*.jpg *.png *.bmp)");
if (filename.isNull()) if (filename.isNull()) {
return; return;
}
QPixmap pixmap; QPixmap pixmap;
if (!pixmap.load(filename)) { if (!pixmap.load(filename)) {
@ -214,16 +218,19 @@ void ImageCropperDemo::onChooseOriginalImage() {
labelPreviewImage->setFrameStyle(0); labelPreviewImage->setFrameStyle(0);
} }
void ImageCropperDemo::onOutputShapeChanged(int idx) { void ImageCropperDemo::onOutputShapeChanged(int idx)
{
// Output: Rectangular // Output: Rectangular
if (idx == 0) if (idx == 0) {
imgCropperLabel->setOutputShape(OutputShape::RECT); imgCropperLabel->setOutputShape(OutputShape::RECT);
else } else {
imgCropperLabel->setOutputShape(OutputShape::ELLIPSE); imgCropperLabel->setOutputShape(OutputShape::ELLIPSE);
}
onUpdatePreview(); onUpdatePreview();
} }
void ImageCropperDemo::onCropperShapeChanged(int idx) { void ImageCropperDemo::onCropperShapeChanged(int idx)
{
switch (CropperShape(idx + 1)) { switch (CropperShape(idx + 1)) {
case CropperShape::RECT: { case CropperShape::RECT: {
imgCropperLabel->setRectCropper(); imgCropperLabel->setRectCropper();
@ -302,25 +309,25 @@ void ImageCropperDemo::onCropperShapeChanged(int idx) {
onUpdatePreview(); onUpdatePreview();
} }
void ImageCropperDemo::onEnableOpacityChanged(int state) { void ImageCropperDemo::onEnableOpacityChanged(int state)
{
if (state == Qt::Checked) { if (state == Qt::Checked) {
sliderOpacity->setEnabled(true); sliderOpacity->setEnabled(true);
imgCropperLabel->enableOpacity(true); imgCropperLabel->enableOpacity(true);
} } else {
else {
sliderOpacity->setEnabled(false); sliderOpacity->setEnabled(false);
imgCropperLabel->enableOpacity(false); imgCropperLabel->enableOpacity(false);
} }
imgCropperLabel->update(); imgCropperLabel->update();
} }
void ImageCropperDemo::onShowDragSquareChanged(int state) { void ImageCropperDemo::onShowDragSquareChanged(int state)
{
if (state == Qt::Checked) { if (state == Qt::Checked) {
editDragSquareEdge->setEnabled(true); editDragSquareEdge->setEnabled(true);
btnChooseDragSquareColor->setEnabled(true); btnChooseDragSquareColor->setEnabled(true);
imgCropperLabel->setShowDragSquare(true); imgCropperLabel->setShowDragSquare(true);
} } else {
else {
editDragSquareEdge->setEnabled(false); editDragSquareEdge->setEnabled(false);
btnChooseDragSquareColor->setEnabled(false); btnChooseDragSquareColor->setEnabled(false);
imgCropperLabel->setShowDragSquare(false); imgCropperLabel->setShowDragSquare(false);
@ -328,49 +335,56 @@ void ImageCropperDemo::onShowDragSquareChanged(int state) {
imgCropperLabel->update(); imgCropperLabel->update();
} }
void ImageCropperDemo::onDragSquareEdgeChanged(QString edge) { void ImageCropperDemo::onDragSquareEdgeChanged(QString edge)
{
imgCropperLabel->setDragSquareEdge(edge.toInt()); imgCropperLabel->setDragSquareEdge(edge.toInt());
imgCropperLabel->update(); imgCropperLabel->update();
} }
void ImageCropperDemo::onOpacityChanged(int val) { void ImageCropperDemo::onOpacityChanged(int val)
{
imgCropperLabel->setOpacity(val / 100.0); imgCropperLabel->setOpacity(val / 100.0);
imgCropperLabel->update(); imgCropperLabel->update();
} }
void ImageCropperDemo::onFixedWidthChanged(QString width) { void ImageCropperDemo::onFixedWidthChanged(QString width)
{
imgCropperLabel->setCropperFixedWidth(width.toInt()); imgCropperLabel->setCropperFixedWidth(width.toInt());
imgCropperLabel->update(); imgCropperLabel->update();
} }
void ImageCropperDemo::onFixedHeightChanged(QString height) { void ImageCropperDemo::onFixedHeightChanged(QString height)
{
imgCropperLabel->setCropperFixedHeight(height.toInt()); imgCropperLabel->setCropperFixedHeight(height.toInt());
imgCropperLabel->update(); imgCropperLabel->update();
} }
void ImageCropperDemo::onMinWidthChanged(QString width) { void ImageCropperDemo::onMinWidthChanged(QString width)
{
imgCropperLabel->setCropperMinimumWidth(width.toInt()); imgCropperLabel->setCropperMinimumWidth(width.toInt());
imgCropperLabel->update(); imgCropperLabel->update();
} }
void ImageCropperDemo::onMinHeightChanged(QString height) { void ImageCropperDemo::onMinHeightChanged(QString height)
{
imgCropperLabel->setMinimumHeight(height.toInt()); imgCropperLabel->setMinimumHeight(height.toInt());
imgCropperLabel->update(); imgCropperLabel->update();
} }
void ImageCropperDemo::onShowRectBorder(int state) { void ImageCropperDemo::onShowRectBorder(int state)
{
if (state == Qt::Checked) { if (state == Qt::Checked) {
btnChooseRectBorderCorlor->setEnabled(true); btnChooseRectBorderCorlor->setEnabled(true);
imgCropperLabel->setShowRectBorder(true); imgCropperLabel->setShowRectBorder(true);
} } else {
else {
btnChooseRectBorderCorlor->setEnabled(false); btnChooseRectBorderCorlor->setEnabled(false);
imgCropperLabel->setShowRectBorder(false); imgCropperLabel->setShowRectBorder(false);
} }
imgCropperLabel->update(); imgCropperLabel->update();
} }
void ImageCropperDemo::onChooseRectBorderColor() { void ImageCropperDemo::onChooseRectBorderColor()
{
QColor color = QColorDialog::getColor(imgCropperLabel->getBorderPen().color(), this); QColor color = QColorDialog::getColor(imgCropperLabel->getBorderPen().color(), this);
if (color.isValid()) { if (color.isValid()) {
setLabelColor(labelRectBorderColor, color); setLabelColor(labelRectBorderColor, color);
@ -381,7 +395,8 @@ void ImageCropperDemo::onChooseRectBorderColor() {
} }
} }
void ImageCropperDemo::onChooseDragSquareColor() { void ImageCropperDemo::onChooseDragSquareColor()
{
QColor color = QColorDialog::getColor(Qt::white, this); QColor color = QColorDialog::getColor(Qt::white, this);
if (color.isValid()) { if (color.isValid()) {
setLabelColor(labelDragSquareColor, color); setLabelColor(labelDragSquareColor, color);
@ -390,26 +405,28 @@ void ImageCropperDemo::onChooseDragSquareColor() {
} }
} }
void ImageCropperDemo::onUpdatePreview() { void ImageCropperDemo::onUpdatePreview()
{
QPixmap preview = imgCropperLabel->getCroppedImage(); QPixmap preview = imgCropperLabel->getCroppedImage();
preview = preview.scaled(labelPreviewImage->width(), labelPreviewImage->height(), preview = preview.scaled(labelPreviewImage->width(), labelPreviewImage->height(),
Qt::KeepAspectRatio, Qt::SmoothTransformation); Qt::KeepAspectRatio, Qt::SmoothTransformation);
labelPreviewImage->setPixmap(preview); labelPreviewImage->setPixmap(preview);
} }
void ImageCropperDemo::onSaveCroppedImage() { void ImageCropperDemo::onSaveCroppedImage()
const QPixmap* pixmap = labelPreviewImage->pixmap(); {
if (!pixmap) { if (!labelPreviewImage->pixmap()) {
QMessageBox::information(this, "Error", "There is no cropped image to save.", QMessageBox::Ok); QMessageBox::information(this, "Error", "There is no cropped image to save.", QMessageBox::Ok);
return ; return ;
} }
QString filename = QFileDialog::getSaveFileName(this, "Save cropped image", "", "picture (*.png)"); QString filename = QFileDialog::getSaveFileName(this, "Save cropped image", "", "picture (*.png)");
if (!filename.isNull()) { if (!filename.isNull()) {
if (imgCropperLabel->getCroppedImage().save(filename, "PNG")) if (imgCropperLabel->getCroppedImage().save(filename, "PNG")) {
QMessageBox::information(this, "Prompt", "Saved successfully", QMessageBox::Ok); QMessageBox::information(this, "Prompt", "Saved successfully", QMessageBox::Ok);
else } else {
QMessageBox::information(this, "Error", "Save image failed!", QMessageBox::Ok); QMessageBox::information(this, "Error", "Save image failed!", QMessageBox::Ok);
} }
}
} }