You could do this with simple timer (no need of threads / sleeps), here is a working example
#include <QWidget>
#include <QPainter>
#include <QKeyEvent>
#include <QPaintEvent>
#include <QTextOption>
#include <QApplication>
{
Q_OBJECT
public:
Widget()
: text
(QString("Press Key To Start :)")) , textOption(Qt::AlignCenter)
, timer(0)
{
setFixedSize(200, 200);
}
virtual ~Widget() {}
protected:
{
if(timer == 0)
QPainter(this).
drawText(event
->rect
(), text, textOption
);
else
}
{
if(timer == 0)
{
if((event->key() >= Qt::Key_A) and (event->key() <= Qt::Key_Z))
text
= QString('A' + event
->key
() - Qt
::Key_A);
else
text.clear();
timer = startTimer(2000); // 2 Seconds
update();
}
}
{
if(timer == event->timerId())
{
timer = 0;
update();
}
killTimer(event->timerId());
}
private:
int timer;
};
int main(int argc, char *argv[])
{
Widget w;
w.show();
return app.exec();
}
#include "Main.moc"
#include <QWidget>
#include <QPainter>
#include <QKeyEvent>
#include <QPaintEvent>
#include <QTextOption>
#include <QApplication>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget()
: text(QString("Press Key To Start :)"))
, textOption(Qt::AlignCenter)
, timer(0)
{
setFixedSize(200, 200);
}
virtual ~Widget() {}
protected:
void paintEvent(QPaintEvent * event)
{
if(timer == 0)
QPainter(this).drawText(event->rect(), text, textOption);
else
QPainter(this).fillRect(event->rect(), QBrush(QColor(Qt::darkGray)));
}
void keyPressEvent(QKeyEvent * event)
{
if(timer == 0)
{
if((event->key() >= Qt::Key_A) and (event->key() <= Qt::Key_Z))
text = QString('A' + event->key() - Qt::Key_A);
else
text.clear();
timer = startTimer(2000); // 2 Seconds
update();
}
}
void timerEvent(QTimerEvent * event)
{
if(timer == event->timerId())
{
timer = 0;
update();
}
killTimer(event->timerId());
}
private:
QString text;
QTextOption textOption;
int timer;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}
#include "Main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks