Is there a QObject class constructor similar in wxWidgets? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Joelito
Experienced Solver
Experienced Solver
Posts: 98
Joined: Wed Jun 18, 2008 8:35 pm
Location: Tijuana, BC, México

Is there a QObject class constructor similar in wxWidgets?

Post by Joelito » Wed Jul 15, 2015 6:12 am

in QT4, I can create a class like this:

Code: Select all

class Foo : public QObject
{
public:
     Foo (QObject *parent = 0) : QObject(parent) {}
     // more code..     
};

// somewhere in another class:
Foo *foo = new Foo (this); // no need to call delete, the parent will destroy its children
Is there something similar in wxWidgets?
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with openbox + tint2; Guest: WinXP SP3 + wxMSW 3.0.2 + msvc 2010.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

catalin
Moderator
Moderator
Posts: 1589
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: Is there a QObject class constructor similar in wxWidgets?

Post by catalin » Wed Jul 15, 2015 6:44 am

What exactly are you talking about?
It might be wxWindow that you are looking for, but without describing your goal and being explicit it will be difficult to get a useful answer.

Joelito
Experienced Solver
Experienced Solver
Posts: 98
Joined: Wed Jun 18, 2008 8:35 pm
Location: Tijuana, BC, México

Re: Is there a QObject class constructor similar in wxWidgets?

Post by Joelito » Wed Jul 15, 2015 3:08 pm

I thought that I was clear.

I'm asking if there's a similar object-based like in QT in wxWidgets, same as my example code. The goal? No need to call delete explicit.
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with openbox + tint2; Guest: WinXP SP3 + wxMSW 3.0.2 + msvc 2010.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

User avatar
doublemax
Moderator
Moderator
Posts: 15068
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Is there a QObject class constructor similar in wxWidgets?

Post by doublemax » Wed Jul 15, 2015 3:41 pm

No.
Use the source, Luke!

trayres
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Dec 07, 2011 6:27 am

Re: Is there a QObject class constructor similar in wxWidgets?

Post by trayres » Thu Jul 16, 2015 8:05 pm

doublemax wrote:No.
How do we achieve a similar effect? A base class with a destroy call that does cleanup, and listens for the exit signal, yes?

Not sure how that looks in WxWidgets.

User avatar
doublemax
Moderator
Moderator
Posts: 15068
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Is there a QObject class constructor similar in wxWidgets?

Post by doublemax » Thu Jul 16, 2015 8:21 pm

I'd say that would be unrelated to wxWidgets, but should be trivial to implement.
Use the source, Luke!

trayres
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Dec 07, 2011 6:27 am

Re: Is there a QObject class constructor similar in wxWidgets?

Post by trayres » Thu Jul 16, 2015 8:28 pm

That would be a really cool addition to wxwidgets; I guess it would be trivial, but it's nice if the trivial things are included in wxwidgets as much as possible - anything that lots of people would want to do, it makes sense to include in the library.

Would it make a good suggestion to the developers?

User avatar
doublemax
Moderator
Moderator
Posts: 15068
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Is there a QObject class constructor similar in wxWidgets?

Post by doublemax » Thu Jul 16, 2015 9:02 pm

I'd really like to hear a practical example where this would be useful. In my whole lifetime as a programmer i never needed something like this, otherwise i probably would have written it myself :)
Use the source, Luke!

trayres
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Dec 07, 2011 6:27 am

Re: Is there a QObject class constructor similar in wxWidgets?

Post by trayres » Fri Jul 17, 2015 12:09 am

doublemax wrote:I'd really like to hear a practical example where this would be useful. In my whole lifetime as a programmer i never needed something like this, otherwise i probably would have written it myself :)
It's a very nice feature to not have to free the objects you've created - I assume the Qt developers thought so as well, so they implemented it. I've used this in Qt as well - one less thing to think about. I'm surprised it would be in Qt and not in Wx, especially if people find it useful.

If FreePascal, you get this from deriving from TComponent, so it exists in Lazarus/fpc as well. Object Pascal has a finalize section that's great for freeing memory as well.

User avatar
tierra
Site Admin
Site Admin
Posts: 1343
Joined: Sun Aug 29, 2004 7:14 pm
Location: Salt Lake City, Utah, USA
Contact:

Re: Is there a QObject class constructor similar in wxWidgets?

Post by tierra » Fri Jul 17, 2015 3:09 am

trayres wrote:It's a very nice feature to not have to free the objects you've created - I assume the Qt developers thought so as well, so they implemented it.
You are basically talking about a core language feature, so I still don't see why a GUI toolkit should be responsible for providing this functionality. However, you do get this feature from using smart pointers, which wxWidgets actually does include, but really only does so primarily for it's own use, but it is available for you to use as well. You don't need to use wxWidgets for it though because it's provided by C++11, and is also still available by incorporating the Boost C++ libraries on older compilers.

There's really no reason to arbitrarily add a dependency on wxObject (and thus wxWidgets itself) for all of your non-GUI code just for that one feature. It's really a terrible idea to encourage everyone to subclass everything on top of QObject for the same reason. Personally, I think it was a poor choice on Qt's part to even provide that, it discourages writing modular code, and makes it significantly harder to properly test in an efficient manner.

trayres
Earned a small fee
Earned a small fee
Posts: 10
Joined: Wed Dec 07, 2011 6:27 am

Re: Is there a QObject class constructor similar in wxWidgets?

Post by trayres » Fri Jul 17, 2015 2:59 pm

tierra wrote:
trayres wrote:It's a very nice feature to not have to free the objects you've created - I assume the Qt developers thought so as well, so they implemented it.
You are basically talking about a core language feature, so I still don't see why a GUI toolkit should be responsible for providing this functionality. However, you do get this feature from using smart pointers, which wxWidgets actually does include, but really only does so primarily for it's own use, but it is available for you to use as well. You don't need to use wxWidgets for it though because it's provided by C++11, and is also still available by incorporating the Boost C++ libraries on older compilers.

There's really no reason to arbitrarily add a dependency on wxObject (and thus wxWidgets itself) for all of your non-GUI code just for that one feature. It's really a terrible idea to encourage everyone to subclass everything on top of QObject for the same reason. Personally, I think it was a poor choice on Qt's part to even provide that, it discourages writing modular code, and makes it significantly harder to properly test in an efficient manner.
This is an excellent answer, thank you! I will consider smart pointers and modularity next time.

Post Reply