可以显示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
|
||||
@@ -247,6 +247,7 @@ Makefile: ../QW/QW.pro C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/win32-g++/qmake.
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/qt_config.prf \
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/win32-g++/qmake.conf \
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/spec_post.prf \
|
||||
.qmake.stash \
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/exclusive_builds.prf \
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/toolchain.prf \
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/default_pre.prf \
|
||||
@@ -275,7 +276,6 @@ Makefile: ../QW/QW.pro C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/win32-g++/qmake.
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/lib/Qt5Gui.prl \
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/lib/Qt5Core.prl \
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/lib/qtmain.prl \
|
||||
.qmake.stash \
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/build_pass.prf
|
||||
$(QMAKE) -o Makefile ..\QW\QW.pro -spec win32-g++ "CONFIG+=debug" "CONFIG+=qml_debug"
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/spec_pre.prf:
|
||||
@@ -455,6 +455,7 @@ C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/qt_functions.prf:
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/qt_config.prf:
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/win32-g++/qmake.conf:
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/spec_post.prf:
|
||||
.qmake.stash:
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/exclusive_builds.prf:
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/toolchain.prf:
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/default_pre.prf:
|
||||
@@ -483,7 +484,6 @@ C:/Qt/Qt5.14.2/5.14.2/mingw73_32/lib/Qt5Widgets.prl:
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/lib/Qt5Gui.prl:
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/lib/Qt5Core.prl:
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/lib/qtmain.prl:
|
||||
.qmake.stash:
|
||||
C:/Qt/Qt5.14.2/5.14.2/mingw73_32/mkspecs/features/build_pass.prf:
|
||||
qmake: FORCE
|
||||
@$(QMAKE) -o Makefile ..\QW\QW.pro -spec win32-g++ "CONFIG+=debug" "CONFIG+=qml_debug"
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user