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();
    });
}
相关推荐
byxdaz19 小时前
QT中服务使用
开发语言·qt
秋田君1 天前
QT_网络编程体系
网络·arm开发·qt
Quz1 天前
QML TextArea 实战(下):大文本加载与性能优化
qt
Quz1 天前
QML TextArea 实战(中):Markdown 渲染与保持滚动
qt
C++ 老炮儿的技术栈2 天前
基于Qt实现轻量化本地音乐播放器
开发语言·c++·qt·c·播放器·音乐
我不是疯子是傻子2 天前
Qt 实时曲线卡顿优化:从QPainter到OpenGL的3级加速实战
开发语言·qt
秋田君2 天前
QT_XML文件操作
xml·数据库·qt
我不是疯子是傻子2 天前
串口通信数据帧粘包拆包再进化:基于 Qt 的滑动窗口与 CRC 校验实战
开发语言·qt
程序员老陆2 天前
Qt中实现窗口真全屏(覆盖Windows任务栏)
开发语言·windows·qt
CodeKwang2 天前
【三维TSP可视化】1000个点以内的最优路径:最近邻 + 2-opt + 3-opt 组合算法实战(Qt实现)
qt·算法