对不起,如果这个问题很简单,但我有以下问题:我有
N.1 QGraphicsView
N.1 QComboBox
我正在尝试将上传到QGraphicsView上的图像保存到桌面上的文件夹中…
如果ComboBox自动采用Qt可用于保存图像的格式,则可以改进应用程序 QImageWriter::supportedImageFormats() 。
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); }