Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QObject * as function parameter
Forum Updated to NodeBB v4.3 + New Features

QObject * as function parameter

Scheduled Pinned Locked Moved General and Desktop
12 Posts 7 Posters 5.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    antonio
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      guziemic
      wrote on last edited by
      #2

      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,

      1 Reply Last reply
      0
      • A Offline
        A Offline
        antonio
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • N Offline
          N Offline
          noah
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DerManu
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              qxoz
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KA51O
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tzander
                  wrote on last edited by
                  #8

                  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 ;)

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    antonio
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      qxoz
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        tzander
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          qxoz
                          wrote on last edited by
                          #12

                          Thomas Zander@ Thank you.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved