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();
    });
}
相关推荐
春蕾夏荷_7282977251 小时前
qt ElaWidgetTools第一个实例
开发语言·qt
韭菜钟9 小时前
在Qt中用cmake实现类似pri文件的功能
开发语言·qt·系统架构
韭菜钟13 小时前
Qt从qmake迁移到cmake的记录
开发语言·qt
永不停转1 天前
详谈 QLayout::SizeConstraint 和 QSizePolicy 对 QWidget 尺寸的影响
c++·qt
王廷胡_白嫖帝1 天前
Qt网络速度测试工具开发教程 - 从零开始构建网络测速应用
开发语言·网络·qt
qq_25929724732 天前
QT-窗口类部件
c++·qt
楚Y6同学2 天前
QT之QSS的使用方法和常用控件的样式设置
开发语言·qt
Zafir20243 天前
Qt实现TabWidget通过addTab函数添加的页,页内控件自适应窗口大小
开发语言·c++·qt·ui
王廷胡_白嫖帝3 天前
Qt文件压缩工具项目开发教程
java·开发语言·qt
牵牛老人3 天前
Qt 插件开发全解析:从接口定义,插件封装,插件调用到插件间的通信
开发语言·qt