一个基于QT支持定时器的控制台程序例子

13 篇文章 0 订阅

程序运行在Windows平台下。非阻塞控制台编程例子。、

#include <Windows.h>
#include <QtCore/QCoreApplication>
#include <QtCore/QTimer>
#include <iostream>
#include <conio.h>
#include <string>
#include <QtCore/QTimer>
#include "../GVController/uiinf.h"

#ifdef _DEBUG
#pragma comment(lib, "..\\x64\\Debug\\xxController.lib")
#else
#pragma comment(lib, "..\\x64\\Release\\xxController.lib")
#endif

void clearInputBuffer()
{
	HANDLE input_handle = GetStdHandle(STD_INPUT_HANDLE);
	FlushConsoleInputBuffer(input_handle);
}

unsigned short peek_keypress()
{
	HANDLE input_handle = GetStdHandle(STD_INPUT_HANDLE);
	DWORD events = 0;    
	INPUT_RECORD input_record; 
	DWORD input_size = 1;  
	BOOL peeked = PeekConsoleInput(input_handle, &input_record, input_size, &events);
	if (!peeked)
		return 0;

	DWORD n;
	if (peeked && GetNumberOfConsoleInputEvents(input_handle, &n))
	{
			clearInputBuffer();
			if (!input_record.Event.KeyEvent.bKeyDown)
				return input_record.Event.KeyEvent.wVirtualKeyCode;
	}

	return 0;
}

int PrintDicomROIMenu()
{
	std::wstring retString;
	int retCode = -1;

	while (true)
	{
		retString.clear();
		retCode = -1;
		std::cout << std::endl;
		std::cout << "1: LoadDicomDir" << std::endl;
		std::cout << "2: LoadNiffi" << std::endl;
		std::cout << "3: test null file path" << std::endl;
		std::cout << "H: Back Root Menu" << std::endl;

		int c = 0;
		while (!(c = peek_keypress()))
			QCoreApplication::instance()->processEvents();

		if (c == 'H')
			return  0;

		if (c == '2')
		{
			if (true == controllerIns()->parseNiffiFile(L"D:\\VTK_DTA\\XueBaoLin.inffi.gz", NULL, retCode, retString))
				std::cout << "parse D:\\VTK_DTA\\XueBaoLin.inffi.gz ret" << std::endl;
			else
				std::cout << "no handler" << std::endl;

			continue;
		}

		if (c == '3')
		{
			if (controllerIns()->parseNiffiFile(L"", NULL, retCode, retString))
			{
				if (retCode != SUCCESS)
				{
					std::cout << "ret error:" << std::endl;
					std::wcout << retString.c_str() << std::endl;
				}
			}
			continue;
		}
	}

	return 0;
}

int  PrintTotalMenu()
{
	while (true)
	{
		std::cout << std::endl;
		std::cout << "1: User Mananger" << std::endl;
		std::cout << "2: Patient Manager" << std::endl;
		std::cout << "3: DICOM ROI" << std::endl;
		std::cout << "4: Register" << std::endl;
		std::cout << "q: Quit App" << std::endl;

		int c = 0;
		while (true)
		{
			c = peek_keypress();
			if (c != 0)
				break;

			QCoreApplication::instance()->processEvents();
		};
	

		if (c == '3')
			PrintDicomROIMenu();

		if (c == 'Q')
			return 1;
	}

	return 0;
}

int main(int argc, char* argv[])
{
	std::wcout.imbue(std::locale("chs")); //支持输出中文
	clearInputBuffer();

	QCoreApplication a(argc, argv);

	if (!controllerIns()->initialize())
	{
		std::cout << "initilize controller failde" << std::endl;
		return -1;
	}
	QTimer* timer = new QTimer();
	timer->setInterval(50);
	timer->start();
	QObject::connect(timer, &QTimer::timeout, [=] {
		controllerIns()->on50Ms();
		});

	while (true)
	{
		if (1 == PrintTotalMenu())
		{
			releaseController();
			a.processEvents();
			delete timer;
			return 0;
		}
	}

	return a.exec();
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值