[英]How to use QComboBox to choose how to save the format of a QImage
對不起,如果這個問題是微不足道的,但我有以下問題:我有
N.1 QGraphicsView
N.1 QComboBox
我正在嘗試將QGraphicsView
上傳的圖像保存到桌面上的文件夾中,通過QComboBox
選擇圖像格式。 我編寫的循環適用於 .png 文件,但我堅持使用其他不同的格式,因為我不確定如何正確處理QComboBox
選擇。
請參閱下面我正在使用的代碼片段:
主窗口.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;
主窗口.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 使用QImageWriter::supportedImageFormats()
自動采用 Qt 可用於保存圖像的格式,則您可以改進您的應用程序。
在下面的示例中,我展示了獲取 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.