1

I've problem with QT statusbar. I want to recive and process changeMessage from statusbar, bo I have problem with slot. How I should write correct slot or how to use connect function correct, with which I've problem too.

file.cpp

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

and on the bottom

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

file.h

private slots:

void func1(const QString &message);

And I've recived the message

QMetaObject::connectSlotsByName: No matching signal for func1(QString)
Miziol
  • 42
  • 8
  • Please provide a [mcve] and show any error message(s) as text verbatim. – G.M. Jul 14 '21 at 12:10
  • in file.h `private slots: void func1(const QString &message);`, and in file.cpp line as above. After running code I've recived `QMetaObject::connectSlotsByName: No matching signal for func1(QString)` – Miziol Jul 14 '21 at 12:21
  • Please use the [edit](https://stackoverflow.com/posts/68377742/edit) facility to add the code to your question. – G.M. Jul 14 '21 at 12:23
  • what is statusbar object type? – Farshid616 Jul 14 '21 at 12:32
  • statusbar is default QStatusBar – Miziol Jul 14 '21 at 12:34
  • Remove the argument name `message` from the text in the `SIGNAL` and `SLOT` macros. Better still, use the new [signal/slot syntax](https://wiki.qt.io/New_Signal_Slot_Syntax#New:_connecting_to_QObject_member). – G.M. Jul 14 '21 at 12:38
  • @G.M. if I well understand, I should have: `connect(statusbar, messageChanged, this, func1);` ? After change like that I've recived: `reference to non-static member function must be called` and `use of undeclared identifier 'messageChanged'` – Miziol Jul 14 '21 at 12:46
  • Please see the answer by @Mestkon . – G.M. Jul 14 '21 at 12:47

1 Answers1

1

If you are using Qt5 you don't need the SIGNAL and SLOT macros in the connect.

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

This will fail at compile time and give you an error message if the signatures are incompatible.

Edit: As @G.M. stated in the comments QMetaObject::connectSlotsByName: No matching signal for func1(QString) is an error Qt gives when one tries to use a slot as a signal.

Mestkon
  • 3,532
  • 7
  • 18
  • With that I come back to the begining and recive: `QMetaObject::connectSlotsByName: No matching signal for func1(QString)` durring running of the program – Miziol Jul 14 '21 at 12:56
  • @Miziol Sorry, but I don't believe that error message comes from the code you've shown. It suggests you're trying to use `func1` as a `signal` rather than a `slot`. – G.M. Jul 14 '21 at 13:01
  • When I move `connect` over `ui->setupUI(this);` it start work, but program stop with ERROR when step into connect function – Miziol Jul 14 '21 at 13:10
  • @Miziol That is because the statusbar UI element does not exist before you setup the UI. – Mestkon Jul 14 '21 at 13:18
  • @Mestkon, but now I have this order: `ui->setupUI();`, `statusbar = statusBar();` and at the end `connect(statusbar, &QStatusBar::messageChanged, this, &file::func1)`, should I change this? – Miziol Jul 15 '21 at 05:58
  • Ok. well, error was in func1, where I make mistake and don't change pointer to value. But still I don't understand why Compiler and Debuger return mi error in `connect` function. – Miziol Jul 15 '21 at 08:16
  • The only reason why you would get an error message saying `QMetaObject::connectSlotsByName: No matching signal for func1(QString)` is because somewhere in your program you are `connect`ing using a slot where `connect` expects a signal. – Mestkon Jul 15 '21 at 08:39