前有一个头像渐变边框的需求,当时是全部用的切图,但是不同的地方可能头像大小不一样,切图适配性太差,正好这几日学了下css渐变的各种妙用,想到之前的头像边框其实可以用渐变来实现。
当时用到的头像切图:
这个还是比较简单,不过定位位置和圆角大小需要自己每次根据实际的尺寸来计算,而且感觉还额外多使用了一个 div 标签,不是很优雅。
但是这个 border-image 和 border-radius 不能同时使用,设置了边框背景,圆角就自动失效了,所以最终做出来的是一个直接的边框背景。
这个样子肯定是不阔以的了,然后 发了了 clip-path 这个裁切属性,搞上去外圆角倒是又了,那里面图片的内圆角呢?图片还是个直角的,和需求有点不符。
用 border-image 方式虽然少用了一个标签,但是没法同时实现内外圆角。最后只能在第一种方式上来进行优化,还是利用背景渐变的方式来实现,不过位置不用定位,直接用 padding 留出需要的边框宽度就行了,不需要自己每次要去计算定位位置,不过圆角还是要去根据大小计算下的。
这个IP地址输入框控件,估计写烂了,网上随便一搜索,保证一大堆,估计也是因为这个控件太容易了,非常适合新手练手,一般的思路都是用4个qlineedit控件拼起来,然后每个输入框设置正则表达式过滤只能输入3位数字,然后安装事件过滤器识别回车自动跳到下一个输入框。关于如何设置正则表达式过滤,这个可以搜索查到,本人也不大懂这个规则,貌似还有专门的书籍专门介绍正则表达式,可能这块非常强大。
开源地址:https://gitee.com/feiyangqingyun/QWidgetDemo https://github.com/feiyangqingyun/QWidgetDemo
#ifndef IPADDRESS_H
#define IPADDRESS_H
/**
* IP地址输入框控件 作者:feiyangqingyun(QQ:517216493) 2017-8-11
* 1:可设置IP地址,自动填入框
* 2:可清空IP地址
* 3:支持按下小圆点自动切换
* 4:支持退格键自动切换
* 5:支持IP地址过滤
* 6:可设置背景色/边框颜色/边框圆角角度
*/
#include <QWidget>
class QLabel;
class QLineEdit;
#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif
class QDESIGNER_WIDGET_EXPORT IPAddress : public QWidget
#else
class IPAddress : public QWidget
#endif
{
Q_OBJECT
Q_PROPERTY(QString ip READ getIP WRITE setIP)
public:
explicit IPAddress(QWidget *parent = 0);
protected:
bool eventFilter(QObject *watched, QEvent *event);
private:
QLabel *labDot1; //第一个小圆点
QLabel *labDot2; //第二个小圆点
QLabel *labDot3; //第三个小圆点
QLineEdit *txtIP1; //IP地址网段输入框1
QLineEdit *txtIP2; //IP地址网段输入框2
QLineEdit *txtIP3; //IP地址网段输入框3
QLineEdit *txtIP4; //IP地址网段输入框4
QString ip; //IP地址
QString bgColor; //背景颜色
QString borderColor;//边框颜色
int borderRadius; //边框圆角角度
private slots:
void textChanged(const QString &text);
public:
//获取IP地址
QString getIP() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
public Q_SLOTS:
//设置IP地址
void setIP(const QString &ip);
//清空
void clear();
//设置背景颜色
void setBgColor(const QString &bgColor);
//设置边框颜色
void setBorderColor(const QString &borderColor);
//设置边框圆角角度
void setBorderRadius(int borderRadius);
};
#endif // IPADDRESS_H
【领QT开发教程学习资料,点击下方链接莬费领取↓↓,先码住不迷路~】
点击这里:Qt资料领取(视频教程+文档+代码+项目实战)
#pragma execution_character_set("utf-8")
#include "ipaddress.h"
#include "qlabel.h"
#include "qlineedit.h"
#include "qboxlayout.h"
#include "qregexp.h"
#include "qvalidator.h"
#include "qevent.h"
#include "qdebug.h"
IPAddress::IPAddress(QWidget *parent) : QWidget(parent)
{
bgColor = "#FFFFFF";
borderColor = "#A6B5B8";
borderRadius = 3;
//用于显示小圆点的标签,居中对齐
labDot1 = new QLabel;
labDot1->setAlignment(Qt::AlignCenter);
labDot1->setText(".");
labDot2 = new QLabel;
labDot2->setAlignment(Qt::AlignCenter);
labDot2->setText(".");
labDot3 = new QLabel;
labDot3->setAlignment(Qt::AlignCenter);
labDot3->setText(".");
//用于输入IP地址的文本框,居中对齐
txtIP1 = new QLineEdit;
txtIP1->setObjectName("txtIP1");
txtIP1->setAlignment(Qt::AlignCenter);
txtIP1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(txtIP1, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
txtIP2 = new QLineEdit;
txtIP2->setObjectName("txtIP2");
txtIP2->setAlignment(Qt::AlignCenter);
txtIP2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(txtIP2, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
txtIP3 = new QLineEdit;
txtIP3->setObjectName("txtIP3");
txtIP3->setAlignment(Qt::AlignCenter);
txtIP3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(txtIP3, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
txtIP4 = new QLineEdit;
txtIP4->setObjectName("txtIP4");
txtIP4->setAlignment(Qt::AlignCenter);
txtIP4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(txtIP4, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
//设置IP地址校验过滤
QRegExp regExp("(2[0-5]{2}|2[0-4][0-9]|1?[0-9]{1,2})");
QRegExpValidator *validator = new QRegExpValidator(regExp, this);
txtIP1->setValidator(validator);
txtIP2->setValidator(validator);
txtIP3->setValidator(validator);
txtIP4->setValidator(validator);
//绑定事件过滤器,识别键盘按下
txtIP1->installEventFilter(this);
txtIP2->installEventFilter(this);
txtIP3->installEventFilter(this);
txtIP4->installEventFilter(this);
QFrame *frame = new QFrame;
frame->setObjectName("frameIP");
QStringList qss;
qss.append(QString("QFrame#frameIP{border:1px solid %1;border-radius:%2px;}").arg(borderColor).arg(borderRadius));
qss.append(QString("QLabel{min-width:15px;background-color:%1;}").arg(bgColor));
qss.append(QString("QLineEdit{background-color:%1;border:none;}").arg(bgColor));
qss.append(QString("QLineEdit#txtIP1{border-top-left-radius:%1px;border-bottom-left-radius:%1px;}").arg(borderRadius));
qss.append(QString("QLineEdit#txtIP4{border-top-right-radius:%1px;border-bottom-right-radius:%1px;}").arg(borderRadius));
frame->setStyleSheet(qss.join(""));
QVBoxLayout *verticalLayout = new QVBoxLayout(this);
verticalLayout->setMargin(0);
verticalLayout->setSpacing(0);
verticalLayout->addWidget(frame);
//将控件按照横向布局排列
QHBoxLayout *layout = new QHBoxLayout(frame);
layout->setMargin(0);
layout->setSpacing(0);
layout->addWidget(txtIP1);
layout->addWidget(labDot1);
layout->addWidget(txtIP2);
layout->addWidget(labDot2);
layout->addWidget(txtIP3);
layout->addWidget(labDot3);
layout->addWidget(txtIP4);
}
bool IPAddress::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QLineEdit *txt = (QLineEdit *)watched;
if (txt == txtIP1 || txt == txtIP2 || txt == txtIP3 || txt == txtIP4) {
QKeyEvent *key = (QKeyEvent *)event;
//如果当前按下了小数点则移动焦点到下一个输入框
if (key->text() == ".") {
this->focusNextChild();
}
//如果按下了退格键并且当前文本框已经没有了内容则焦点往前移
if (key->key() == Qt::Key_Backspace) {
if (txt->text().length() <= 1) {
this->focusNextPrevChild(false);
}
}
}
}
return QWidget::eventFilter(watched, event);
}
void IPAddress::textChanged(const QString &text)
{
int len = text.length();
int value = text.toInt();
//判断当前是否输入完成一个网段,是的话则自动移动到下一个输入框
if (len == 3) {
if (value >= 100 && value <= 255) {
this->focusNextChild();
}
}
//拼接成完整IP地址
ip = QString("%1.%2.%3.%4").arg(txtIP1->text()).arg(txtIP2->text()).arg(txtIP3->text()).arg(txtIP4->text());
}
QString IPAddress::getIP() const
{
return this->ip;
}
QSize IPAddress::sizeHint() const
{
return QSize(250, 20);
}
QSize IPAddress::minimumSizeHint() const
{
return QSize(30, 10);
}
void IPAddress::setIP(const QString &ip)
{
//先检测IP地址是否合法
QRegExp regExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
if (!regExp.exactMatch(ip)) {
return;
}
if (this->ip != ip) {
this->ip = ip;
//将IP地址填入各个网段
QStringList list = ip.split(".");
txtIP1->setText(list.at(0));
txtIP2->setText(list.at(1));
txtIP3->setText(list.at(2));
txtIP4->setText(list.at(3));
}
}
void IPAddress::clear()
{
txtIP1->clear();
txtIP2->clear();
txtIP3->clear();
txtIP4->clear();
txtIP1->setFocus();
}
void IPAddress::setBgColor(const QString &bgColor)
{
if (this->bgColor != bgColor) {
this->bgColor = bgColor;
}
}
void IPAddress::setBorderColor(const QString &borderColor)
{
if (this->borderColor != borderColor) {
this->borderColor = borderColor;
}
}
void IPAddress::setBorderRadius(int borderRadius)
{
if (this->borderRadius != borderRadius) {
this->borderRadius = borderRadius;
}
}
原文链接:https://www.cnblogs.com/feiyangqingyun/p/11665022.html
【领QT开发教程学习资料,点击下方链接莬费领取↓↓,先码住不迷路~】
点击这里:「链接」
最近在做直播后台,涉及到对用户的一些操作,比如关注/取关/禁言/踢出直播间。多个地方都要用,需要封装一个弹框组件
根据 点击事件event参数,来获取位置,根据需要来实际计算位置
获取元素top/left,其实直接用getBoundingClientRect 里的bottom / right 即可
export const getCurrentDialogXY = (event: Event) => {
const target = event.target as HTMLElement
const { bottom, right } = target.getBoundingClientRect()
return { bottom, right }
}
调用
```js
const avatarHandler = (event: Event) => {
const { right, bottom } = getCurrentDialogXY(event)
currentX.value = right
currentY.value = bottom
}
传入子组件,在子组件内接收到left/top 重新定位即可
<user-info-dialog
:left="currentX"
:top="currentY"
/>
<div ref="userInfoRef" class="dialog-wrap" :style="{ top: top + 'px', left: left + 'px' }"></div>
通过监听弹框的ref(弹框有显示隐藏逻辑,所以监听弹框的ref即可,然后动态设置位置),
watchEffect(() => {
const userInfoRefValue = userInfoRef.value
if (userInfoRefValue) {
const offsetHeight = userInfoRefValue.offsetHeight // 获取弹框的高度
const offsetWidth = userInfoRefValue.offsetWidth // 获取元素的宽度
// 不管弹框显示是左侧还是右侧,根据需求 都需要 减去 弹框的高度 - 用户头像高度的一半 进行定位
propTop.value = props.top - offsetHeight - userAvatarWidth.value / 2
// 如果是弹框显示在左侧,需要减去弹框的宽度 - 头像的宽度
if (props.roleEnumType === 1) {
propLeft.value = props.left - offsetWidth - userAvatarWidth.value
}
}
})
你会发现,其实我们的整个元素,是直接在根节点上,根常规里的,每个弹框,必须要绑定在循环里,才能做一一对应的关系,怎么不太一样呢? 这里是用到的
vue3中的Teleport:将其插槽内容渲染到 DOM 中的另一个位置 这对于我们层级比较多,做全局定位,脱离当前元素,非常好用!!!
*请认真填写需求信息,我们会在24小时内与您取得联系。