A
A
Alexander2021-02-11 22:46:07
Qt
Alexander, 2021-02-11 22:46:07

How to call a function after clicking a button and return the result (in Qt Creator)?

I'm trying to write a simple password generator where the user specifies the number of characters and one password is generated. There is a "Generate" button on the form, and when it is pressed, the program reads the number of characters that will make up the password. In the void MainWindow::on_generate_clicked() function, I call the generate function. Here is the code:

void MainWindow::on_generate_clicked()
{

    int num = ui->charedit->value();

    GetRandomString(num);

    ui->password->setText(randomString);  ////  ВОТ ЗДЕСЬ ОШИБКА

    ui->password->show();  

}

QString GetRandomString(int num)
{
    const QString possible_characters {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"};

   QString randomString;

   for(int i=0; i<num; i++)
   {
       int index = rand() % possible_characters.length();
       QChar nextChar = possible_characters.at(index);
       randomString.append(nextChar);
   }

   return randomString;
}


And here in the void MainWindow::on_generate_clicked() function, after returning the result of randomString , the compiler says that randomString is an undeclared identifier. I understand that this is some kind of stupid mistake, but for the first time I come across Qt and the development of window applications, and before that I only wrote under the console.

Help fix the problem please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2021-02-11
@AlexB_49

Logically. Try like this:

QString randomString = GetRandomString(num);
ui->password->setText(randomString);

Or:
ui->password->setText(GetRandomString(num));
PS: There is nothing to check now.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question