Accessing functions of parent class in child class
-
@-Header.h
class RichEditor : public QTextEdit
{
Q_OBJECT
public:
RichEditor(Datamine *parent);
};
class Datamine : public QMainWindow
{
Q_OBJECTpublic:
Datamine(QWidget *parent = 0);
private:
RichEditor *textEditor;
}Inside datamine.cpp
#include header.h
RichEditor::RichEditor(Datamine parent)//gives error expected ) before '' token--
{
//i wish to add connect to signals of parent and the signal I will trigger in insertfrommimedata(not shown)
}Datamine::Datamine(QWidget *parent)
: QMainWindow(parent)
{}@
What is shown about is only a small part of code.
I am trying to override insertfrommimedata of texteditor and call a function of datamine from within it. THe problem is that I am unable to access richeditors parent(Datamine) to access these functions
I am trying to call the constructor of richeditor with datamine as the class and error says expect ) before * token
I can't move richeditor class to below datamine since in the private of Datamine I have defined RichEditor *texteditor .
I am learning QT this is my first project...please help. Any snippets on accessing child from parent is appreciated -
Is there really no semicolon after the ending brace of class Datamine in header.h?
Are there really no quotes around "header.h" in file datamine.cpp?
Both of these things could cause compiler problems.
Also, before the class definition of RichEditor, add the line:
class Datamine;
and google "forward declaration". -
Hi,
You are preparing yourself for some troubles. Child widget should not know anything about their parent. It's creating a tight coupling that generally ends in maintenance nightmare. You should use signals and slots to communication between the two. The parent widget should be responsible for the connections, but not the child.
Have a look at the various examples and demos you can find within Qt's sources. The documentation itself provides also a lot of them.
One chapter you should start with is "Signals and Slots"Happy learning !
-
Thank you scottR forward declaration solved it.