Sorry if this question is trivial but I have the following problem: I have
N.1 QGraphicsView
N.1 QComboBox
I am trying to save the images uploaded on the QGraphicsView
into a folder on my Desktop choosing the format of the image through a QComboBox
. The loop I wrote it works for a .png file but I am stuck with other different formats as I am not sure how to correctly handle the QComboBox
choice.
See below the snipped of code I am using:
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);
}
This is the QComboBox
that will take care of the format:
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;
}
}
Thanks for helping with this issue. I know it is trivial but I am stuck and wanted to understand how to handle this exception.
You can improve your application if the ComboBox automatically takes the formats that Qt can use to save an image using QImageWriter::supportedImageFormats()
.
In the following example I show the generic method that is to obtain the currentText of the QComboBox:
#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();
}
In your case:
// 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);
}