main.cpp:
Código
#include <QtGui/QApplication> #include "qtpass.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); QtPass w; w.show(); return a.exec(); }
qtpass.h:
Código
#ifndef QTPASS_H #define QTPASS_H #include <QtGui> class QLineEdit; class QPushButton; class QLabel; class QSpinBox; class QString; class QtPass : public QWidget { Q_OBJECT public: QtPass(QWidget *parent = 0); private slots: void GenerarPass(); void About(); private: QString *pass; QLineEdit *LinePass; QPushButton *ButtonGen, *ButtonAbout, *ButtonSalir; QLabel *LabelPass; QSpinBox *SpinChar; QHBoxLayout *TopLayout; QHBoxLayout *BottomLayout; QVBoxLayout * MainLayout; }; #endif // QTPASS_H
qtpass.cpp:
Código
#include "qtpass.h" QtPass::QtPass(QWidget *parent) : QWidget(parent) { //Creamos los Widgets que vamos a necesitar this->LinePass = new QLineEdit; this->SpinChar = new QSpinBox; this->ButtonGen = new QPushButton("&Generar"); this->ButtonAbout = new QPushButton("&About"); this->ButtonSalir = new QPushButton("&Salir"); this->LabelPass = new QLabel("Password: "); //Configuramos algunos Widgets this->ButtonGen->setDefault(true); this->LinePass->setReadOnly(true); this->SpinChar->setMaximum(32); this->SpinChar->setMinimum(8); //Preparamos los Layouts que necesitaremos para organizar. this->TopLayout = new QHBoxLayout; this->BottomLayout = new QHBoxLayout; this->MainLayout = new QVBoxLayout; //Empezamos la organizacion de los Widgets this->TopLayout->addWidget(this->LabelPass); this->TopLayout->addWidget(this->LinePass); this->TopLayout->addWidget(this->SpinChar); this->BottomLayout->addWidget(this->ButtonGen); this->BottomLayout->addWidget(this->ButtonAbout); this->BottomLayout->addWidget(this->ButtonSalir); this->MainLayout->addLayout(this->TopLayout); this->MainLayout->addLayout(this->BottomLayout); //Añadimos titulo y el Layout principal a la ventana. this->setWindowTitle("QtPassGen by Lord R.N.A."); this->setLayout(this->MainLayout); this->setFixedSize(420,80); //Realizamos las conexiones que necesitaremos. connect(this->ButtonSalir,SIGNAL(clicked()),this,SLOT(close())); connect(this->ButtonGen,SIGNAL(clicked()),this,SLOT(GenerarPass())); connect(this->ButtonAbout,SIGNAL(clicked()),this,SLOT(About())); } //La funcion encargada de generar el password. void QtPass::GenerarPass() { this->pass = new QString; QTime *seed = new QTime; seed->start(); //Para inicializar el puntero seed con la hora actual. qsrand(seed->msec()); //La semilla para qrand(); for(int i=0;this->SpinChar->value()>i;i++) { this->pass->insert(i,QChar(qrand()%95 +33));//Inserta en la posicion i, el char devuelto por QChar(); if(this->pass->at(i)==QChar(96))i--; } this->LinePass->setText(*this->pass); delete this->pass; delete seed; } //El MessageBox para el About. void QtPass::About() { QMessageBox::about(this,"About","<b>QtPassGen v1.0 by Lord R.N.A.</b> <br><br>" "Aplicacion Generadora de Passwords <br>Qt 4.6" "<br><br><b>R.N.A. Labs.</b>"); }