Qt:图片切割

cpp 复制代码
void MainWindow::on_action_slice_triggered()
{
    QDialog *dialog = new QDialog(this);
    dialog->setWindowTitle("切割");
    dialog->setFixedSize(200, 150);
    QVBoxLayout *vbox = new QVBoxLayout;
    QHBoxLayout *hbox = new QHBoxLayout;
    QLabel *label = new QLabel("方向");
    hbox->addWidget(label);
    QComboBox *comboBox = new QComboBox;
    QStringList SL_direction;
    SL_direction << "竖向" << "横向";
    comboBox->addItems(SL_direction);
    hbox->addWidget(comboBox);
    vbox->addLayout(hbox);
    hbox = new QHBoxLayout;
    label = new QLabel("份数");
    hbox->addWidget(label);
    QLineEdit *lineEdit = new QLineEdit;
    QString sn = settings.value("Slice", "5").toString();
    lineEdit->setText(sn);
    hbox->addWidget(lineEdit);
    vbox->addLayout(hbox);
    label = new QLabel;
    label->setAlignment(Qt::AlignCenter);
    vbox->addWidget(label);
    connect(comboBox, &QComboBox::currentTextChanged, [=]{
        label->setText("");
    });
    connect(lineEdit, &QLineEdit::textChanged, [=]{
        label->setText("");
    });
    QPushButton *pushButton_confirm = new QPushButton("确定");
    QPushButton *pushButton_cancel = new QPushButton("取消");
    hbox = new QHBoxLayout;
    hbox->addStretch();
    hbox->addWidget(pushButton_confirm);
    hbox->addWidget(pushButton_cancel);
    hbox->addStretch();
    vbox->addLayout(hbox);
    dialog->setLayout(vbox);    
    dialog->show();

    connect(pushButton_confirm, &QPushButton::clicked, [=]{
        bool b;
        int n = lineEdit->text().toInt(&b, 10);
        if (b) {
            for (int i=0; i<n; i++) {
                QImage image;
                if (comboBox->currentIndex() == 0) {
                    int dy = imageWidget->image.height() / n;
                    image = imageWidget->image.copy(0, dy * i, imageWidget->image.width(), dy);
                } else if (comboBox->currentIndex() == 1) {
                    int dx = imageWidget->image.width() / n;
                    image = imageWidget->image.copy(dx * i, 0, dx, imageWidget->image.height());
                }
                QString fp = "";
                if (path == "")
                    fp = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + "/" + comboBox->currentText() + QString::number(i) + ".png";
                else
                    fp = QFileInfo(path).path() + "/" + QFileInfo(filename).baseName() + comboBox->currentText() + QString::number(i) + "." + QFileInfo(filename).suffix();
                //qDebug() << fp;
                image.save(fp, nullptr, 100);
                label->setText("切割:" + comboBox->currentText() + " " + QString::number(i+1) + "/" + QString::number(n));
            }
            settings.setValue("Slice", lineEdit->text());
        } else {
            QMessageBox::critical(dialog, "错误", "份数不是数字");
        }
    });

    connect(pushButton_cancel, &QPushButton::clicked, [=]{
        dialog->close();
    });
}
相关推荐
Quz4 天前
QML Hello World 入门示例
qt
xcyxiner7 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner8 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner8 天前
DicomViewer (添加模型类)3
qt
xcyxiner9 天前
DicomViewer (目录调整) 2
qt
xcyxiner9 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能11 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G11 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt
森G11 天前
77、线程池原理和实现------服务器源码解析----云视频服务项目
服务器·c++·qt
森G11 天前
71、打包发布---------打包发布
c++·qt