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();
    });
}
相关推荐
A.A呐4 分钟前
【QT第五章】系统相关
开发语言·qt
sycmancia27 分钟前
Qt——Qt中的标准对话框
开发语言·qt
freshman_y20 小时前
Qtcreator怎么新建安卓项目?编写一个五子棋游戏APP?
android·qt
wljy11 天前
Qt入门(一)
开发语言·qt
火山上的企鹅1 天前
QT/QGroundControl 实战:Mission Planner 航线在 QGC 中出现 Takeoff 落到 (0,0) 的排查与修复
qt·mp·qgc·无人机开发
雪的季节1 天前
qt信号槽跨线程使用时候的坑
java·开发语言·qt
yy_xzz1 天前
【Qt 开发笔记】能扛住断电、多线程的通用配置类(移植直接用)
笔记·qt
丁劲犇1 天前
改造传统Qt6Widgets程序为多会话MCPServer生产力工具-技巧与实现
qt·ai·agent·并发·mcp·mcpserver·widgets
sycmancia1 天前
Qt——对话框及其类型
开发语言·qt
sycmancia1 天前
Qt——登录对话框
开发语言·qt