Qt c++不会为所有对象调用move_slot.为什么?
Qt c++ does not call the move_slot for all objects. Why?
int forIterator = 0;
Scenes::Scenes(QWidget * parent): QObject ()
{
setScene(scene);
for(int i = 0; i < Dot::number_of_dotz; i++)
{
QTimer *timer_move = new QTimer();
QObject::connect(timer_move,SIGNAL(timeout()),dotz[i],SLOT(move_slot()));
timer_move->start(10);
}
Dot::number_of_dots 在 move_slot(( 中更新(+1(,因为创建了一个新点,但从未调用新点的move_slot。 为什么会这样(不(发生?
看来你应该在move_slot()
创建一个新的Dot
。 当 Dot::number_of_dotz 静态成员的值大于 0 时Scenes::Scenes(QWidget * parent)
调用构造函数,并且可能您已经在其中创建了一些点实例。因此,Scene
构造函数被调用一次,就不会再次调用。
很明显,您这样做是因为您想在创建Scenes
类后开始扩展 dotz。
我的解决方案是检查move_slot()
中的sender()
类型,如果是 QTimer,您可以在move_slot()
本身中创建新QTimer
。
我建议您为QTimer
设置父级以防止内存泄漏。
void Dot::move_slots(){
// your code
QTimer *caller_object = qobject_cast<QTimer*>(sender());
// caller_object will be nullptr if this slot is not being called
// by an object other than a QTimer
if(caller_object){
QTimer *timer_move = new QTimer(this);
QObject::connect(timer_move,SIGNAL(timeout()),this,SLOT(move_slot()));
timer_move->start(10);
}
}
我希望我对你想要实现的目标的假设是正确的。如果不是这种情况,请发表评论。
相关文章:
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 复制elision、std::move和链式函数调用
- 为什么我的代码中没有调用move构造函数
- 如何调用move构造函数
- 复制初始化:为什么即使关闭了复制省略,也没有调用move或copy构造函数
- 调用了Copy ctor而不是move ctor
- 基于范围的 std::move 调用意外复制构造函数
- 编译器发射std :: move()运行时调用
- 为什么编译器在调用 move 后选择复制 ctor
- 调用专用 std::move()
- 在智能指针的反引用值上调用 std::move()
- Move-CTOR和COPY-CTOR未调用
- 为什么noexcept move构造函数在向量重新分配期间没有被调用
- 为什么 std::move 未定义,尽管编译器使用 -std=c++11 调用
- 反复调用MOVE,其中需要RVALUE参考
- 警告在 std::move'ing 时调用复制 ctor
- 如何在基类中调用 "move" '&&' 构造函数?在C++
- move构造函数在C++中调用了两次吗
- 与move构造函数配对时,会发生意外的析构函数调用
- 为move调用转换构造函数,但未为copy调用转换构造函数