c++ - How can a QWidget get the "entire" parent? -
i have instantiated widgetclass in mainwindow.cpp. want pass in "this" widget more (qwidget* parent) (mainwindow* parent). on build, widgetclass established before mainwindow , errors out.
i want instancevariables in mainwindow??
ie:
mywidget(qwidget* parent, mainwindow* parent);
there issues on such task.. maybe problem have use forward declaration include 1 class in 1 other include first ones.. or maybe fact default constructor of qwidget
has first argument default parameter.. error exactly?
anyway, here complete example:
mainwindow.h
#ifndef mainwindow_h #define mainwindow_h #include <qmainwindow> #include "mywidget.h" namespace ui { class mainwindow; } class mainwindow : public qmainwindow { q_object public: explicit mainwindow(qwidget *parent = 0); ~mainwindow(); int foo(int n); private: ui::mainwindow *ui; mywidget *w; }; #endif // mainwindow_h
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <iostream> mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent), ui(new ui::mainwindow) { ui->setupui(this); w = new mywidget(this); this->setcentralwidget(w); } mainwindow::~mainwindow() { delete ui; } int mainwindow::foo(int n) { std::cout << "foo" << std::endl; return n+42; }
mywidget.h
#ifndef mywidget_h #define mywidget_h #include <qwidget> class mainwindow; class mywidget : public qwidget { q_object public: explicit mywidget(mainwindow *main, qwidget *parent = 0); signals: public slots: }; #endif // mywidget_h
mywidget.cpp
#include "mywidget.h" #include "mainwindow.h" #include <iostream> mywidget::mywidget(mainwindow *main, qwidget *parent) : qwidget(parent) { std::cout << main->foo(0) << std::endl; }
does help?
Comments
Post a Comment