QObject各成员函数功能详细说明(文字+用例+代码+效果图)

文章目录

1.测试工程配置

创建名为QtGuiAppTest的qwidget工程,带ui文件
在这里插入图片描述

2.成员函数

2.1 bool blockSignals(bool block)

(1)功能说明
如果一个继承自qobject的对象的阻塞信号设置为true,该对象发出的任何信号都会被阻塞,即发出的信号不会触发任何槽函数执行。返回值为阻塞信号的前一个设置的值。
注意:即使设置了阻塞信号, destroyed()信号仍然会被发出,并触发连接的槽。
(2)效果展示
在这里插入图片描述

(3)调用程序
QtGuiAppTest.ui
在这里插入图片描述

QtGuiAppTest.h

#pragma once
#include <QtWidgets/QWidget>
#include "ui_QtGuiAppTest.h"
class QtGuiAppTest : public QWidget
{
   
    Q_OBJECT
public:
    QtGuiAppTest(QWidget *parent = Q_NULLPTR);
public slots:
    //显示count计数
    void SlotTest();
    //根据下拉列表选项,设置blocksignals
    void SlotComboxChg(int id);
private:
    Ui::QtGuiAppTestClass ui;
    //计数
    int count;
};

QtGuiAppTest.cpp

#include "QtGuiAppTest.h"
QtGuiAppTest::QtGuiAppTest(QWidget *parent)
    : QWidget(parent)
{
   
    ui.setupUi(this);
    //初始设置信号阻塞
    ui.pushButton->blockSignals(true);
    count = 0;
    //连接按钮点击信号和SlotTest槽函数
    connect(ui.pushButton, &QPushButton::clicked, this,  &QtGuiAppTest::SlotTest);
    //连接下拉列表comboBox的选择改变信号与SlotComboxChg槽函数
    connect(ui.comboBox,  QOverload<int>::of(&QComboBox::currentIndexChanged),  this,
            &QtGuiAppTest::SlotComboxChg);
}
void QtGuiAppTest::SlotTest()
{
   
    ui.label->setText("count:" +  QString::number(count));
    count++;
}
void QtGuiAppTest::SlotComboxChg(int id)
{
   
        //如果下拉列表为启用阻塞信号,则设置pushButton的blockSignals为true
    if(id == 0)
    {
   
        ui.pushButton->blockSignals(true);
    }
    //如果下拉列表为关闭阻塞信号,则设置pushButton的blockSignals为true
    if(id == 1)
    {
   
        ui.pushButton->blockSignals(false);
    }
}

2.2 int startTimer(std::chrono::milliseconds time, Qt::TimerType timerType = Qt::CoarseTimer)

(1)功能说明
这是一个重载函数。
启动一个定时器,并返回该定时器ID。如果没有能够启动定时器则则返回0。
定时器事件会在每个事件间隔触发,直到killTimer()被调用。如果第一个参数time与std::chrono::duration::zero()相等,则定时器事件会在没有其他系统事件需要处理的情况下被反复调用
当一个定时器事件触发,虚函数timerEvent()会被调用并传入 QTimerEvent类的参数
如果多个定时器正在运行,QTimerEvent::timerId()可以被用于查找当前活跃的是哪个定时器
注意:定时器的精度依赖于底层运行的系统和硬件。第二个参数timerType允许去自定义定时器的精度。参考Qt::TimerType 中不同定时器类型枚举值。大多数平台支持一个20毫秒的精度;还有一些提供更高的精度支持。如果qt无法获取并提供定时器事件请求的精度数值,qt将默认丢弃一些精度。
(2)效果展示
在这里插入图片描述

(3)调用程序
QtGuiAppTest.ui
在这里插入图片描述

QtGuiAppTest.h

#pragma once
#include <QtWidgets/QWidget>
#include "ui_QtGuiAppTest.h"
class QtGuiAppTest : public QWidget
{
   
    Q_OBJECT
public:
    QtGuiAppTest(QWidget *parent = Q_NULLPTR);
protected:
    void timerEvent(QTimerEvent* event) override;
private:
    Ui::QtGuiAppTestClass ui;
    int count;
};

QtGuiAppTest.cpp

#include "QtGuiAppTest.h"
QtGuiAppTest::QtGuiAppTest(QWidget *parent)
    : QWidget(parent)
{
   
    ui.setupUi(this);
    //设置定时器时间间隔为1秒
    using namespace std::chrono;
    this->startTimer(1s);
}
void QtGuiAppTest::timerEvent(QTimerEvent* event)
{
   
    ui.label->setText("count:" +  QString::number(count));
    count++;
}

注意:
C++14之前延时时间设置语法如下

  using namespace std::chrono;
  startTimer(milliseconds(50));
  startTimer(seconds(1));
  startTimer(minutes(1));

C++14之后延时时间设置语法如下

  startTimer(100ms);
  startTimer(5s);
  startTimer(2min);
  startTimer(1h);

2.3 int startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer)

(1)功能说明
第一个参数interval表示毫秒数,其他与第2.2节相同。
(2)效果展示
同第2.2节相同。
(3)调用程序

QtGuiAppTest::QtGuiAppTest(QWidget *parent)
    : QWidget(parent)
{
   
    ui.setupUi(this);
    //设置定时器时间间隔为1秒
    this->startTimer(1000);
}

2.4 bool setProperty(const char *name, const QVariant &value)

设置继承自qobject类对象的名称和值,见《QT属性系统(property system,Q_PROPERTY)功能和使用详细说明(文字+用例+代码+效果图)》

2.5 void setParent(QObject *parent)

给当前对象设置一个父类对象。

2.6 void setObjectName(const QString &name)

给对象设置一个名称,可以通过对象名称使用findchild找到一个子对象,默认为空字符串。

2.7 void installEventFilter(QObject *filterObj)

(1)功能说明
安装事件过滤器。
(2)效果展示
给按钮安装一个事件过滤器,过滤双击单击按钮事件,只能通过ctrl+B按下触发label内容改变。
在这里插入图片描述

(3)调用程序
QtGuiAppTest.ui
在这里插入图片描述

QtGuiAppTest.h

#pragma once
#include <QtWidgets/QWidget>
#include "ui_QtGuiAppTest.h"
class QtGuiAppTest : public QWidget
{
   
    Q_OBJECT
public:
    QtGuiAppTest(QWidget *parent = Q_NULLPTR);
public slots:
    //根据按钮点击信号,改变label的内容
    void SlotTestEventFilter();
private:
    Ui::QtGuiAppTestClass ui;
};
/**
事件过滤器
*/
class EnterKeyPressFilter : public QObject
{
   
    Q_OBJECT
public:
    EnterKeyPressFilter(QWidget* parent = Q_NULLPTR);
    //重写事件过滤函数
    bool eventFilter(QObject* watched, QEvent* event)  override;
};

QtGuiAppTest.cpp

#include "QtGuiAppTest.h"
#include<qevent.h>
QtGuiAppTest::QtGuiAppTest(QWidget *parent)
    : QWidget(parent)
{
   
    ui.setupUi(this);
    //创建过事件滤器
    EnterKeyPressFilter* pEKPFilter = new  EnterKeyPressFilter(this);
    //给按钮添加快捷键
    ui.pushButton->setShortcut(QKeySequence(Qt::CTRL +  Qt::Key_B));
    //给按钮安装事件过滤器
    ui
  • 26
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个基于Qt插件机制和OpenCV库实现的图像缩放功能的示例代码: 首先,创建一个Qt插件项目,并将OpenCV库添加到项目中。 在插件类头文件中定义纯虚函数: ```c++ class ImageProcessorInterface { public: virtual ~ImageProcessorInterface() {} virtual void setImage(const QImage& image) = 0; virtual QImage processImage(double scaleFactor) = 0; }; ``` 在插件类实现文件中实现缩放功能: ```c++ #include "imageprocessorinterface.h" #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> class ImageProcessor : public QObject, public ImageProcessorInterface { Q_OBJECT Q_PLUGIN_METADATA(IID "com.example.ImageProcessorInterface" FILE "imageprocessor.json") Q_INTERFACES(ImageProcessorInterface) public: void setImage(const QImage& image) override { m_image = image; } QImage processImage(double scaleFactor) override { cv::Mat mat = cv::Mat(m_image.height(), m_image.width(), CV_8UC4, const_cast<uchar*>(m_image.bits()), m_image.bytesPerLine()); cv::Mat resizedMat; cv::resize(mat, resizedMat, cv::Size(), scaleFactor, scaleFactor); QImage resultImage(resizedMat.data, resizedMat.cols, resizedMat.rows, static_cast<int>(resizedMat.step), QImage::Format_ARGB32); return resultImage; } private: QImage m_image; }; ``` 在主应用程序中加载插件并使用插件提供的函数进行图像缩放: ```c++ #include <QPluginLoader> #include <QPainter> #include "imageprocessorinterface.h" void MainWindow::onScaleImage(double scaleFactor) { if (m_imageProcessorPlugin) { ImageProcessorInterface* imageProcessor = qobject_cast<ImageProcessorInterface*>(m_imageProcessorPlugin->instance()); if (imageProcessor) { QImage processedImage = imageProcessor->processImage(scaleFactor); m_scaledImage = processedImage; update(); } } } void MainWindow::paintEvent(QPaintEvent* event) { QMainWindow::paintEvent(event); QPainter painter(this); painter.drawImage(QPoint(0, 0), m_scaledImage); } void MainWindow::loadImage(const QString& filePath) { QImage image(filePath); if (!image.isNull()) { m_originalImage = image; m_scaledImage = m_originalImage; if (m_imageProcessorPlugin) { ImageProcessorInterface* imageProcessor = qobject_cast<ImageProcessorInterface*>(m_imageProcessorPlugin->instance()); if (imageProcessor) { imageProcessor->setImage(m_originalImage); } } update(); } } void MainWindow::loadImageProcessor(const QString& filePath) { if (m_imageProcessorPlugin) { m_imageProcessorPlugin->unload(); } m_imageProcessorPlugin = new QPluginLoader(filePath, this); if (m_imageProcessorPlugin) { QObject* plugin = m_imageProcessorPlugin->instance(); if (plugin) { ImageProcessorInterface* imageProcessor = qobject_cast<ImageProcessorInterface*>(plugin); if (imageProcessor) { imageProcessor->setImage(m_originalImage); } } } } ``` 需要注意的是,插件的元数据应该定义在一个JSON文件中,例如imageprocessor.json: ```json { "IID": "com.example.ImageProcessorInterface", "MetaData": { "Keys": [ "ImageProcessor" ], "Values": [ "OpenCV Image Processor" ] } } ``` 以上代码仅为示例,实际实现中可能需要进行修改以适应具体应用场景。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懒人空想家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值