call to non static member function without an object argument
-
wrote on 12 Nov 2020, 19:59 last edited by Chaki 11 Dec 2020, 20:03
Hello everyone,
I am trying to implement Signal&Slots. But I am getting the Error: "call to non static member function without an object argument "mainwindow.cpp
void MainWindow::on_Button_clicked() { addressEntry *name = new addressEntry(); name->setName(ui->lineName->text()); connect(addressEntry::setName(name), SIGNAL(entry(QString)), addressReaderWriter::Write(name), SLOT(Write(QString))); }
addressEntry.cpp
void addressEntry::setName(QString name) { mName = name; qDebug() << "setName"; emit entry(name); } QString addressEntry::getName() { return mName; }
addressReaderWriter.cpp
void addressReaderWriter::Write(QString name){ name.getName(); }
Hope Someone can help.
-
Hi,
Your connect statement is wrong.
setName is a member function that you are calling like if it was a static function.
The first and third parameters of connect should be objects.
-
Hi,
Your connect statement is wrong.
setName is a member function that you are calling like if it was a static function.
The first and third parameters of connect should be objects.
wrote on 12 Nov 2020, 20:24 last edited by Chaki 11 Dec 2020, 20:27@SGaist Thank you for your reply. I understand the problem, but could you tell me how I can solve it?
My goal is it to transfer the variable "name->setName(ui->lineName->text());"from void MainWindow::on_btnAdd_clicked() to void addressReaderWriter::Write(QString name){
by using Signal&Slots -
Dis you read the documentation of the method ?
The first parameter shall be the object that will emit the signal and the third the object that will have the slot activated.
-
Dis you read the documentation of the method ?
The first parameter shall be the object that will emit the signal and the third the object that will have the slot activated.
-
@Chaki said in call to non static member function without an object argument:
but I am having trouble to implement it correctly
Then please show what you have now...
-
@Chaki said in call to non static member function without an object argument:
but I am having trouble to implement it correctly
Then please show what you have now...
wrote on 13 Nov 2020, 07:23 last edited by@jsulm currently I am trying:
mainwindow.cpp:
void MainWindow::on_btnAdd_clicked() { addressEntry *name = new addressEntry(); name->**setName**(ui->lineEdit->text()); connect(&name, SIGNAL(addressEntry::entry(QString)), addressReaderWriter::Write(name), SLOT(addressReaderWriter::Write(QString))); }
addressentry.h:
signals: void entry(QString name);
addressentry.cpp:
void addressEntry::setName(QString name) { mName = name; emit entry(name); }
addressReaderWriter.h:
public slots: void Write(QString);
addressReaderWriter.cpp
void addressReaderWriter::Write(QString name){ name.getName(); }
-
Your signal/slot signatures in the connect() are wrong. Please read https://doc.qt.io/qt-5/signalsandslots.html before using something.
-
Your signal/slot signatures in the connect() are wrong. Please read https://doc.qt.io/qt-5/signalsandslots.html before using something.
wrote on 13 Nov 2020, 07:37 last edited by@Christian-Ehrlicher I know that the connect is wrong and I have read the Docu, but I am having trouble using it correctly
-
There is so much wrong in your code I don't even know where to start. I would suggest you to start with one of the Qt examples and learn c++ - sorry.
-
@Christian-Ehrlicher I know that the connect is wrong and I have read the Docu, but I am having trouble using it correctly
@Chaki said in call to non static member function without an object argument:
I have read the Docu
If you did then why are you still doing it wrongly? Your code does not even make sence to be honest.
connect(name, &addressEntry::entry, addressReaderWriterInstancePtr, &addressReaderWriter::Write);
addressReaderWriterInstancePtr would be a pointer to an instance of addressReaderWriter which you want to react on the signal.
-
@MalitheBIG said in call to non static member function without an object argument:
Thats not how you treat new useres who try to get into QT.
Before you learn Qt you have to learn c++, thats the reality - sorry.
-
wrote on 13 Nov 2020, 08:27 last edited by
@MalitheBIG Da stimme ich Ihnen zu
-
@MalitheBIG said in call to non static member function without an object argument:
NUR WEIL DU EHRLICH
Bitte nicht persöhnlich werden...
-
wrote on 13 Nov 2020, 08:29 last edited by
@MalitheBIG That's true, maybe try and help him instead of just calling him stupid
-
@MalitheBIG That's true, maybe try and help him instead of just calling him stupid
@Mr_Kohli said in call to non static member function without an object argument:
instead of just calling him stupid
I never did - I told him/her to learn basic c++ before starting with Qt.
-
@MalitheBIG what are you talking about, knowing/learning c++ is not personal stuff its essential to use Qt
-
@MalitheBIG Qt is a C++ framework. How do you want to use Qt without knowing C++?
-
wrote on 13 Nov 2020, 08:51 last edited by JonB
@MalitheBIG , @Chaki
In order to use Qt you do need to know C++. This forum is for helping people with Qt questions. We do try, but we are not a C++ tutorial forum. That's just how it is.There's nothing wrong with learning to program and not yet knowing C++ well enough. Nobody blames you for that. It's just that here is not the best place to ask about C++ questions, there are better, more suitable C++ learning forums out there.
Having said that. The Qt
connect()
statement requires 4 arguments, 2 pairs of 2, as follows:connect(signallingObject, signallingMethod, slotObject, slotMethod);
signallingObject
is some object on which something happens --- like a click.signallingMethod
is a method (belonging to thesignallingObject
class, declared as asignals
method in the.h
file).slotObject
is some object which wants to know when the event happens --- like, show a message whensignallingObject
gets clicked.slotMethod
is a method (belonging to theslotObject
class, declared as aslots
method in the.h
file).slotMethod
should accept the parameters whichsignallingMethod
sends.
So a simple example example looks like
// In, say, your `MainWindow` class connect(ui->someButton, &QPushButton::clicked, this, &MainWindow::onButtonClicked); /*slot*/ MainWindow::onButtonClicked() { qDebug() << "Button was clicked"; }
The important think is that the
connect()
will usually look like:connect(signallingObject, &SignallingObjectClass::method, slotObject, &SlotObjectClass::method);
You would benefit hugely from taking some time to study some of the tutorial signals & slots examples out there on the web. They are better than asking people here to come up with examples. If you Google for
qt5 signal slot tutorial
you get several suggestions, not only in the Qt docs, which is where I would start from:
https://doc.qt.io/qt-5/signalsandslots.html
https://www.bogotobogo.com/Qt/Qt5_SignalsSlotsGui.php
https://prognotes.net/2019/12/qt-5-signals-and-slots-tutorial/
https://www.vikingsoftware.com/getting-the-most-of-signal-slot-connections/
https://wiki.qt.io/New_Signal_Slot_SyntaxP.S.
I will strongly recommend one thing. In @Chaki's code, and in a lot of older examples on the web, the old styleSIGNAL()
&SLOT()
macros are used for the methods. Do not do this. It might look simpler than the new style&Class::method
, but it allows you to write nonsense, which gets through the compiler but doesn't work at runtime. Using the new style will only allow you to write something sensible at compile time, and if it rejects what you try it means it was not correct. The new style is covered in the https://wiki.qt.io/New_Signal_Slot_Syntax link I gave above.
1/23