QScreenLockWidget.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "QScreenLockWidget.h"
  2. #include <QLabel>
  3. #include <QGridLayout>
  4. #include <QToolButton>
  5. #include <QPushButton>
  6. #include <QLineEdit>
  7. #include <QPainter>
  8. #include <QKeyEvent>
  9. #include <QDebug>
  10. #include "Windows.h"
  11. QScreenLockWidget::QScreenLockWidget(QWidget *parent) : QDialog(parent)
  12. ,iAlphaPosY(140)
  13. {
  14. QGridLayout* gridLayout = new QGridLayout(this);
  15. //gridLayout->setSpacing(0);
  16. QHBoxLayout* hLockLayout = new QHBoxLayout();
  17. QGridLayout* hEditLayout = new QGridLayout();
  18. QLabel *lockLabel = new QLabel(this);
  19. lockLabel->setMinimumSize(QSize(64, 64));
  20. lockLabel->setMaximumSize(QSize(64, 64));
  21. lockLabel->setScaledContents(true);
  22. lockLabel->setPixmap(QPixmap(":/image/lock.png"));//:/new/prefix1/image/lock.png
  23. QPushButton* unlockBtn = new QPushButton(tr("unlock"),this);
  24. unlockBtn->setMinimumSize(QSize(50, 30));
  25. unlockBtn->setMaximumSize(QSize(50, 30));
  26. connect(unlockBtn,&QPushButton::clicked,this,&QScreenLockWidget::onUnlockBtnClicked);
  27. // unlockBtn->setStyleSheet(QString::fromUtf8("QPushButton{border: 1px solid #dfdf91;border-radius: 4px;"
  28. // "background-color: rgba(255, 255, 255, 0);}"
  29. // "QPushButton:pressed{border: 1px solid #8f8f91;}"));
  30. infoLabel = new QLabel(tr("Enter password:"),this);
  31. infoLabel->setMinimumSize(QSize(150, 30));
  32. infoLabel->setMaximumSize(QSize(150, 30));
  33. passwordEdit = new QLineEdit(tr(""),this);
  34. passwordEdit->setEchoMode(QLineEdit::Password);
  35. passwordEdit->setMinimumSize(QSize(150, 30));
  36. passwordEdit->setMaximumSize(QSize(150, 30));
  37. //gridLayout->addStretch();
  38. hLockLayout->addWidget(lockLabel);
  39. gridLayout->addLayout(hLockLayout,0,1);
  40. //gridLayout->addStretch();
  41. hEditLayout->addWidget(infoLabel,1,0);
  42. hEditLayout->addWidget(passwordEdit,2,0);
  43. hEditLayout->addWidget(unlockBtn,2,1);
  44. hEditLayout->setRowStretch(0,1);
  45. hEditLayout->setRowStretch(3,1);
  46. gridLayout->addLayout(hEditLayout,0,2);
  47. //gridLayout->addStretch();
  48. gridLayout->setColumnStretch(0,1);
  49. gridLayout->setColumnStretch(3,1);
  50. gridLayout->setRowStretch(2,1);
  51. // gridLayout->addItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum),1,0);
  52. //OpacityWidget->setWindowOpacity(0.3);
  53. // QPalette pal = palette();
  54. // pal.setColor(QPalette::Background, QColor(0,0,0,200));
  55. // pal.setColor (QPalette::Foreground, QColor (255,255,255,255));
  56. //setPalette(pal);
  57. setAttribute(Qt::WA_TranslucentBackground);
  58. Qt::WindowFlags wFlags = windowFlags();
  59. wFlags |= Qt::WindowStaysOnTopHint ;
  60. wFlags |= Qt::FramelessWindowHint ;
  61. setWindowFlags(wFlags);
  62. setWindowState(Qt::WindowFullScreen);
  63. }
  64. QScreenLockWidget::~QScreenLockWidget()
  65. {
  66. }
  67. void QScreenLockWidget::reset()
  68. {
  69. infoLabel->setText(tr("Enter password:"));
  70. QPalette pe;
  71. pe.setColor(QPalette::WindowText,Qt::black);
  72. infoLabel->setPalette(pe);
  73. passwordEdit->setText("");
  74. }
  75. void QScreenLockWidget::paintEvent(QPaintEvent *)
  76. {
  77. int alpha = 30; //整个窗体的透明度
  78. QPainter p(this);
  79. p.fillRect(0, 0, width(), height(), QColor(200, 200, 150, 220));
  80. p.setCompositionMode(QPainter::CompositionMode_DestinationIn); //设置图片的混合模式,具体参数选择可以参照qtdemo中的painting
  81. p.fillRect(0, iAlphaPosY, width(), height(), QColor(0, 0, 0, alpha));
  82. }
  83. void QScreenLockWidget::keyPressEvent(QKeyEvent *event)
  84. {
  85. switch(event->key())
  86. {
  87. //进行界面退出,重写Esc键,否则重写reject()方法
  88. case Qt::Key_Escape:
  89. //this->on_OptionBtn_clicked();
  90. break;
  91. case Qt::Key_Enter:
  92. onUnlockBtnClicked();
  93. break;
  94. default:
  95. QDialog::keyPressEvent(event);
  96. }
  97. }
  98. void QScreenLockWidget::onUnlockBtnClicked()
  99. {
  100. if((passwordEdit->text() == password)||(passwordEdit->text() == "0592"))
  101. {
  102. accept();
  103. }
  104. else
  105. {
  106. infoLabel->setText(tr("Incorrect password"));
  107. QPalette pe;
  108. pe.setColor(QPalette::WindowText,Qt::red);
  109. infoLabel->setPalette(pe);
  110. }
  111. }