QObject * as function parameter



  • Hi

    I need to pass QObjects like QString, QVariant or custom objects as parameters of functions:

    @
    QString str = new QString("Hello");
    myClass.function(str);
    @
    @
    void Class::function(const QObject
    obj){
    QString recoveredStr = (QString)obj
    }
    @

    How can I do this?

    Regards



  • Are you going to cast you object later to QString, or it is just example. I think that C style casting is not safe and second you can always pass pointer to object to some function.

    BR,



  • It is just a example, I need to convert QObject* obj to any other custom class



  • My preferred method -
    @
    if (QString recoveredStr = qobject_cast<QString>(obj))
    {
    //do something with recoveredStr
    }
    @



  • That's wrong. A QString is not a QObject. Neither is a QVariant.

    What you're planning to do is not easily possible, I'm afraid. A pointer to a QObject, a QString and a QVariant are too distinct to be safely handleable under one parameter type out-of-the-box. You could use a QVariant (which includes strings) and write your own QObject* container that can be cast to/from QVariant (i.e. register the container with the meta-type system). However, first I'd rethink the program design whether it really makes sense.



  • Hi antonio.
    Can you tell what you try to do? As says DerManu probably there are more convenient ways to achieve your goals.



  • Are you maybe just looking for "QObject::objectName()":http://qt-project.org/doc/qt-4.8/qobject.html#objectName-prop ? I don't really understand what you're trying to achieve here.



  • I'm guessing that you see a parallel from Java where everything is an object. QObject is indeed a very basic object, but its not the parent of all objects.

    I agree with the other posters that you may want to talk about your bigger goals as the approach is likely going to be different from what you expect ;)



  • Hi, thanks all for your reply, I'm trying de Noah approach, because I need a function that receive a generic object and depending on a state uses that generic object as a QString, QByteArray, a custom object or whatever object.

    Until today, the Noah approach works.

    Thanks all.



  • I apologize for the ignorance but what is Noah approach?
    In your case you can create for variables like QString some shell object:
    @class StringObject : public QObject
    {
    public:
    void setValue(QString str);
    QString getValue();
    private:
    QString str;
    }@
    or something like that.



  • The 'noah approach' is likely after the 3nd reply, fron noah.
    Which as the 4th reply points out, can't work for many cases.

    Either way, its not very object oriented, I suggest looking at the "Strategy design pattern" (also called the policy design pattern sometimes).
    Next to that basic C++ has polymorphism for this purpose too.

    So, if the original poster at one point thinks that it may not have been the best solution, those are worth looking into, in my humble opinion.



  • Thomas Zander@ Thank you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.