-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgetroot.cpp
109 lines (88 loc) · 2.88 KB
/
getroot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "getroot.h"
#include "ui_getroot.h"
#include <QKeyEvent>
#define T_PrivPtr( o ) (( StationaryLampSet *) o )
getroot::getroot(QWidget *parent) :
QWidget(parent),
ui(new Ui::getroot)
{
ui->setupUi(this);
//注册监视对象
ui->lineEdit->installEventFilter(this);
//设置窗口为对话框模式并设置窗口大小
setWindowFlags(Qt::Dialog); //窗体没有最大化最小化按钮
setFixedSize(308, 160); //设置窗体的大小
//设置编辑框为密码输入模式并获取焦点
ui->lineEdit->setEchoMode(QLineEdit::Password);ui->lineEdit->setFocus();
//设置密码提示默认颜色
ui->label_pass->setStyleSheet("color:rgb(100, 100, 100);");
}
getroot::~getroot()
{
delete ui;
}
//切换密码输入模式
void getroot::on_pushButton_show_clicked()
{
//检测输入模式是否为密码输入
if(ui->lineEdit->echoMode()==QLineEdit::Password){
ui->pushButton_show->setStyleSheet("QPushButton{border-image:url(:/ico/res/hide_pass.png);}");
ui->lineEdit->setEchoMode(QLineEdit::Normal);
}else{
ui->pushButton_show->setStyleSheet("QPushButton{border-image:url(:/ico/res/show_pass.png);}");
ui->lineEdit->setEchoMode(QLineEdit::Password);
}
}
//监视对象,处理回车消息
bool getroot::eventFilter(QObject *target, QEvent *event)
{
/*处理按键消息 */
if (target == ui->lineEdit && event->type() == QEvent::KeyPress){
/*强制类型转换 */
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == 16777220) {
getroot::on_pushButton_check_clicked();
return true;
}
}
/*处理按键消息 */
return QWidget::eventFilter(target, event);
}
void getroot::on_lineEdit_textEdited(const QString &arg1)
{
Q_UNUSED(arg1);
if(ui->lineEdit->text()==""){
ui->label_pass->show();
}else{
ui->label_pass->hide();
}
}
void getroot::on_pushButton_close_clicked()
{
getroot::close();
emit quit();//发射退出信号
}
void getroot::on_getroot_destroyed()
{
emit quit();//发射退出信号
}
void getroot::on_pushButton_check_clicked()
{
if(ui->lineEdit->text()==""){
ui->label_pass->setText("请输入密码!");
ui->label_pass->setStyleSheet("color:rgb(255, 0, 0);");
return;
}
//安全的强制类型转换
Ventoy2Disk *main = static_cast<Ventoy2Disk*>(parentWidget());
QString done=main->shell( "echo "+ui->lineEdit->text()+"|sudo -S fdisk -l");
if(done==""){
ui->lineEdit->setText("");
ui->label_pass->show();
ui->label_pass->setText("密码错误!");
ui->label_pass->setStyleSheet("color:rgb(255, 0, 0);");
}else{
emit showmainwindow(ui->lineEdit->text()); //发出显示主窗口信号
getroot::close();
}
}