control key

This commit is contained in:
Andrei Yankovich 2018-08-16 22:14:01 +03:00
parent 85f161f617
commit 77c4fa3c14
3 changed files with 26 additions and 19 deletions

View File

@ -2,6 +2,7 @@
#include "ui_mainwindow.h"
#include <QLabel>
#include <QDateTime>
#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
@ -13,6 +14,19 @@ MainWindow::MainWindow(QWidget *parent) :
timer.start();
}
void MainWindow::keyPressEvent(QKeyEvent * key) {
if (key->key() == Qt::Key_Space) {
if (timer.isActive()) {
timer.stop();
} else {
timer.start();
}
}
}
MainWindow::~MainWindow() {
delete ui;
}
@ -21,15 +35,5 @@ void MainWindow::handleTick() {
auto time = QDateTime::currentMSecsSinceEpoch();
ui->msec->setText(QString::number( time% 1000));
if (!(time % 1000)) {
if (color == Qt::white) {
color = Qt::green;
} else {
color = Qt::white;
}
ui->color->setStyleSheet(QString("background-color: '%0';").arg(color.name(QColor::HexRgb)));
}
ui->msec->setText(QString::number(time));
}

View File

@ -14,6 +14,7 @@ class MainWindow : public QMainWindow
public:
explicit MainWindow(QWidget *parent = nullptr);
void keyPressEvent(QKeyEvent*);
~MainWindow();
private:

View File

@ -19,19 +19,21 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="msec">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="color">
<property name="styleSheet">
<string notr="true">background-color: rgb(138, 226, 52);</string>
<string notr="true">font: 48pt &quot;Ubuntu&quot;;</string>
</property>
<property name="text">
<string/>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
</layout>