Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
Logically. Try like this:
QString randomString = GetRandomString(num);
ui->password->setText(randomString);
ui->password->setText(GetRandomString(num));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question