quick_exit_at_quick_exit()函数与C ++中的示例

quick_exit

C ++ at_quick_exit()函数 (C++ at_quick_exit() function)

at_quick_exit() function is a library function of cstdlib header. It is used to set a function that should be executed on quick exit. Sometimes we need to write such code (like freeing the memory occupied by the global variables, closing the file objects, etc) that should be executed when the quick exit, any user-defined can be written for it and that can be set as a quick exit function using using at_quick_exit() function.

at_quick_exit()函数cstdlib标头的库函数。 它用于设置应在快速退出时执行的功能。 有时我们需要编写这样的代码(例如释放全局变量占用的内存,关闭文件对象等),该代码应在快速退出时执行,可以为其编写任何用户定义的代码,并将其设置为使用at_quick_exit()函数的快速退出功能

Syntax of at_quick_exit() function:

at_quick_exit()函数的语法:

C++11:

C ++ 11:

    extern "C" int at_quick_exit (void (*func)(void)) noexcept;
    extern "C++" int at_quick_exit (void (*func)(void)) noexcept;

Parameter(s):

参数:

  • func – represents the function to be called on quick exit.

    func –表示快速退出时要调用的函数。

Return value:

返回值:

The return type of this function is int, it returns 0 if the function registered successfully; non-zero, otherwise.

该函数的返回类型为int ,如果该函数成功注册,则返回0;否则返回0。 非零,否则。

Example:

例:

    // defining the function
    void function_name(void){
        // function code
    }

    // setting the function
    at_quick_exit(function_name);

C ++代码演示at_quick_exit()函数的示例 (C++ code to demonstrate the example of at_quick_exit() function)

// C++ code to demonstrate the example of
// at_quick_exit() function

#include <iostream>
#include <cstdlib>
using namespace std;

void myfunc(void)
{
    cout << "Bye, Bye..." << endl;
}

// main() section
int main()
{

    //setting the functions
    at_quick_exit(myfunc);

    cout << "Hi, friends..." << endl;

    cout << "Input first number: ";
    int a;
    cin >> a;

    cout << "Input second number: ";
    int b;
    cin >> b;

    cout << a << "+" << b << " = " << a + b << endl;

    //quick exit
    quick_exit(EXIT_SUCCESS);

    cout << "End of the main()..." << endl;

    return 0;
}

Output

输出量

Hi, friends...
Input first number: 10
Input second number: 20
10+20 = 30
Bye, Bye...

Reference: C++ at_quick_exit() function

参考: C ++ at_quick_exit()函数

翻译自: https://www.includehelp.com/cpp-tutorial/at_quick_exit-function-with-example.aspx

quick_exit

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个完整的示例,演示了如何在 QML 使用自定义的 C++ 模型,并使用 `setData()` 函数来更新数据。 首先,我们需要创建一个自定义的 C++ 模型。假设我们的模型继承自 QAbstractListModel,用于存储字符串列表。 mylistmodel.h: ``` #ifndef MYLISTMODEL_H #define MYLISTMODEL_H #include <QAbstractListModel> class MyListModel : public QAbstractListModel { Q_OBJECT public: explicit MyListModel(QObject *parent = nullptr); // 重写基类虚函数 int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; private: QStringList m_data; }; #endif // MYLISTMODEL_H ``` mylistmodel.cpp: ``` #include "mylistmodel.h" MyListModel::MyListModel(QObject *parent) : QAbstractListModel(parent) { m_data << "Apple" << "Banana" << "Cherry" << "Durian"; } int MyListModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent) return m_data.count(); } QVariant MyListModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || index.row() >= m_data.count()) return QVariant(); if (role == Qt::DisplayRole) return m_data.at(index.row()); return QVariant(); } bool MyListModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role == Qt::EditRole) { m_data[index.row()] = value.toString(); emit dataChanged(index, index); return true; } return false; } ``` 接下来,在 main.cpp 注册模型到 QML 上下文: ``` #include <QGuiApplication> #include <QQmlApplicationEngine> #include "mylistmodel.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); // 注册模型到 QML 上下文 qmlRegisterType<MyListModel>("com.example", 1, 0, "MyListModel"); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); } ``` 最后,在 QML 使用该模型,并调用 `setData()` 函数来更新数据: ``` import QtQuick 2.14 import QtQuick.Window 2.14 import com.example 1.0 Window { visible: true width: 400 height: 400 MyListModel { id: myModel } ListView { anchors.fill: parent model: myModel delegate: Text { text: model.display MouseArea { anchors.fill: parent onClicked: { // 调用 setData() 函数更新数据 myModel.setData(index, "new value", Qt.EditRole); } } } } } ``` 在这个示例,我们创建了一个 ListView,使用自定义的 MyListModel 作为数据源。每个列表项都是一个 Text 控件,用于显示字符串。当用户点击某个列表项时,我们调用 `setData()` 函数来更新该项的值。注意,我们需要传递 Qt.EditRole 作为第三个参数,以指定要更新的角色。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值