QImageWriter::supportedMimeTypes() jpeg



  • Hi,
    In screenShot example,
    http://doc.qt.io/qt-5/qtwidgets-desktop-screenshot-example.html

    void Screenshot::saveScreenshot()
    {
        const QString format = "png";
        QString initialPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
        if (initialPath.isEmpty())
            initialPath = QDir::currentPath();
        initialPath += tr("/untitled.") + format;
    
        QFileDialog fileDialog(this, tr("Save As"), initialPath);
        fileDialog.setAcceptMode(QFileDialog::AcceptSave);
        fileDialog.setFileMode(QFileDialog::AnyFile);
        fileDialog.setDirectory(initialPath);
        QStringList mimeTypes;
        foreach (const QByteArray &bf, QImageWriter::supportedMimeTypes())
            mimeTypes.append(QLatin1String(bf));
        fileDialog.setMimeTypeFilters(mimeTypes);
        fileDialog.selectMimeTypeFilter("image/" + format);
        fileDialog.setDefaultSuffix(format);
        if (fileDialog.exec() != QDialog::Accepted)
            return;
        const QString fileName = fileDialog.selectedFiles().first();
        if (!originalPixmap.save(fileName)) {
            QMessageBox::warning(this, tr("Save Error"), tr("The image could not be saved to \"%1\".")
                                 .arg(QDir::toNativeSeparators(fileName)));
        }
    }
    

    After clicking "Save Screenshot" pushbutton, "Save As"
    dialog pops up. In "Files of type" combobox, I see several supported file
    formats such as .bmp, .jpeg, .jpg, .png, ...
    When I select "PNG image(.png)" comboBox, the "File name" lineEdit shows the file name with the extension changed automatically to myfile.png. but when I select "JPEG image(.jpeg *.jpg *.jpe)", the "File name:" LineEdit shows with the extension changed to myfile.jpeg. I want it to change its extension automatically to myfile.jpg instead of myfile.jpeg.
    How can I do this?


  • Lifetime Qt Champion

    Hi,

    Shot in the dark: remove all other extensions from the jpeg entry.

    Hope it helps



  • @SGaist
    How could I remove them? Do I have to modify the source code of Qt?


  • Lifetime Qt Champion

    No, when you populate mimeTypes, check if jpeg appears in the entry and modify it before adding it to mimeTypes.



  • @SGaist
    I tried it. But it did not work. Instead of supportedMimeTypes(), I decided to use supportedImageFormats() though it demands some extra work. Thanks anyway.


  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.