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();
    });
}
相关推荐
初次见面我叫泰隆12 小时前
Qt——3、常用控件
开发语言·qt·客户端
无小道13 小时前
Qt——QWidget
开发语言·qt
派葛穆15 小时前
Python-PyQt5 安装与配置教程
开发语言·python·qt
初次见面我叫泰隆18 小时前
Qt——4、Qt窗口
开发语言·qt·客户端开发
墨月白19 小时前
[QT]QProcess的相关使用
android·开发语言·qt
小小码农Come on19 小时前
QT信号槽机制原理
开发语言·qt
未来可期LJ21 小时前
【Qt 问题合集】Qt报错:No executable specified 如何解决呢?
qt
LYOBOYI12321 小时前
QML 中 Item、Window、Popup、Rectangle使用手册
qt
qq_401700411 天前
Qt开发过程中遇到哪些经典的bug
qt·bug
SNAKEpc121381 天前
PyQtGraph应用(五):k线回放复盘功能实现
python·qt·pyqt