抱歉,这个问题不重要,但是我有以下问题:我有
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);
}
关于c++ - 如何使用QComboBox选择如何保存QImage的格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55095133/