【问题标题】:SLOT for statusbar->messageChanged() QTstatusbar->messageChanged() QT 的 SLOT
【发布时间】:2021-09-23 09:54:52
【问题描述】:

我对 QT 状态栏有疑问。我想从状态栏接收并处理 changeMes​​sage,我的插槽有问题。我应该如何编写正确的插槽或如何正确使用连接功能,我也有问题。

文件.cpp

connect(statusbar, SIGNAL(messageChanged(const QString &message)), this, SLOT(func1(const QString &message)));

在底部

void file::func1(const QString &message)
{
    statusBarElements->at(0)->setText(statusBarTextElements->at(0));
}

文件.h

private slots:

void func1(const QString &message);

我收到了消息

QMetaObject::connectSlotsByName: No matching signal for func1(QString)

【问题讨论】:

  • 请提供minimal reproducible example 并逐字显示任何错误消息。
  • 在 file.h private slots: void func1(const QString &message); 和上面的 file.cpp 行中。运行代码后,我收到了QMetaObject::connectSlotsByName: No matching signal for func1(QString)
  • 请使用edit 工具将代码添加到您的问题中。
  • 什么是状态栏对象类型?
  • statusbar 是默认的 QStatusBar

标签: c++ qt statusbar uistatusbar qstatusbar


【解决方案1】:

如果您使用的是Qt5,则不需要在连接中使用SIGNALSLOT 宏。

connect(statusbar, &QStatusBar::messageChanged, this, &file::func1);

如果签名不兼容,这将在编译时失败并给您一条错误消息。

编辑: 作为@GM在 cmets 中说明 QMetaObject::connectSlotsByName: No matching signal for func1(QString) 是 Qt 在尝试将插槽用作信号时给出的错误。

【讨论】:

  • 这样我回到开头并收到:QMetaObject::connectSlotsByName: No matching signal for func1(QString) 在程序运行期间
  • @Miziol 对不起,但我不相信错误消息来自您显示的代码。它建议您尝试将func1 用作signal 而不是slot
  • 当我将connect 移动到ui->setupUI(this); 上时,它开始工作,但是当进入连接功能时程序停止并出现错误
  • @Miziol 那是因为在设置 UI 之前状态栏 UI 元素不存在。
  • @Mestkon,但现在我有这个命令:ui->setupUI();statusbar = statusBar();,最后是connect(statusbar, &QStatusBar::messageChanged, this, &file::func1),我应该改变这个吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多