0

My code contains a function which takes a lot of time to compute. To make it feel more responsive, I wanted to visualize every tenth of the progress with a progress bar. However, the function is implemented in another class other than my Main Widget class and I cannot access the ui elements of the Widget class. I tried putting a signal which can be emitted during the function, however it comes up as an error. The relevant code looks like this:

//Class cpp implementation
void Dataset::calculateNew(){

for(int i = 0; i<1000; i++){
     if(i%100==0)
       emit valueChanged(i);  //first Error
        for(int j = 0; j<1000; j++){
            for(int k=0; k<1000; k++){

             //Expensive Matrix calculation
        }
     }
  }
} 
//Class .h implementation

signal:
valueChanged(int value);

//Widget implementation
 
connect(Dataset::calculateNew(), SIGNAL(valueChanged(int)), this, SLOT(updateProgressBar(int))); //second Error here
 

Am I thinking in the right way? What should I do to make it work? Or is there another way to access and change ui Elements of the Widget class.

Note: I tried including the "widget.h" in Dataset class, but it isn´t recognized as a class to be included,

  • In `connect(Dataset::calculateNew(), SIGNAL(valueChanged(int)), this, SLOT(updateProgressBar(int)));` the first parameter is supposed to be a pointer to a QObject but `Dataset::calculateNew()` is a void function – drescherjm Jan 13 '22 at 22:12
  • Can you post the errors you get? I'd also recommend you start using the new Qt signal/slot syntax: https://wiki.qt.io/New_Signal_Slot_Syntax, unless you're using a fairly old version of Qt. – mzimmers Jan 13 '22 at 22:28
  • Sure, the errors are: (commented above) Error 1: undefined reference to `Dataset::valueChanged(int)' – infinitedreamer666 Jan 13 '22 at 22:57
  • Error 2: no matching function for call to 'Widget::connect(Dataset*, const char [19], Widget*, const char [21])' connect(&dataset, SIGNAL(valueChanged(int)), this, SLOT(updateProgressBar(int))); – infinitedreamer666 Jan 13 '22 at 23:01
  • Error 3: no type named 'Object' in 'struct QtPrivate::FunctionPointer' – infinitedreamer666 Jan 13 '22 at 23:01
  • Error 4: no type named 'type' in 'struct std::enable_if' – infinitedreamer666 Jan 13 '22 at 23:01
  • Your code is missing parts that make it impossible to help without a lot of guessing. Please see a [mcve] – drescherjm Jan 13 '22 at 23:03

2 Answers2

0

Hard to say without the minimal example, but I guess the problem lies in your call to connect:

connect(Dataset::calculateNew(), SIGNAL(valueChanged(int)), this, SLOT(updateProgressBar(int))); //second Error here

Provided your dataset object is called ds, it should look like this: connect(&ds, SIGNAL(valueChanged(int)), this, SLOT(updateProgressBar(int)));

BTW, why don't you use the new signal-slot syntax based on fucntion pointers? Are you still on QT4?

alagner
  • 3,448
  • 1
  • 13
  • 25
  • Thanks for the help. I changed it accordingly however now I get this error: no matching function for call to 'Widget::connect(Dataset*, const char [19], Widget*, const char [21])' connect(&dataset, SIGNAL(valueChanged(int)), this, SLOT(updateProgressBar(int))); – infinitedreamer666 Jan 13 '22 at 23:02
  • And when I try to emit the valueChanged(int), I get 'undefined reference to `Dataset::valueChanged(int)' – infinitedreamer666 Jan 13 '22 at 23:04
  • About the new syntax, you are right, however I am required to use this syntax as it has been being used on the entire project. – infinitedreamer666 Jan 13 '22 at 23:05
  • 1
    @infinitedreamer666 do you have Q_OBJECT macro at the beginning of Dataset class? This looks like MOC did not generate the signal. – alagner Jan 13 '22 at 23:24
  • 1
    And IIRC Dataset should inherit from QObject. – alagner Jan 13 '22 at 23:31
0

Don't use these old signals. Use the new

connect(datasetPotr, &Dataset::valueChanged, this,&thisClassObject::updateProgressBar);

Also, this will destroy your loop performance. Because you will push update on each tick & force redraw in loop thread. You should look in to more complex system of notification... Say notify every xx int values, so from 0- 100 do every 10, so you do 10% increments. Etc etc.

Dariusz
  • 960
  • 13
  • 36