본문 바로가기
SWE/Qt

Qt 스톱워치 윈도우 프로그램 1day

by S나라라2 2021. 2. 17.
반응형

main.cpp

timer.cpp timer.h -> timer.ui

clockthread.cpp clockthread.h

 

TBD

- 마우스 좌표로 프로그램 위치 옮기기

- 메뉴바 없애기

- pause 동작하기

 

 

main.cpp

#include "timer.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	timer w;
	w.show();
	return a.exec();
}

timer.h

#pragma once

#ifndef TIMER_H
#define TIMER_H

#include "clockthread.h"
#include <QtWidgets/QMainWindow>
#include "ui_timer.h"

class ClockThread;

class timer : public QMainWindow
{
	Q_OBJECT

public:
	timer(QWidget *parent = Q_NULLPTR);

private:
	Ui::timerClass ui;
	ClockThread m_clockThread;

signals:
	void reset();

private slots:
	void on_closeButton_clicked();
	void on_startButton_clicked();
	void on_pauseButton_clicked();
	void on_resetButton_clicked();
};

#endif

timer.cpp

#include "timer.h"

/**
 */
timer::timer(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);
	setParent(0); // Create toplevel widget -> ?

	setAttribute(Qt::WA_NoSystemBackground, true);
	// 위젯이 배경이 없는 것을 나타낸다.
	// 위젯이 paint Event를 받을 때, 배경이 자동적으로 다시 그려지지 않는다.
	// WA_OpaquePaintEvent와는 다르게, 새로 노출된 부분이 절대 배경으로 채워지지 않는다.
	// WA_OpaquePaintEvent : 윈도우가 처음에 보여진 이후에, 프로그램이 paint event를 동작시킬 때까지 이것을 통해 user는 볼 수 있다.

	setAttribute(Qt::WA_TranslucentBackground, true);
	// 위젯이 투명한 배경을 가져야만 한다.
	// 위젯의 불투명하지 않은 부분이 투명해진다. 왜냐하면 위젯이 alpha 채널을 가지고 잇기 때문이다.
	// WA_TranslucentBackground 플래그를 셋팅하는 것은 WA_NoSystemBackground를 셋팅하도록 만든다.
	// 또한 윈도우에서 실행되는 프로그램이라면 Qt::FramelessWindowHint 플래그도 설정해야한다.

	setAttribute(Qt::WA_PaintOnScreen); 

	setFixedSize(600,240);

	connect(&m_clockThread, SIGNAL(sendTime(QString)), ui.label, SLOT(setText(QString)), Qt::QueuedConnection);
	connect(this, SIGNAL(reset()), &m_clockThread, SLOT(onReset()));
}

/**
 */
void timer::on_closeButton_clicked()
{
	//ui.label->setText("clicked closeButton");
}

/**
 */
void timer::on_startButton_clicked()
{
	//ui.label->setText("clicked startButton");
	m_clockThread.start();
}

/**
 */
void timer::on_pauseButton_clicked()
{
	//ui.label->setText("clicked pauseButton");
	m_clockThread.wait(); // 파라미터 ULONG_MAX 줘야하는건지?
}

/**
 */
void timer::on_resetButton_clicked()
{
	//ui.label->setText("clicked resetButton");
	emit reset();
}

timer.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>timerClass</class>
 <widget class="QMainWindow" name="timerClass">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>600</width>
    <height>237</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>timer</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QWidget" name="gridLayoutWidget">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>4</y>
      <width>581</width>
      <height>151</height>
     </rect>
    </property>
    <layout class="QGridLayout" name="gridLayout">
     <item row="2" column="1">
      <widget class="QPushButton" name="pause">
       <property name="text">
        <string>pause</string>
       </property>
      </widget>
     </item>
     <item row="1" column="0" colspan="3">
      <widget class="QLabel" name="label">
       <property name="sizePolicy">
        <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
         <horstretch>0</horstretch>
         <verstretch>0</verstretch>
        </sizepolicy>
       </property>
       <property name="text">
        <string>TextLabel</string>
       </property>
       <property name="alignment">
        <set>Qt::AlignCenter</set>
       </property>
      </widget>
     </item>
     <item row="2" column="0">
      <widget class="QPushButton" name="startButton">
       <property name="text">
        <string>start</string>
       </property>
      </widget>
     </item>
     <item row="2" column="2">
      <widget class="QPushButton" name="resetButton">
       <property name="text">
        <string>reset</string>
       </property>
      </widget>
     </item>
     <item row="0" column="2">
      <widget class="QPushButton" name="closeButton">
       <property name="sizePolicy">
        <sizepolicy hsizetype="Minimum" vsizetype="Minimum">
         <horstretch>0</horstretch>
         <verstretch>0</verstretch>
        </sizepolicy>
       </property>
       <property name="text">
        <string>close</string>
       </property>
      </widget>
     </item>
    </layout>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>600</width>
     <height>26</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources>
  <include location="timer.qrc"/>
 </resources>
 <connections/>
</ui>

clockthread.cpp

#include "clockthread.h"

#include <QTimer>

ClockThread::ClockThread(QObject* parent)
: QThread(parent)
{
	init();
}

ClockThread::~ClockThread()
{
}

void ClockThread::init()
{
	m_hours = 0;
	m_minutes = 0;
	m_seconds = 0;
}

void ClockThread::onReset()
{
}

void ClockThread::run()
{
	QTimer timer;
	connect(&timer, SIGNAL(timeout()), this, SLOT(timerHit()), Qt::DirectConnection);
	timer.setInterval(1000); // 1000msc = 1 sec
	timer.start();
	exec();
}

void ClockThread::timerHit()
{
	m_seconds++;
	if(m_seconds>60)
	{
		m_minutes++;
		m_seconds = 0;
	}
	if(m_minutes>60)
	{
		m_hours++;
		m_minutes = 0;
	}

	QString time = QString("%1:%2:%3").arg(m_hours).arg(m_minutes).arg(m_seconds);
	emit sendTime(time);
}

clockthread.h

#ifndef CLOCKTHREAD_H
#define CLOCKTHREAD_H

#include <QThread>
#include <QObject>

class ClockThread : public QThread
{
	Q_OBJECT;

public:
	ClockThread(QObject* parent=Q_NULLPTR);
	virtual ~ClockThread();

private:
	int m_hours;
	int m_minutes;
	int m_seconds;

private:
	void init();
	void run();

signals:
	void sendTime(QString time);

private slots:
	void onReset();
	void timerHit();
};

#endif

 

반응형