首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > QT开发 >

在线程中调用键盘事件偶有点有关问题

2012-05-27 
在线程中调用键盘事件偶有点问题#include QtCore#include stdio.h#include stdlib.h#include QDebu

在线程中调用键盘事件偶有点问题

#include <QtCore>
#include <stdio.h>
#include <stdlib.h>
#include <QDebug>
#include<QKeyEvent>
const int DataSize = 15; //总共显示在屏幕上的字节
const int BufferSize = 5; //缓冲区的大小
int buffer[BufferSize];
int aa;
QWaitCondition bufferNotEmpty;//当缓冲区为空时,读取线程会等侍有数据后再执行读操作
QWaitCondition bufferNotFull;//当缓冲区写满时,写入线程会等侍有东西被读走以后再写入
QMutex mutex; //线程间数据的操作
int numUsedBytes = 0; //当前缓冲区的写入字节
QKeyEvent *k;


class WriteBufferThread : public QThread
{
public:
void run();
};

class ReadBufferThread: public QThread
{
public:
void run();
};
void WriteBufferThread::run()
{
for (int i = 0; i < DataSize; ++i) {

mutex.lock();
if (numUsedBytes == BufferSize)
bufferNotFull.wait(&mutex);//缓冲区已满,等侍有数据被读走



 buffer[i % BufferSize]=k->key();

sleep(1);

++ numUsedBytes; //每写入一个字节缓冲区使用数加 1
bufferNotEmpty.wakeAll();//并且告之此时缓冲区肯定不为空
mutex.unlock();
}
}



void ReadBufferThread::run()
{
for (int i = 0; i < DataSize; ++i) {
mutex.lock();
if (numUsedBytes == 0)
bufferNotEmpty.wait(&mutex); //缓冲区已空,等侍有数据写入




aa=buffer[i%BufferSize];
sleep(1);

--numUsedBytes; //每读取一个字节缓冲区使用数减 1
bufferNotFull.wakeAll();//并且告之此时缓冲区肯定不满
mutex.unlock();
}

}

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
WriteBufferThread write;
ReadBufferThread read;
write.start();//启动写线程
read.start(); //启动读线程
write.wait(); //等侍写线程退出
read.wait(); //等侍读线程退出
return 0;
}

大家帮忙看下这个程序 有问题 我想实现在线程中监听键盘按下

[解决办法]

探讨

随便踩下我就给分

[解决办法]
那就在线程中 ,进行按键的判断就行了

响应 keyPressEvent ( QKeyEvent * e )
 

热点排行