Tutorial does not fill my Qmap[solved]



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


  • 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???



  • 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.


Log in to reply
 

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