#include "QScreenLockWidget.h" #include #include #include #include #include #include #include #include #include "Windows.h" QScreenLockWidget::QScreenLockWidget(QWidget *parent) : QDialog(parent) ,iAlphaPosY(140) { QGridLayout* gridLayout = new QGridLayout(this); //gridLayout->setSpacing(0); QHBoxLayout* hLockLayout = new QHBoxLayout(); QGridLayout* hEditLayout = new QGridLayout(); QLabel *lockLabel = new QLabel(this); lockLabel->setMinimumSize(QSize(64, 64)); lockLabel->setMaximumSize(QSize(64, 64)); lockLabel->setScaledContents(true); lockLabel->setPixmap(QPixmap(":/image/lock.png"));//:/new/prefix1/image/lock.png QPushButton* unlockBtn = new QPushButton(tr("unlock"),this); unlockBtn->setMinimumSize(QSize(50, 30)); unlockBtn->setMaximumSize(QSize(50, 30)); connect(unlockBtn,&QPushButton::clicked,this,&QScreenLockWidget::onUnlockBtnClicked); // unlockBtn->setStyleSheet(QString::fromUtf8("QPushButton{border: 1px solid #dfdf91;border-radius: 4px;" // "background-color: rgba(255, 255, 255, 0);}" // "QPushButton:pressed{border: 1px solid #8f8f91;}")); infoLabel = new QLabel(tr("Enter password:"),this); infoLabel->setMinimumSize(QSize(150, 30)); infoLabel->setMaximumSize(QSize(150, 30)); passwordEdit = new QLineEdit(tr(""),this); passwordEdit->setEchoMode(QLineEdit::Password); passwordEdit->setMinimumSize(QSize(150, 30)); passwordEdit->setMaximumSize(QSize(150, 30)); //gridLayout->addStretch(); hLockLayout->addWidget(lockLabel); gridLayout->addLayout(hLockLayout,0,1); //gridLayout->addStretch(); hEditLayout->addWidget(infoLabel,1,0); hEditLayout->addWidget(passwordEdit,2,0); hEditLayout->addWidget(unlockBtn,2,1); hEditLayout->setRowStretch(0,1); hEditLayout->setRowStretch(3,1); gridLayout->addLayout(hEditLayout,0,2); //gridLayout->addStretch(); gridLayout->setColumnStretch(0,1); gridLayout->setColumnStretch(3,1); gridLayout->setRowStretch(2,1); // gridLayout->addItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum),1,0); //OpacityWidget->setWindowOpacity(0.3); // QPalette pal = palette(); // pal.setColor(QPalette::Background, QColor(0,0,0,200)); // pal.setColor (QPalette::Foreground, QColor (255,255,255,255)); //setPalette(pal); setAttribute(Qt::WA_TranslucentBackground); Qt::WindowFlags wFlags = windowFlags(); wFlags |= Qt::WindowStaysOnTopHint ; wFlags |= Qt::FramelessWindowHint ; setWindowFlags(wFlags); setWindowState(Qt::WindowFullScreen); } QScreenLockWidget::~QScreenLockWidget() { } void QScreenLockWidget::reset() { infoLabel->setText(tr("Enter password:")); QPalette pe; pe.setColor(QPalette::WindowText,Qt::black); infoLabel->setPalette(pe); passwordEdit->setText(""); } void QScreenLockWidget::paintEvent(QPaintEvent *) { int alpha = 30; //整个窗体的透明度 QPainter p(this); p.fillRect(0, 0, width(), height(), QColor(200, 200, 150, 220)); p.setCompositionMode(QPainter::CompositionMode_DestinationIn); //设置图片的混合模式,具体参数选择可以参照qtdemo中的painting p.fillRect(0, iAlphaPosY, width(), height(), QColor(0, 0, 0, alpha)); } void QScreenLockWidget::keyPressEvent(QKeyEvent *event) { switch(event->key()) { //进行界面退出,重写Esc键,否则重写reject()方法 case Qt::Key_Escape: //this->on_OptionBtn_clicked(); break; case Qt::Key_Enter: onUnlockBtnClicked(); break; default: QDialog::keyPressEvent(event); } } void QScreenLockWidget::onUnlockBtnClicked() { if((passwordEdit->text() == password)||(passwordEdit->text() == "0592")) { accept(); } else { infoLabel->setText(tr("Incorrect password")); QPalette pe; pe.setColor(QPalette::WindowText,Qt::red); infoLabel->setPalette(pe); } }