0

I want a notice dialog to come up when an action has been triggered multiple times consecutively (so basically a bit like how StickyKeys is enabled). I understand that I can basically do connect(this->trigger, SIGNAL(triggered()), this, SLOT(onTrigger())) for detecting a single trigger, but how could I detect when it happens 10 times?

Thanks.

P.S - how could I do a "don't show this message again" QCheckBox?

islandmonkey
  • 15
  • 1
  • 1
  • 7

2 Answers2

3

You can implement your slot in the following way:

void MyClass::onTrigger()
{
    static int count = 0;
    if (count++ == 10) {
        // show the dialog here
    }
}
vahancho
  • 20,808
  • 3
  • 47
  • 55
0

You would need an external counter for this as the connect method, or QObject cannot do this for you out-of-box. I would write this:

MyClass::MyClass(QObject *parent) : QObject(parent), m_cnt(0)
{
    ...
    // Removed the needless this usage
    connect(trigger, SIGNAL(triggered()), SLOT(onTrigger()));
    ...
}

void MyClass::onTrigger()
{
    if (m_cnt++ == 10) {
        m_dialog.show();
        // or: m_dialog.exec();
    }
}
László Papp
  • 51,870
  • 39
  • 111
  • 135