可以显示5x5的格子,且每个格子里面的数字都是随机且不重复的
This commit is contained in:
@@ -17,10 +17,12 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
widget.cpp
|
||||
widget.cpp \
|
||||
workthread.cpp
|
||||
|
||||
HEADERS += \
|
||||
widget.h
|
||||
widget.h \
|
||||
workthread.h
|
||||
|
||||
FORMS += \
|
||||
widget.ui
|
||||
|
||||
+10
-11
@@ -4,15 +4,10 @@
|
||||
#include <QPen>
|
||||
#include <QBrush>
|
||||
#include <QTimer>
|
||||
#include "WorkThread.h"
|
||||
|
||||
struct care
|
||||
{
|
||||
int num;
|
||||
long color;
|
||||
|
||||
};
|
||||
|
||||
care car[10][10];
|
||||
extern care car[5][5];
|
||||
|
||||
Widget::Widget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
@@ -35,6 +30,8 @@ Widget::Widget(QWidget *parent)
|
||||
mainrun = new QTimer(this);
|
||||
connect(mainrun,&QTimer::timeout,this,&Widget::mainrun_timeout);
|
||||
mainrun->start(1);
|
||||
WorkThread *workthread = new WorkThread;
|
||||
workthread->start();
|
||||
}
|
||||
|
||||
Widget::~Widget()
|
||||
@@ -54,8 +51,9 @@ void Widget::paintEvent(QPaintEvent *)
|
||||
*结尾需要end结束
|
||||
*/
|
||||
QPainter p;
|
||||
QFont font1("黑体",30,QFont::Bold,true);
|
||||
p.begin(this);
|
||||
|
||||
p.setFont(font1);//画家装配字体
|
||||
|
||||
//定义画笔
|
||||
QPen pen;
|
||||
@@ -76,14 +74,15 @@ void Widget::paintEvent(QPaintEvent *)
|
||||
|
||||
|
||||
|
||||
#define car_size 80
|
||||
for(int y=0;y<10;y++)
|
||||
#define car_size 110
|
||||
for(int y=0;y<5;y++)
|
||||
{
|
||||
for(int x=0;x<10;x++)
|
||||
for(int x=0;x<5;x++)
|
||||
{
|
||||
brush.setColor(car[y][x].color); //设定笔刷
|
||||
p.setBrush(brush); //笔刷交给画家
|
||||
p.drawRect(x*car_size,y*car_size,car_size,car_size);
|
||||
p.drawText((x*car_size)+5,(y*car_size)+90,QString("%1").arg(car[y][x].num));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "workthread.h"
|
||||
#include <time.h>
|
||||
|
||||
care car[5][5];
|
||||
int car_rand_buff[26];
|
||||
int car_rand_int=0;
|
||||
int car_rand=0;
|
||||
int while_buff=0;
|
||||
|
||||
void WorkThread::run()
|
||||
{
|
||||
srand(time(NULL));
|
||||
for(int y=0;y<5;y++)
|
||||
{
|
||||
for(int x=0;x<5;x++)
|
||||
{
|
||||
|
||||
do
|
||||
{
|
||||
while_buff=0;
|
||||
car_rand=(rand()%25)+1;
|
||||
for(int z=0;z<car_rand_int;z++)
|
||||
{
|
||||
if(car_rand_buff[z]==car_rand)
|
||||
{
|
||||
while_buff=1;
|
||||
}
|
||||
}
|
||||
}while(while_buff);
|
||||
|
||||
|
||||
car[y][x].num=car_rand;
|
||||
car_rand_buff[car_rand_int]=car_rand;
|
||||
car_rand_int++;
|
||||
}
|
||||
|
||||
}
|
||||
while (true)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef WORKTHREAD_H
|
||||
#define WORKTHREAD_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QThread>
|
||||
|
||||
class WorkThread : public QThread
|
||||
{
|
||||
|
||||
protected:
|
||||
void run();
|
||||
|
||||
};
|
||||
|
||||
//****************************************************c
|
||||
|
||||
struct care
|
||||
{
|
||||
int num;
|
||||
long color;
|
||||
|
||||
};
|
||||
|
||||
#endif // WORKTHREAD_H
|
||||
Reference in New Issue
Block a user