简体   繁体   English

Qt C++,将 class 实例信号连接到 Widget 主 class

[英]Qt C++, connect class instance signal to Widget main class

My code contains a function which takes a lot of time to compute.我的代码包含一个 function 需要大量时间来计算。 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.但是,function 在我的主小部件 class 之外的另一个 class 中实现,并且我无法访问小部件 ZABB4DZ1ED29DCC40 的 ui 元素。 I tried putting a signal which can be emitted during the function, however it comes up as an error.我尝试放置一个可以在 function 期间发出的信号,但是它作为错误出现。 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.或者是否有另一种方法来访问和更改小部件 class 的 ui 元素。

Note: I tried including the "widget.h" in Dataset class, but it isn´t recognized as a class to be included,注意:我尝试在数据集 class 中包含“widget.h”,但它未被识别为要包含的 class,

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)));如果您的数据集 object 被称为ds ,它应该如下所示: 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?你还在QT4吗?

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.您应该研究更复杂的通知系统......说每 xx 个 int 值通知一次,所以从 0-100 每 10 次执行一次,因此您执行 10% 的增量。 Etc etc.等等等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM