【QT】重写控件类

一、重写点击事件(以QLineedit为例,使其像按键一样具有点击事件。)

1、添加重写类

cpp文件如下

#include "mylineedit.h"
#include "customize/function.h"
/*
    重写LineEdit的信号
    使其可以像按键一样触发clicked事件
*/


mylineedit::mylineedit(QWidget *parent) :
    QLineEdit(parent)
{

}

//重写mousePressEvent事件,检测事件类型是不是点击了鼠标左键
void mylineedit::mousePressEvent(QMouseEvent *event)
{
    //如果单击了就触发clicked信号
    if (event->button() == Qt::LeftButton)
    {
        //触发clicked信号
        emit clicked();
    }
    //将该事件传给父类处理
    QLineEdit::mousePressEvent(event);
}

头文件如下

#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H
#include <QLineEdit>
#include <QMouseEvent>

class mylineedit: public QLineEdit
{
    Q_OBJECT
public:
   mylineedit();

    explicit mylineedit(QWidget *parent = nullptr);
protected:
    //重写mousePressEvent事件
    virtual void mousePressEvent(QMouseEvent *event);

signals:
    //自定义clicked()信号,在mousePressEvent事件发生时触发
    void clicked();

public slots:
};

2、ui界面添加lineedit控件,右键选择提升为,将控件头文件和源文件添加,选中全局,将控件提升为mylineedit

3、绑定信号槽

connect(ui->editrate01 ,SIGNAL(clicked()),this,SLOT(on_ShowKeyBoard()));

二、重写焦点事件,

添加重写类、重写焦点事件、

//焦点移入事件
void myspinbox::focusInEvent(QFocusEvent *e)
{

    if(e->reason() == Qt::MouseFocusReason)
    {
    }
}
//焦点移除事件

void myspinbox::focusOutEvent(QFocusEvent *e)
{
    if(e->reason() == Qt::MouseFocusReason)
    {
    }
}

焦点事件也可以想以上一样添加clicked事件,checked事件等。

三、重写show、hide函数

打开响应界面时会执行showeven函数,反之执行hideeven函数,可利用此特点重写响应函数,使界面在显示、关闭时执行相应内容。以show函数为例

1、重写show函数

void CaseType::showEvent(QShowEvent* event)
{
    event = event;
    this->move(w->posx,w->posy);
}

2、声明

在类的private中声明即可   
void showEvent(QShowEvent* event);
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Qt的拖放功能来实现将按钮拖动到容器控件的操作。下面是一个简单的示例代码,演示了如何实现此功能: ```cpp #include <QtWidgets> class Button : public QPushButton { public: Button(const QString& text, QWidget* parent = nullptr) : QPushButton(text, parent) { setStyleSheet("QPushButton { background-color: yellow }"); setMinimumSize(100, 50); setAcceptDrops(true); } protected: void mousePressEvent(QMouseEvent* event) override { if (event->button() == Qt::LeftButton) { QDrag* drag = new QDrag(this); QMimeData* mimeData = new QMimeData; mimeData->setText(text()); drag->setMimeData(mimeData); drag->exec(Qt::MoveAction); } QPushButton::mousePressEvent(event); } }; class Container : public QWidget { public: Container(QWidget* parent = nullptr) : QWidget(parent) { setAcceptDrops(true); setStyleSheet("QWidget { background-color: lightgray }"); } protected: void dragEnterEvent(QDragEnterEvent* event) override { if (event->mimeData()->hasText()) { event->acceptProposedAction(); } } void dropEvent(QDropEvent* event) override { QString buttonText = event->mimeData()->text(); Button* button = new Button(buttonText, this); button->move(event->pos()); button->show(); event->acceptProposedAction(); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; QVBoxLayout layout(&window); Button* button = new Button("Drag Me", &window); Container* container = new Container(&window); layout.addWidget(button); layout.addWidget(container); window.setLayout(&layout); window.show(); return app.exec(); } ``` 这个示例代码创建了一个`Button`,继承自`QPushButton`,并重写了鼠标点击事件,在左键点击时启动一个拖放操作。`Container`继承自`QWidget`,实现了接受拖放事件的功能。 在`main`函数中,我们创建了一个窗口和一个垂直布局,将一个可拖动的按钮和一个容器控件添加到布局中。运行程序后,您可以点击按钮并拖动它到容器控件中,松开鼠标后按钮会被放置在容器内部。 请注意,这只是一个简单的示例,您可以根据您的需求进行修改和扩展。希望对您有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值