ZBLOG

E:\YZYEdu\YZYEdu\SendPaper\answeritem.cpp:9: error: no matching function for call to 'answerItem::connect(QLabel*&, void (answerItem::*)(), answerItem*, void (answerItem::*)())' connect(label, &answerItem::labelClicked, this, &answerItem::onL...

错误信息表明你在 connect 语句中使用了错误的槽函数。这是因为 onLabelClickedMainWindow 类的成员函数,而不是 answerItem 类的成员函数。

请修改 connect 调用,确保槽函数的类名是正确的。应该将 this 更改为指向 MainWindow 的实例。这里是正确的代码示例:

#include <QWidget>
#include <QVBoxLayout>
#include <QLabel>
#include <QMessageBox>
#include "answeritem.h" // 确保包含 answerItem 的头文件

class MainWindow : public QWidget {
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

private slots:
    void onLabelClicked();

private:
    answerItem *label; // 假设你已经定义了 answerItem 类
};

MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent) {

    label = new answerItem(this);
    
    connect(label, &answerItem::labelClicked, this, &MainWindow::onLabelClicked); // 修改这里

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(label);
}

void MainWindow::onLabelClicked() {
    QMessageBox::information(this, "Clicked", "Label was clicked!");
}

总结

  1. 连接信号和槽时

    • 使用正确的对象来进行连接。
    • 在这个例子中,槽函数位于 MainWindow 类,所以应当使用 this 指向 MainWindow 实例。
  2. 确认类定义

    • 确保你已经在适当的位置(如头文件)定义了信号和插槽,并且包括了必要的头文件。
  3. 其他注意事项

    • 在 Qt 中,一般情况下需要使用宏如 Q_OBJECT 来启用元对象系统,以便信号和槽功能正常工作。

如果有进一步的问题,请继续提问!


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=21461

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?