【问题标题】:Pass argument to function when clicking pushbutton QT [duplicate]单击按钮QT时将参数传递给函数[重复]
【发布时间】:2021-10-09 01:14:05
【问题描述】:

刚开始使用 Qt(以及 C++),还没有真正习惯 SIGNAL/SLOT 系统。

我已将我的代码简化为:

int main()
{
int number = 1;
int * numptr;
numptr = number;
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(start(numptr);
}

我有一个名为“pushButton”的按钮,当按下它时,我希望它启动一个以整数指针作为参数的“开始”函数。

int start(int * number)
{
// Do something with the number
}

但这似乎不起作用。 点击按钮时如何调用函数start并将数字指针作为参数传递?

【问题讨论】:

    标签: c++ windows qt


    【解决方案1】:

    最简单的方法是连接到 lambda 函数并捕获 numptr:

    connect(ui.pushButton, &QPushButton::clicked, [numptr](){
        // Do something with the number
    }
    

    【讨论】:

    • 是的。我没有注意到 OP 是从 main 调用它的。请注意,OP 在他们自己的代码中也使用了this。我会把它从我的身上删除。
    猜你喜欢
    • 2017-06-04
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多