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. I need some help with a QStringList, please??
Forum Updated to NodeBB v4.3 + New Features

I need some help with a QStringList, please??

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.5k 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.
  • Z Offline
    Z Offline
    zerocool
    wrote on last edited by
    #1

    Here is my code right now and I was wondering how I could get the count of however many lines in that list... by the way I could do it with regular C++ but, I want to use Qt. As in QStringList.count() or something but.. I get a segment fault when I run my program after it encrypts the data. I know it has to be the count because, I remove the listUserInfo.count(), replace it with like 2 or something.. my program works perfectly. I don't understand why that would cause said, "Segment Fault". Any help would be appreciated thanks guys.

    [code] bool decryptUserFile(QString sFile)
    {
    // Open the usernames.dat file
    QFile inputFile(sFile);
    bool result = inputFile.open(QIODevice::ReadOnly);

        if (!result)
            return false;
    
        QTextStream in(&inputFile);
        QString fileContents = in.readAll();
    
        if (fileContents.endsWith(encMessage))
        {
            user_file_data = fileContents.replace(encMessage, "\0");
        }
    
        QString decData = calculateXor(user_file_data.toAscii(), theKey.toAscii());
    
        // rewrite the decrypted data to the same file
        QStringList listUserInfo = decData.split('\n');
    
        for (int i = 0; i < listUserInfo.count(); i++)
            sUserInfo[i] = listUserInfo[i];
    
        // close the inputFile
        inputFile.close();
    
        return true;
    }[/code]
    
    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      @
      for (int i = 0; i < listUserInfo.count(); i++)
      sUserInfo[i] = listUserInfo[i];
      @

      Are you sure that: sUserInfo.size() >= listUserInfo.size() ??


      BTY: I can not understand what you are trying to do. If you are really doing "encryption or decryption.", you should not using QString/QTextStream/..., instead, you should always use QByteArray or char* .

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zerocool
        wrote on last edited by
        #3

        I was wondering if anyone could tell me why this don't work and how to fix it please...

        [code] else if (!username.isEmpty() && !passcode.isEmpty())
        {
        for (int i = 0, j = 0; i < sUsername.count() && j < sPasscode.count(); i++, j++)
        {
        if ((username == sUsername[i]) && (passcode == sPasscode[j]))
        {
        // You are successfully logged on to the program
        ui->nameEdit->clear();
        ui->codeEdit->clear();
        QMessageBox::information(this, tr("Login Screen"), tr("You have successfully logged on to this program!"));
        }
        else
        {
        QMessageBox::information(this, tr("Login Screen"), tr("Sorry but that was an invalid username or passcode.\nTry again..."));
        }
        }
        }[/code]

        Thanks in advance...

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dbzhang800
          wrote on last edited by
          #4

          You can easily figure out why your above code doesn't work when you debug your application.

          1. Set a breakpoint
          2. Step by Step

          BTW: why use two var: i & j ?

          1 Reply Last reply
          0
          • W Offline
            W Offline
            Wilk
            wrote on last edited by
            #5

            Hello
            I have several questions:

            Why didn't you create separate post for each of your problem?

            What should the last code block do? And what's the problem with it?

            What did you do to find out what's wrong?

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              zerocool
              wrote on last edited by
              #6

              The reason I didn't post in a new topic is because it's the same project I'm working on...
              The last code block should check the <QStringList>sUsername[i] == username && <QStringList>sPasscode[j] == passcode. And it doesn't do it.
              I just no what wrong and I used the debugger... (I know because, the whole thing worked perfectly before I added that in there.)

              I want to check them vars and if any username and password combination was entered then say something like, "You have successfully logged into this program."

              1 Reply Last reply
              0
              • W Offline
                W Offline
                Wilk
                wrote on last edited by
                #7

                Well, if i've understood you right, you want to check all combinations of login and password, i.e. n*m variants, where n is number of logins and m is number of passwords.
                If so, you're doing it in a wrong way, you should:
                @
                else if (!username.isEmpty() && !passcode.isEmpty())
                {
                for (int i = 0; i < sUsername.count(); j++)
                for (j = 0; j < sPasscode.count(); j++)
                {
                if ((username == sUsername[i]) && (passcode == sPasscode[j]))
                {
                // You are successfully logged on to the program
                ui->nameEdit->clear();
                ui->codeEdit->clear();
                QMessageBox::information(this, tr("Login Screen"), tr("You have successfully logged on to this program!"));
                }
                else
                {
                QMessageBox::information(this, tr("Login Screen"), tr("Sorry but that was an invalid username or passcode.\nTry again..."));
                }
                }
                }
                @
                But I don't understand the meaning of this procedure: if you want to authenticate user, then you should check that he entered correct password for some correct login. Something like:
                @
                else if (!username.isEmpty() && !passcode.isEmpty()) {
                if (users.count(username)) {
                if (passcodes[username] == passcode) {
                QMessageBox::information(this, tr("Login Screen"), tr("You have successfully logged on to this program!"));
                }
                else {
                QMessageBox::information(this, tr("Login Screen"), tr("Sorry but that was an invalid passcode.\nTry again..."));
                }
                }
                else {
                QMessageBox::information(this, tr("Login Screen"), tr("Sorry but that was an invalid username.\nTry again..."));
                }
                }
                @

                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