Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Save a file

    General and Desktop
    3
    5
    2001
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      bear234 last edited by

      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

      1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

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

        (Z(:^

        1 Reply Last reply Reply Quote 0
        • raven-worx
          raven-worx Moderators last edited by

          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();
          }
          }
          @

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply Reply Quote 0
          • B
            bear234 last edited by

            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]

            1 Reply Last reply Reply Quote 0
            • B
              bear234 last edited by

              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]

              1 Reply Last reply Reply Quote 0
              • First post
                Last post