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. Who's interested in template QObjects?

Who's interested in template QObjects?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.3k 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.
  • JKSHJ Offline
    JKSHJ Offline
    JKSH
    Moderators
    wrote on last edited by
    #1

    Hi all,

    Currently, template classes cannot be QObjects; one of C++'s powerful features cannot be used on QObjects.

    There has been some work done to change this. It wasn't obvious if the benefits outweighed the potential issues or not, and the efforts were put off to another day: http://lists.qt-project.org/pipermail/development/2013-March/010442.html

    I'm interested to see this feature implemented. Does anyone have any cool ideas on how template QObjects could be used?

    Here's one I thought might be handy: The ability to run an arbitrary function in a separate thread, and have the return value delivered straight into a slot -- much cleaner than QtConcurrent::run()

    @
    // Template definition
    template <typename T>
    QParallel : public QObject {
    Q_OBJECT

    public:
    static QParallel* run(T (function)(), bool autoDelete = true) {
    /

    Starts running the function in a separate thread, then returns a
    pointer to let the user connect the finished() signal to their slot(s).

        The user doesn't need to delete the object manually.
        */
    }
    

    signals:
    void finished(T);
    }
    @

    @
    // Expensive function
    QString myExpensiveFunc();
    @

    @
    // User code
    auto p = QParallel<QString>::run(&myExpensiveFunc);
    connect(p, SIGNAL(finished(QString)),
    this, SLOT(processResult(QString)));
    @

    (This example doesn't take parameters for myExpensiveFunc(), but those can easily be supported via C++11's variadic templates)

    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      I do not have a cool idea for where to have templated QObject.
      However in some case it would be convenient to use a template with QObject.

      As you pointed already out the current implementations are simply cutting off one of C++ powerful possibilities. Certainly there are always ways around it, but very soon you find yourself copy and pasting code and changing the class names.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        [quote author="koahnig" date="1390484857"]very soon you find yourself copy and pasting code and changing the class names. [/quote]Precisely! Keeping them in sync with each other is not a pleasant task, either.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        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