简体   繁体   English

有没有一种方法可以调试针对该对象排队的Qt插槽调用?

[英]Is there a way to debug Qt slot calls queued for the object?

An object lives in a thread and have some slots, that are called with 一个对象生活在一个线程中,并具有一些插槽,这些插槽用

Qt::QueuedConnection 

or 要么

Qt::BlockingQueuedConnection

Is there a way to know how many slot calls are in the queue? 有没有办法知道队列中有多少个slot calls Or even know number of each slot calls in the that queue ? 甚至不知道该queue number of each slot callsqueue

Maybe a bit offtopic, but next pattern may help you (when you need only one slot call per several operations). 可能有点偏离主题,但是下一个模式可能会对您有所帮助(当您每几个操作只需要一个插槽调用时)。 For example, when you have a lot of unpredictable data changes and you need to update a view. 例如,当您有很多不可预测的数据更改时,您需要更新视图。

class SlotProxy : public QObject
{
  Q_OBJECT

signals:
  void triggered();

public:
  explicit SlotProxy( QObject *parent = nullptr );
  void trigger();

private:
  void onTimeout();

private:
  QPointer<QTimer> _timer;
};

SlotProxy::SlotProxy( QObject *parent = nullptr )
  : _timer{ new QTimer{ this } }
{
  _timer->setInterval( 0 );
  _timer->setSingleShot( true );
  connect( _timer, &QTimer::timeout, this, &SlotProxy::onTimeout );
}

void SlotProxy::trigger()
{
  _timer->start( 0 ); // May be not zero;
}

void SlotProxy::onTimeout()
{
  emit triggered();
}

Usage: 用法:

class MyForm : public QWidget
{
/*...*/
void updateGui();
};

class Worker : public QObject
{
/*...*/
signals:
  void updateRequest();

public:
  explicit Worker( QObject *parent = nullptr );

private:
  void heavyTask();

private:
  SlotProxy _proxy;
};

Worker::Worker( QObject *parent )
  : QObject{ parent }
{
  connect( _proxy, &SlotProxy::triggered, this, &Worker::updateRequest );
}

void Worker::heavyTask()
{
  for ( auto i = 0; i < 1000; i++ )
  {
    /*...*/
    _proxy.trigger();
  }
}

MyForm form;
Worker worker;

QObject::connect( &worker, &Worker::updateRequest, &form, &MyForm::updateGui );
worker.heavyTask();
// here you will have only 1 slot MyForm::updateGui in queue

Subject to improve. 有待改进。 You may call _timer->start( 100 ); 您可以调用_timer->start( 100 ); with non-zero timeout for better latency, but you need to somehow force emitting a signal periodically. 具有非零超时以提供更好的延迟,但是您需要以某种方式强制定期发射信号。 It will do a code a bit more complex, please write a comment if you need help. 它将使代码更加复杂,如果需要帮助,请写评论。

See this question for details. 有关详细信息,请参见此问题 You need to monitor (via a filter) the QMetaCallEvent events. 您需要监视(通过过滤器) QMetaCallEvent事件。

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

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