如何使用QComboBox来选择如何保存QImage的格式

How to use QComboBox to choose how to save the format of a QImage

抱歉,如果这个问题很简单,但我有以下问题:我有

N.1 QGraphicsView

N.1 QComboBox

我正在尝试将上传到 QGraphicsView 的图像保存到桌面上的一个文件夹中,通过 QComboBox 选择图像格式。我编写的循环适用于 .png 文件,但由于我不确定如何正确处理 QComboBox 选择,所以我坚持使用其他不同的格式。

请参阅下面我正在使用的代码片段:

mainwindow.h

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    bool fileExists(QString path);
    void bothPrintScreenBtn(const QString &pathImg, bool checkFolder);

private slots:
    void on_bothPrintScreenBtn_clicked();

private:
    bool Lwrite = true;
    int counterA=0;
    int counterB=0;

mainwindow.cpp

// Checking if the file-A and file-B exists already

bool MainWindow::fileExists(QString path) {
    QFileInfo check_file(path);
    // check if file exists and if yes: Is it really a file and no directory?
    if (check_file.exists() && check_file.isFile()) {
        return true;
    } else {
        return false;
    }
}

void MainWindow::bothPrintScreenBtn(const QString& pathImg, bool checkFolder)
{
    QString outA;
    do{
        outA = pathImg+"/printScreenA/"+ QString::number(counterA)+".png";
        counterA++;
    }
    while((checkFolder && fileExists(outA)));
    QImage imageA = ui->graphicsViewLX->grab().toImage();
    imageA.save(outA);

    QString outB;
    do{
        outB = pathImg+"/printScreenB/"+ QString::number(counterB)+".png";
        counterB++;
    }
    while((checkFolder && fileExists(outB)));
    QImage imageB = ui->graphicsViewRX->grab().toImage();
    imageB.save(outB);
}

void MainWindow::on_bothPrintScreenBtn_clicked()
{
    bothPrintScreenBtn("/home/pathTo/Desktop", !Lwrite);
}

这是 QComboBox 将处理格式:

void MainWindow::on_comboBoxFormat_A_currentIndexChanged(int index)
{
    switch (index)
    {
    case(0):
        // Nothing happens
        break;
    case(1):
        // Choose .tiff format
        break;
    case(2):
        // Choose .tif format
        break;
    case(3):
        // Choose .jpg format
        break;
    case(4):
        // Choose .jpeg format
        break;
    case(5):
        // Choose .png format
        break;
    default:
        break;
    }
}

感谢您帮助解决这个问题。我知道这是微不足道的,但我被卡住了,想了解如何处理这个异常。

如果 ComboBox 自动采用 Qt 可以用来保存图像的格式,您可以改进您的应用程序 QImageWriter::supportedImageFormats()

在下面的示例中,我展示了获取 QComboBox 的 currentText 的通用方法:

#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene *scene = new QGraphicsScene;
    QGraphicsView *view = new QGraphicsView{scene};
    scene->addRect(QRectF(0, 0, 100, 100), QPen(Qt::red), QBrush(Qt::blue));
    scene->addEllipse(QRectF(40, 30, 100, 100), QPen(Qt::green), QBrush(Qt::gray));
    QComboBox *combo_formats = new QComboBox;
    for(const QByteArray & format : QImageWriter::supportedImageFormats()){
        combo_formats->addItem(format);
    }
    QPushButton *save_button = new QPushButton{"Save"};
    QObject::connect(save_button, &QPushButton::clicked,[view, combo_formats](){
       QPixmap pixmap = view->grab();
       QString filename = QString("%1.%2").arg("image").arg(combo_formats->currentText());
       pixmap.save(filename);
    });
    QMainWindow w;
    QWidget *central_widget = new QWidget;
    w.setCentralWidget(central_widget);
    QFormLayout *lay = new QFormLayout{central_widget};
    lay->addRow(view);
    lay->addRow("Select Format:", combo_formats);
    lay->addRow(save_button);
    w.show();
    return a.exec();
}

你的情况:

// constructor

for(const QByteArray & format : QImageWriter::supportedImageFormats()){
    ui->comboBoxFormat_A->addItem(format);
}
// ...

void MainWindow::bothPrintScreenBtn(const QString& pathImg, bool checkFolder)
{
    QString suffix = ui-comboBoxFormat_A->currentText();
    QString outA;
    do{
        outA = QString("%1/printScreenA/%2.%3").arg(pathImg).arg(counterA).arg(suffix);
        counterA++;
    }
    while((checkFolder && fileExists(outA)));
    QPixmap pixmapA = ui->graphicsViewLX->grab().toImage();
    pixmapA.save(outA);

    QString outB;
    do{

        outB = QString("%1/printScreenB/%2.%3").arg(pathImg).arg(counterB).arg(suffix);;
        counterB++;
    }
    while((checkFolder && fileExists(outB)));
    QPixmap pixmapB = ui->graphicsViewRX->grab()
    pixmapB.save(outB);
}