Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Save a file
QtWS25 Last Chance

Save a file

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.3k Views
  • 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 Offline
    B Offline
    bear234
    wrote on last edited by
    #1

    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
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

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

      (Z(:^

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        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
        0
        • B Offline
          B Offline
          bear234
          wrote on last edited by
          #4

          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
          0
          • B Offline
            B Offline
            bear234
            wrote on last edited by
            #5

            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
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved