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. Tutorial does not fill my Qmap[solved]
Qt 6.11 is out! See what's new in the release blog

Tutorial does not fill my Qmap[solved]

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.1k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Marco_105
    wrote on last edited by
    #1

    Hello,
    would like to know why the Address Book loading return all time an empty object instead those i fill it ?

    @void AddressBook::loadFromFile()
    {
    QString fileName = QFileDialog::getOpenFileName(this,
    tr("Open Address Book"),"",
    tr("Address Book (.abk);;All Files()"));
    //si rien n'est choisi
    if(fileName.isEmpty())
    {
    return;
    }else{

        //chemin de fichier
        QFile file(fileName);
    
        //test ouverture en lecture seule
        if(!file.open(QIODevice::WriteOnly))
        {
            QMessageBox::information(this,
                                     tr("Impossible d'ouvrir ce fichier"),
                                     file.errorString());
        }
    
        //variable de flux (version fixé)
        QDataStream in(&file);
        in.setVersion(QDataStream::Qt_4_5);
        contacts.empty();
        in >> contacts;
    
        if(contacts.isEmpty())
        {
            QMessageBox::information(this,tr("No contacts in file"),
                                     tr("File is empty"));
    
        }else{
            QMap<QString,QString>::iterator iter = contacts.begin();
            nameLine->setText(iter.key());
            addressText->setText(iter.value());
        }
    
        updateInterface(NavigationMode);
    }@
    
    1 Reply Last reply
    0
    • P Offline
      P Offline
      prady_80
      wrote on last edited by
      #2

      Do you want to read from the file or write to the file???
      Check if(!file.open(QIODevice::WriteOnly)) should it be set to readonly???

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Marco_105
        wrote on last edited by
        #3

        Yes, you find it, this is maybe a mistake wrote in the AddressBook tutorial when i copy/paste the previous code from the function 'saveToFile'.

        Since i solve it by change the above line 16 in:

         @ if(!file.open(QIODevice::ReadWrite))@
        

        and that running with the expected behaviour now, ussssssh.

        I was so upset about it.

        Thanks for watching.

        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