π»C++ GUI-based Random Password Generator
Demo :
Click Video πππ
Code :
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QCheckBox>
#include <QSpinBox>
#include <QString>
#include <QClipboard>
#include <cstdlib>
#include <ctime>
class PasswordGenerator : public QWidget {
Q_OBJECT
public:
PasswordGenerator(QWidget *parent = nullptr) : QWidget(parent) {
setWindowTitle("Advanced Password Generator");
resize(400, 300);
QVBoxLayout *layout = new QVBoxLayout(this);
passwordField = new QLineEdit(this);
passwordField->setReadOnly(true);
layout->addWidget(passwordField);
lengthBox = new QSpinBox(this);
lengthBox->setRange(4, 32);
lengthBox->setValue(12);
layout->addWidget(lengthBox);
upperCase = new QCheckBox("Include Uppercase", this);
lowerCase = new QCheckBox("Include Lowercase", this);
numbers = new QCheckBox("Include Numbers", this);
symbols = new QCheckBox("Include Symbols", this);
layout->addWidget(upperCase);
layout->addWidget(lowerCase);
layout->addWidget(numbers);
layout->addWidget(symbols);
QPushButton *generateButton = new QPushButton("Generate Password", this);
QPushButton *copyButton = new QPushButton("Copy to Clipboard", this);
layout->addWidget(generateButton);
layout->addWidget(copyButton);
connect(generateButton, &QPushButton::clicked, this, &PasswordGenerator::generatePassword);
connect(copyButton, &QPushButton::clicked, this, &PasswordGenerator::copyToClipboard);
}
private slots:
void generatePassword() {
QString upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
QString lower = "abcdefghijklmnopqrstuvwxyz";
QString num = "0123456789";
QString sym = "!@#$%^&*()_-+=<>?/";
QString allChars = "";
if (upperCase->isChecked()) allChars += upper;
if (lowerCase->isChecked()) allChars += lower;
if (numbers->isChecked()) allChars += num;
if (symbols->isChecked()) allChars += sym;
if (allChars.isEmpty()) {
passwordField->setText("Select at least one option!");
return;
}
QString password = "";
int length = lengthBox->value();
srand(time(0));
for (int i = 0; i < length; i++) {
password += allChars[rand() % allChars.length()];
}
passwordField->setText(password);
}
void copyToClipboard() {
QApplication::clipboard()->setText(passwordField->text());
}
private:
QLineEdit *passwordField;
QSpinBox *lengthBox;
QCheckBox *upperCase;
QCheckBox *lowerCase;
QCheckBox *numbers;
QCheckBox *symbols;
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
PasswordGenerator window;
window.show();
return app.exec();
}
Comments
Post a Comment