1、控件移动和放缩
与之前在pyqt
移动label
差不多,使用setGeometry
实现:
这是按钮初始时的坐标及宽高:
使用setGeometry
就可以在代码中重设其坐标及宽高,重设坐标则移动,重设宽高则放缩,如:
//向右移动 90px
ui->toolButton_1->setGeometry(280, 110, 37, 18);
//宽高变大2倍
ui->toolButton_1->setGeometry(280, 110, 37*2, 18*2);
或者单独使用resize()
修改尺寸,使用move
来移动控件。
2、lineedit只显示下边框:
其实和html5
中使用css
类似,只要设置每条边的宽度就行,如:
ui->lineEdit->setStyleSheet("border-top:2px solid;");
那么显示出来就是上边框2px
宽,其余边框不显示:
各条边分别是:
border:2px; //所有边框
border-top:2px; //上边框
border-bottom:2px; //下边框
border-left:2px; //左边框
border-right: 2px; //右边框
注: 但是在实际使用时有一个问题,经过多次实验,发现下左右边框并不能直接使用,直接写上去是不显示的。正确使用是下左右边框必须写在上边框后面
。
比如设置四条边的边宽,必须先设定上边框的宽度,如:
ui->lineEdit->setStyleSheet("border-top:5px solid;border-bottom:5px solid;border-left:5px solid;border-right: 5px solid;");
然后才正常显示:
先加了上边框,然后随便后面跟哪几个边框都可以,前提是一定要先写上边框。
那么我要只显示下边框就很简单了,只要把其他边框宽度设为0px就行了:
ui->lineEdit->setStyleSheet("border-top:0px solid;border-bottom:1px solid;border-left:0px solid;border-right: 0px solid;");
运行结果:
完美!
3、边框阴影
参考自:https://blog.csdn.net/wzz953200463/article/details/100533435
在mainwindow.h
中引入QGraphicsDropShadowEffect
:
#include <QGraphicsDropShadowEffect>
在mainwindow.cpp
中:
//阴影-------------------------------------------------------------------------
QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
shadow_effect->setOffset(0, 0);
//阴影颜色
shadow_effect->setColor(QColor(38, 78, 119, 127));
//阴影半径
shadow_effect->setBlurRadius(22);
ui->login->setGraphicsEffect(shadow_effect); //应用于login按钮
阴影前:
阴影后:
窗口的阴影:
想要显示整个窗口的阴影,直接setGraphicsEffect(shadow_effect);
是不行的,因为我可控制的区域就是窗口的区域,窗口把区域占满了,阴影就没有地方显示了,想要整个窗口显示阴影,可以在窗口上放置一个frame
,然后把其他组件都放到里面,然后让frame
显示阴影,但是要注意frame和窗口之间要留一点距离来显示阴影。
然后再把整个窗口透明就行了。
4、QLineEdit相关操作和一些使用
(1)设置输入的文本在QLineEdit中的边距:
QLineEdit.setTextMargins(left=,top=,right=,bottom=) //设置文本边距
QLineEdit.textMargins() //获取文本边距
使用时:
ui->lineEdit->setStyleSheet("border-top:0px solid;border-bottom:1px solid rgb(229,229,229);border-left:0px solid;border-right: 0px solid;");
ui->lineEdit->setTextMargins(16,10,0,0); //这里分别是文本的左边距为16和上边距为10
运行结果:
(2)添加QLineEdit内容为密码格式:
设置方法:
setEchoMode(EchoMode) //设置文本格式,此处的EchoMode为填充文本的格式,在下方
常用文本格式:
资料来自:https://blog.csdn.net/qq_33850438/article/details/52244848
Constant Value Description
QLineEdit::Normal 0 Display characters as they are entered. This is the default.
//正常显示形式,也就是边输入边显示
QLineEdit::NoEcho 1 Do not display anything. This may be appropriate for passwords
where even the length of the password should be kept secret.
//不会显示任何字符,包括长度
QLineEdit::Password 2 Display platform-dependent password mask characters instead of
the characters actually entered.
//根据平台使用模糊字符代替你实际输入的字符
QLineEdit::PasswordEchoOnEdit 3 Display characters as they are entered while editing
otherwise display characters as with Password.
//当你处于输入状态的时候,是正常显示字符。 输入完毕之后
//使用Password形式隐藏字符
使用时:
ui->lineEdit->setStyleSheet("border-top:0px solid;border-bottom:1px solid rgb(229,229,229);border-left:0px solid;border-right: 0px solid;");
ui->lineEdit->setTextMargins(16,10,0,0);
ui->lineEdit->setEchoMode(QLineEdit::Password); //设置为密码格式
运行结果:
(3)选中QLineEdit输入文本时,下边框和图标改变:
首先要确定QLineEdit
选中时的事件:
//选中时的事件
QEvent::FocusIn
//未选中时的事件
QEvent::FocusOut
为了实现这一功能,在事件过滤器来实现:
//事件过滤器
bool MainWindow::eventFilter(QObject *target, QEvent *e)
{
if(target==ui->lineEdit) //密码输入框
{
if(e->type()==QEvent::FocusIn) //选中
{
ui->lineEdit->setStyleSheet("border-top:0px solid;border-bottom:1px solid rgb(18,183,245);border-left:0px solid;border-right: 0px solid;");
ui->label_2->setStyleSheet("background:transparent;image:url(:/new/prefix1/source/suo2.png);");
}
else if (e->type()==QEvent::FocusOut) //未选中
{
ui->lineEdit->setStyleSheet("border-top:0px solid;border-bottom:1px solid rgb(229,229,229);border-left:0px solid;border-right: 0px solid;");
ui->label_2->setStyleSheet("background:transparent;image:url(:/new/prefix1/source/suo1.png);");
}
}
if(target==ui->lineEdit_2) //用户名输入框
{
if(e->type()==QEvent::FocusIn) //选中
{
ui->lineEdit_2->setStyleSheet("border-top:0px solid;border-bottom:1px solid rgb(18,183,245);border-left:0px solid;border-right: 0px solid;");
ui->label->setStyleSheet("background:transparent;image:url(:/new/prefix1/source/QQblue.png);");
}
else if (e->type()==QEvent::FocusOut) //未选中
{
ui->lineEdit_2->setStyleSheet("border-top:0px solid;border-bottom:1px solid rgb(229,229,229);border-left:0px solid;border-right: 0px solid;");
ui->label->setStyleSheet("background:transparent;image:url(:/new/prefix1/source/QQgrey.png);");
}
}
return QMainWindow::eventFilter(target, e);
}
完美!