Solved Can't Compile connect method
-
I am new to Qt. I am trying to learn to make Notepad from tutorial available online by Qt.
The following method throws an undefined reference to 'Notepad::newDocument()'
error:connect(ui->actionNew,&QAction::triggered,this,&Notepad::newDocument);
Some tips on this please...
-
So where did you implement the function Notepad::newDocument() ?
-
Hi and welcome to the forums.
If this example
https://doc.qt.io/qt-5/qtwidgets-tutorials-notepad-example.html
Then first you add the slotclass Notepad : public QMainWindow { Q_OBJECT public: explicit Notepad(QWidget *parent = 0); ~Notepad(); private slots: void newDocument(); <<< ...
then later they show the implementation. Which goes in the cpp file.
void Notepad::newDocument() { currentFile.clear(); ui->textEdit->setText(QString()); }
-
@mrjj
Thanks a lot...
I tried that as well it doesn't work. :( -
Hi,
You have to give more details. "Doesn't work" goes from missing a semi-colon to asteroid hitting the earth Tunguska style.
-
@SGaist I understand. below is the in detail description of the problem.
I first declared the signal element shown below with actionNew:
Then I declared a slot like below:
After that I connected them and below show are the compiled results :
-
You have to implement the slot even if empty to get started.
-
@theroark said in Can't Compile connect method:
Then I declared a slot like below:
A declaration is not enough. You must also give it a function body.
- See https://docs.microsoft.com/en-us/cpp/cpp/functions-cpp?view=vs-2019 for an explanation.
- See @mrjj's post above for a solution.
-