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. using qobject_cast with QFutureWatcher
Forum Updated to NodeBB v4.3 + New Features

using qobject_cast with QFutureWatcher

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 977 Views 2 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.
  • T Offline
    T Offline
    Thombou
    wrote on last edited by
    #1

    Hi,
    Inside a slot, I want to access the result property of my watcher.
    In my constructor, I have

        QFutureWatcher<bool> *watcher = new QFutureWatcher<bool>;
        QFuture<bool> future;
        connect(watcher, SIGNAL(finished()), this, SLOT(handle_db_connection()));
        connect(watcher, SIGNAL(finished()), watcher, SLOT(deleteLater()));
        future = QtConcurrent::run([=](){return db->init();});
        watcher->setFuture(future);
    

    In the slot handle_db_connection, I want to test the value of the bool return by the thread.
    Here is the implementation of my slot

    void MainWindow::handle_db_connection()
    {
        QFutureWatcher<bool> *watcher = qobject_cast<QFutureWatcher<bool>*>(sender());
        if(!watcher->result()){
             // Put actions here
            }
    }
    

    However at compilation time, the compilers tells me that QObject_cast requires the Q_OBJECT marco. Does it mean that the QFutureWatcher class does not have that macro in its definition? In my class MainWindow, I have put the macro.

    If it's the case, any idea of how to retrieve the value of the watcher in the slot?

    Thanks!

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You can only use qobject_cast<> if the class is derived from QObject. If it's not the case then you have to use other ways like e.g. dynamic_cast<> or static_cast<>.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      T 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        You can only use qobject_cast<> if the class is derived from QObject. If it's not the case then you have to use other ways like e.g. dynamic_cast<> or static_cast<>.

        T Offline
        T Offline
        Thombou
        wrote on last edited by
        #3

        @Christian-Ehrlicher
        You mean my MainWindow class? Because this class derives from QMainWindow

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Thombou said in using qobject_cast with QFutureWatcher:

          You mean my MainWindow class?

          I don't see where a MainWindow is involved in your qobject_cast... it's qobject_cast<QFutureWatcher...>

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          T 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @Thombou said in using qobject_cast with QFutureWatcher:

            You mean my MainWindow class?

            I don't see where a MainWindow is involved in your qobject_cast... it's qobject_cast<QFutureWatcher...>

            T Offline
            T Offline
            Thombou
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            That is what thought, it is related to the destination type. But then I dont understand because QFutureWatcher derives from QObject

            B 1 Reply Last reply
            0
            • T Thombou

              @Christian-Ehrlicher
              That is what thought, it is related to the destination type. But then I dont understand because QFutureWatcher derives from QObject

              B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #6

              @Thombou
              Yes, QFutureWatcher derives from QObject but does not have the Q_OBJECT marco.
              You can see that from qfuturewatcher.h
              I think maybe Q_OBJECT macro cannot be used in a template class.
              You can use a lambda here as a slot.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Why not make the watcher a member of your MainWindow class ?
                Or are you expecting to create other watchers ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                T 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  Why not make the watcher a member of your MainWindow class ?
                  Or are you expecting to create other watchers ?

                  T Offline
                  T Offline
                  Thombou
                  wrote on last edited by
                  #8

                  @SGaist I dont know what are the general good practices... I asked a question about it some days ago but it has not got any reply yet. If I only use the watcher once, does it need to be a member of the class? I thought that I should only put objects as member when they are reused several times.
                  Any advice is welcome :)

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Then a follow-up question. What are you doing in that thread ? Seems you are doing some database setup but that makes it look like you are about to do some nasty stuff with regard to Qt's SQL module.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    T 1 Reply Last reply
                    2
                    • SGaistS SGaist

                      Then a follow-up question. What are you doing in that thread ? Seems you are doing some database setup but that makes it look like you are about to do some nasty stuff with regard to Qt's SQL module.

                      T Offline
                      T Offline
                      Thombou
                      wrote on last edited by Thombou
                      #10

                      @SGaist Indeed I was starting a database connection. However I removed this because I found out that if I start a connection to a database in a thread, then I cannot use it from another thread. I was doing so becasue when the database server is not found, it takes time until db.open() returns false, and during this time, the main window dost not show up and is not responsive.
                      At the end I removed the thread thing, but because I might need it in the future to run expensive computation tasks, I'm still interested in knowing how to use it properly to avoid blocking the main thread (and the event loop).

                      To continue on good practices, here is the topic I have wrote some days ago and I describe what I want to do. and some questions that are related.
                      Just a question: I understood that it is good in general to separate operations on the database (model side) from operations on the view side. Therefore I thought that writing a class that would handle all database requests and return pointers to the different models created would be a good idea. It would also take care of the inserts etc. Is that a good way to work with the QtSql module? In the constructor of my database class I check is there is not already an existing connection.

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        Juan Garcia
                        wrote on last edited by
                        #11

                        @SGaist what you suggest if we are expecting to create other watchers and we need to recover it on slot to read some property from them?

                        SGaistS 1 Reply Last reply
                        0
                        • J Juan Garcia

                          @SGaist what you suggest if we are expecting to create other watchers and we need to recover it on slot to read some property from them?

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Juan-Garcia hi,

                          What kind of properties do you have in mind ?
                          Are you thinking of creating a lot of watchers ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          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