Save a file



  • i want to copy a qgraphicsview to a qpixmap and save it.
    for this, i code like this:
    QFileDialog fileDialog(this, "Enregistrer sous", "",
    "Images (*.jpg)");
    fileDialog.setAcceptMode(QFileDialog::AcceptSave);
    if(fileDialog.exec() == QDialog::Accepted)
    {
    QString path = fileDialog.selectedFiles()[0];
    QPixmap pixmap = QPixmap::grabWidget(ui->graphicsView);
    QFile file(path);
    file.open(QIODevice::ReadWrite);
    QByteArray ba;
    QBuffer buffer(&ba);
    buffer.open(QIODevice::WriteOnly);
    pixmap.save(&buffer, "JPG");
    file.write(ba);

    now I can save a file in jpg mode successfully.
    but there is a problem:
    when I save a file, I type "myfile" and click "save", OK, a file "myfile.jpg" will be created.
    but if i want to save again and I still type "myfile" and click "save", the old "myfile.jpg" will be replaced immediately. (normally a warning should popup because of "file has existed".)
    if i want to get a warning, i must name my file "myfile.jpg", which means that I must type ",jpg"

    as we know, for a software, if we type just the name without an extension, a warning should popup if necessary. this is what i want.

    can u help me???

    thx in advance


  • Moderators

    Please wrap your code in '@' tags, right now it's hard to read and analyse your code.


  • Moderators

    first of all please use the code tags when you post code. This makes reading it a lot easier.

    What system you are on? Which Qt version are you using?

    What happens when you try this:
    @
    QString fileName = QFileDialog::getSaveFileName( this, “Enregistrer sous”, “”, “Images (*.jpg)” );
    if( ! fileName.isEmpty() )
    {
    QPixmap pixmap = QPixmap::grabWidget(ui->graphicsView);
    QFile file(fileName);
    if( file.open(QIODevice::ReadWrite) )
    {
    QByteArray ba;
    QBuffer buffer(&ba);
    buffer.open(QIODevice::WriteOnly);
    pixmap.save(&buffer, “JPG”);
    file.write(ba);
    file.close();
    }
    }
    @



  • thx for ur advice
    [quote author="sierdzio" date="1389944435"]Please wrap your code in '@' tags, right now it's hard to read and analyse your code.[/quote]



  • thx.
    I got the same problem with ur code.

    my system: win7
    my Qt: 5.0.2

    so this is the problem of different system or different Qt?

    [quote author="raven-worx" date="1389944632"]first of all please use the code tags when you post code. This makes reading it a lot easier.

    What system you are on? Which Qt version are you using?

    What happens when you try this:
    @
    QString fileName = QFileDialog::getSaveFileName( this, “Enregistrer sous”, “”, “Images (*.jpg)” );
    if( ! fileName.isEmpty() )
    {
    QPixmap pixmap = QPixmap::grabWidget(ui->graphicsView);
    QFile file(fileName);
    if( file.open(QIODevice::ReadWrite) )
    {
    QByteArray ba;
    QBuffer buffer(&ba);
    buffer.open(QIODevice::WriteOnly);
    pixmap.save(&buffer, “JPG”);
    file.write(ba);
    file.close();
    }
    }
    @[/quote]


Log in to reply
 

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