What is the proper way to forward QProgressBar updates through classes in the logic layer?

StackOverflow https://stackoverflow.com/questions/14865646

  •  09-03-2022
  •  | 
  •  

Question

I'm using a QProgressBar and have already figured out how to send the progress from a specific class in the logic layer with this procedure:

  • setting up the connection in the view-layer class.
  • creating the signal signal in the logic-layer class.
  • modifying the logic-layer class such that it is a QObject.
  • calling QObject as the base-class class constructor for the logic-layer.

Now, I would like my specific class in the logic-layer to send the progress through another class.

For example:

  1. Class A (in the view-layer): create connection for the progress in class B.
  2. Class B (in the logic-layer): create connection for the progress in class C.
  3. Class C: updating the progress so it can be seen in class A (view-layer).

I tried to imitate this procedure for using a connection and signal from class B to C, but it doesn't seem to be working.

Any answer will be appreciated

Was it helpful?

Solution 3

OK I figured it out. What I did is to send the peogress-bar object from MainWindow (view layer) to LogicClass1 (logic layer) and then to connect it with SIGNAL to LogicClass2.

Of course, LogicClass1 should inherit from QObject as mentioned above-

class LogicClass1 : public QObject
{

    Q_OBJECT
   .
   .
   .

And most important thing (because I forgot about it): right click on the project and "Run qmake" :)

OTHER TIPS

I would create a middle layer between the logic layer and the ui layer called service layer, and I would put in interface classes to connect the two layers. The basis of this service layer would be to take messages from the ui and to know what to expect back from the logic layer. Your service layer would then shoot the message to the logic layer, It could then create virtual implementations of the classes you expect back from the logic layer. Fill out to concrete implamentation and pass them back to the ui layer. This may be kind of confusing, but thats because it is. It is a large topic reffered to as a n-tier appliciation or a host of other names but really a good book on design patterns would probably be helpful working on a project that requires interfaces in c++, but you could always probably abstract the ideas through the design pattern being applied in another language like c#. Hope this helped a little, sorry it's not something you can just type and see, but getting a good understanding for what your trying to do I believe would be much more beneficial than just working through a temporary signal slot mechanism issue. If you do provide more code I may be able to diagnose your current situation.

You can use a SIGNAL/SIGNAL connection to forward signals with compatible signatures. In the code sample below, the important part is the connect in LogicClass1's constructor.

// My other logic class
class LogicClass1 : public QObject
{   Q_OBJECT
public:
    LogicClass1(LogicClass2* lc2)
        : lc2(lc2)
    {
        connect(lc2, SIGNAL(signalPrograssIteration(int)), 
                this, SIGNAL(signalPrograssIteration(int)));
    }

signals:
    void signalPrograssIteration(int);

private:
    LogicClass2 *lc2;
};

// My other logic class
class LogicClass2 : public QObject
{   Q_OBJECT
public:
    LogicClass2(){}

signals:
    void signalPrograssIteration(int);

private:
    void doSomething();
};

// the function that actually does stuff
void LogicClass2::doSomthing()
{
    int val = 0;
    for(...)
    {
        .
        .
        emit signalPrograssIteration(val);
        .
        .
    }
}

You can see a similar example in the documentation for QObject::connect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top