qt - How to access stuff in one function from another, called from third in c++ -


i have code:

void forms::login() {     loginform *loginform = new loginform;     loginform->show(); }  void forms::startgame() {     worldform *worldform = new worldform;     worldform->show();     loginform->hide(); } 

so, trying tie logics 1 separate file, logics.cpp. first using regular functions, couldn't access loginform startgame (cause cannot add .h file, crashes program, don't ask why - qt stuff. these types qwidgets). need is:

  1. how add declaration of worldform & loginform root of class forms (or header file), cannot this.

  2. the final goal call startgame() (or, in case, forms::startgame() static function) class in file, , startgame() should able hide loginform, seemed above.

i can't this:

void forms::world(int a) {     worldform *worldform = new worldform;     if(a==0) {         worldform->show();     }else{         worldform->hide();     } } 

because need worldform created once, , create lots of instances 1 each call.

here's code: https://github.com/ewancoder/game

what you're looking member variables , singleton pattern done in style of qapplication.

what seem doing declaring global variables of widget type:

// interface (.h) extern qwidget mywidget; // implementation (.cpp) qwidget mywidget; 

this never work, since construction of such object happens before main() starts! key difference have between delphi , c++. in delphi, must call constructors of objects yourself. in c++, it's done automatically - , global variables, constructed time main() gets execute. need use pointers instead.

the forms class can declared , implemented shown below.

the class isn't copyable since qscopedpointer isn't copyable, should make explicit human reader. there's no reason class copyable anyway, if, say, used qsharedpointer instead.

the qscopedpointer smart pointer used forms don't leak. destructor qscopedpointer automatically frees allocated form, if any. can used bool, it's true when non-null.

the class enforces invariant there 1 instance of it. instance should constructed in main() function, after qapplication. can access instance anywhere using static forms::instance() method.

for example, in loginform::on_loadbutton_clicked(), you'd use

forms::instance()->world(); 
// core.h - interface  #include <qscopedpointer>  class loginform; class worldform; class forms {     q_disable_copy(forms)     qscopedpointer<loginform> m_loginform;     qscopedpointer<worldform> m_worldform;     static forms * m_instance; // declaration public:     forms();     ~forms();     static forms * instance();     void login(int act = 0);     void world(); };  // forms.cpp - implementation  #include "core.h" #include "forms/loginform.h" #include "forms/worldform.h"  forms * forms::m_instance = 0; // definition // runtime default-constructs static class members, technically // explicit initialization null pointer not necessary.  void forms::login(int act) {     if (!m_loginform) m_loginform.reset(new loginform);     switch(act) {     case 1:         m_loginform->hide();         break;     default:         m_loginform->show();     } }  void forms::world() {     if (!m_worldform) m_worldform.reset(new worldform);     m_worldform->show(); }  forms::forms() {     q_assert(! m_instance);     m_instance = this; }  forms::~forms() {     m_instance = 0; }  forms * forms::instance() {     return m_instance; }  // main.cpp  #include <qapplication> #include "classes/core.h"  int main(int argc, char *argv[]) {     qapplication game(argc, argv);     forms forms;     forms.login();     return game.exec();     // local object instances destructed in c++-generated-code.     // order of destruction opposite order of definition.     // compiler puts following code "here":     // forms.~forms();     // game.~qapplication(); } 

so, 1 might ask, why not make in forms class static , done it? it's not possible due lifetime of static class member variables. such members constructed before main() starts, , destructed after main() ends. that's big problem, since:

  1. you can't leverage smart pointers widgets, since deleted when qapplication isn't there anymore, , that's not allowed. can reset() pointers manually, defeats purpose of smart pointers. smart pointers there don't have manage memory manually.

  2. since there's no instance of forms class - it's wrapper static members - destructor won't ever invoked, , can't leverage c++ clean members automatically.

the can make of methods static, not member variables. so, still ok, makes method implementations cumbersome:

class forms {     q_disable_copy(forms)     qscopedpointer<loginform> m_loginform;     qscopedpointer<worldform> m_worldform;     static forms * m_instance; public:     forms();     ~forms();     static forms * instance();     static void login(int act = 0);     static void world(); };  void forms::login(int act) {     forms * inst = instance();     if (!inst->m_loginform) inst->m_loginform.reset(new loginform);     ... } 

but wrong:

// wrong class forms {     static qscopedpointer<loginform> m_loginform;     static qscopedpointer<worldform> m_worldform; public:     static void login(int act = 0);     static void world(); }; 

it's wrong since scoped pointers destructed, , forms deleted, after main() ends. you're not allowed widgets - not destruct them - no qapplication around.

to work around that, you'd need explicitly free form instances in static destructor-like method, must manually call delete forms. main become delphi written in c++ syntax. have strong bias towards delphi idioms. idioms don't belong in c++ -- must unlearn them. c++ compilers whole lot of code generation required manual labor in delphi. expected leverage that.

// awkward: delphi code in c++ syntax int main(int argc, char *argv[]) {     qapplication game(argc, argv);     forms::login();     int rc = game.exec();     forms::destroy();     return rc; }  // braindead: delphi code in c++ syntax class forms {     static qscopedpointer<loginform> m_loginform;     static qscopedpointer<worldform> m_worldform; public:     static void login(int act = 0);     static void world();     static void destroy(); }; 

this explains why c++ more powerful language delphi. in delphi, have worry lot of things c++ compiler takes care of, preventing forgetting important freeing memory or releasing other resources.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -