qt自定义 QLineEdit

简单封了一个:

可根据情况 添加按钮图片,修改按钮大小

方法1:

#include<QLineEdit>
#include<QPushButton>
#include<QHBoxLayout>
#include<QEvent>
#include<QDebug>
#include<QMouseEvent>
class CustomLine : public QLineEdit
{
    Q_OBJECT
public:
    CustomLine(QWidget *parent= nullptr):QLineEdit(parent)
    {
        HB = new QHBoxLayout;
        Button = new QPushButton;
        Button->setFixedWidth(30);
        HB->addWidget(Button,0,Qt::AlignRight);
        HB->setContentsMargins(0,1,1,1);
        this->setLayout(HB);
        Button->installEventFilter(this);
    }
    // 槽函数绑定
    void  BindFunction(std::function<void(void)>func)
    {
        connect(Button,&QPushButton::clicked,func);
    }
private:
	//解决 鼠标放置在按钮上 图标显示
    void mouseMoveEvent(QMouseEvent * event)
    {
        if(event->pos().x() < this->width()- Button->width()-5)
        {
            this->setCursor(QCursor(Qt::IBeamCursor));
        }
        else
        {
             this->setCursor(QCursor(Qt::ArrowCursor));
        }
    }

    QPushButton *Button;
    QHBoxLayout *HB;
};

方法2:

#include<QLineEdit>
#include<QPushButton>
#include<QHBoxLayout>
#include<QEvent>
#include<QDebug>
#include<QMouseEvent>
class CustomLine : public QLineEdit
{
    Q_OBJECT
public:
    CustomLine(QWidget *parent= nullptr):QLineEdit(parent)
    {
        HB = new QHBoxLayout;
        Button = new QPushButton;
        Button->setFixedWidth(30);
        HB->addWidget(Button,0,Qt::AlignRight);
        HB->setContentsMargins(0,1,1,1);
        this->setLayout(HB);

        Button->setFocusPolicy(Qt::NoFocus);
        //解决 鼠标放置在按钮上 图标显示 
        Button->setCursor(QCursor(Qt::ArrowCursor));
    }
    // 槽函数绑定
    void  BindFunction(std::function<void(void)>func)
    {
        connect(Button,&QPushButton::clicked,func);
    }
private:
    QPushButton *Button;
    QHBoxLayout *HB;
};

测试:

lineEdit->BindFunction([]{
        qDebug()<<"button clieck....";
    });

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要自定义QLineEdit的右键菜单,可以通过重写QLineEdit的contextMenuEvent()函数来实现。下面是一个示例代码: ```cpp CustomLineEdit::CustomLineEdit(QWidget *parent) : QLineEdit(parent) { setContextMenuPolicy(Qt::CustomContextMenu); connect(this, &CustomLineEdit::customContextMenuRequested, this, &CustomLineEdit::showCustomContextMenu); } void CustomLineEdit::showCustomContextMenu(const QPoint &pos) { QMenu menu(this); QAction *action = menu.addAction("Custom Action"); connect(action, &QAction::triggered, this, &CustomLineEdit::onCustomActionTriggered); menu.exec(mapToGlobal(pos)); } void CustomLineEdit::onCustomActionTriggered() { // 处理自定义操作 } ``` 在上面的代码中,我们首先通过setContextMenuPolicy()函数将QLineEdit的上下文菜单策略设置为Qt::CustomContextMenu,然后在构造函数中连接customContextMenuRequested信号到我们自己的槽函数showCustomContextMenu()。 在showCustomContextMenu()函数中,我们创建一个QMenu对象,并添加我们自己的自定义操作。这里我们只添加了一个名为"Custom Action"的操作,当用户点击这个操作时,会触发onCustomActionTriggered()槽函数。 最后,我们在showCustomContextMenu()函数中调用menu.exec()函数来显示菜单。由于我们需要将菜单显示在鼠标点击的位置,所以需要使用mapToGlobal()函数将QPoint对象转换为全局坐标系。 在onCustomActionTriggered()函数中,我们可以处理我们自己的自定义操作。这里我们只是简单地打印一条消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值