对不起,如果这个问题是微不足道的,但我有以下问题:我有
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的电流文本的通用方法:
#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);
}