改成Qwidget
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
# This file is used to ignore files which are generated
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
*~
|
||||
*.autosave
|
||||
*.a
|
||||
*.core
|
||||
*.moc
|
||||
*.o
|
||||
*.obj
|
||||
*.orig
|
||||
*.rej
|
||||
*.so
|
||||
*.so.*
|
||||
*_pch.h.cpp
|
||||
*_resource.rc
|
||||
*.qm
|
||||
.#*
|
||||
*.*#
|
||||
core
|
||||
!core/
|
||||
tags
|
||||
.DS_Store
|
||||
.directory
|
||||
*.debug
|
||||
Makefile*
|
||||
*.prl
|
||||
*.app
|
||||
moc_*.cpp
|
||||
ui_*.h
|
||||
qrc_*.cpp
|
||||
Thumbs.db
|
||||
*.res
|
||||
*.rc
|
||||
/.qmake.cache
|
||||
/.qmake.stash
|
||||
|
||||
# qtcreator generated files
|
||||
*.pro.user*
|
||||
|
||||
# xemacs temporary files
|
||||
*.flc
|
||||
|
||||
# Vim temporary files
|
||||
.*.swp
|
||||
|
||||
# Visual Studio generated files
|
||||
*.ib_pdb_index
|
||||
*.idb
|
||||
*.ilk
|
||||
*.pdb
|
||||
*.sln
|
||||
*.suo
|
||||
*.vcproj
|
||||
*vcproj.*.*.user
|
||||
*.ncb
|
||||
*.sdf
|
||||
*.opensdf
|
||||
*.vcxproj
|
||||
*vcxproj.*
|
||||
|
||||
# MinGW generated files
|
||||
*.Debug
|
||||
*.Release
|
||||
|
||||
# Python byte code
|
||||
*.pyc
|
||||
|
||||
# Binaries
|
||||
# --------
|
||||
*.dll
|
||||
*.exe
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
QT += core gui
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
|
||||
CONFIG += c++11
|
||||
|
||||
# The following define makes your compiler emit warnings if you use
|
||||
# any Qt feature that has been marked deprecated (the exact warnings
|
||||
# depend on your compiler). Please consult the documentation of the
|
||||
# deprecated API in order to know how to port your code away from it.
|
||||
DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
# You can also make your code fail to compile if it uses deprecated APIs.
|
||||
# In order to do so, uncomment the following line.
|
||||
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
widget.cpp
|
||||
|
||||
HEADERS += \
|
||||
widget.h
|
||||
|
||||
FORMS += \
|
||||
widget.ui
|
||||
|
||||
# Default rules for deployment.
|
||||
qnx: target.path = /tmp/$${TARGET}/bin
|
||||
else: unix:!android: target.path = /opt/$${TARGET}/bin
|
||||
!isEmpty(target.path): INSTALLS += target
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
#include "widget.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
Widget w;
|
||||
w.show();
|
||||
return a.exec();
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
#include "widget.h"
|
||||
#include "ui_widget.h"
|
||||
#include <QPainter>
|
||||
#include <QPen>
|
||||
#include <QBrush>
|
||||
#include <QTimer>
|
||||
|
||||
struct care
|
||||
{
|
||||
int num;
|
||||
long color;
|
||||
|
||||
};
|
||||
|
||||
care car[10][10];
|
||||
|
||||
Widget::Widget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, ui(new Ui::Widget)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
QTimer *mainrun;
|
||||
|
||||
|
||||
for(int y=0;y<10;y++)
|
||||
{
|
||||
for(int x=0;x<10;x++)
|
||||
{
|
||||
car[y][x].color=0xffffff;
|
||||
car[y][x].num=0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
mainrun = new QTimer(this);
|
||||
connect(mainrun,&QTimer::timeout,this,&Widget::mainrun_timeout);
|
||||
mainrun->start(1);
|
||||
}
|
||||
|
||||
Widget::~Widget()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void Widget::paintEvent(QPaintEvent *)
|
||||
{
|
||||
//QPainter p(this); //方法1 创建画家对象 并指定绘图设备
|
||||
|
||||
|
||||
/*
|
||||
*方法2 创建画家对象
|
||||
*通过bggin指定绘图设备
|
||||
*中间做绘图操作
|
||||
*结尾需要end结束
|
||||
*/
|
||||
QPainter p;
|
||||
p.begin(this);
|
||||
|
||||
|
||||
//定义画笔
|
||||
QPen pen;
|
||||
QBrush brush;//创建笔刷
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
pen.setWidth(1);//设置线宽
|
||||
pen.setColor(QColor(28,28,28)); //常用颜色
|
||||
pen.setStyle(Qt::SolidLine); //设置风格
|
||||
p.setPen(pen); //将画笔给画家
|
||||
|
||||
brush.setStyle(Qt::SolidPattern);//设定笔刷样式
|
||||
|
||||
|
||||
|
||||
|
||||
#define car_size 80
|
||||
for(int y=0;y<10;y++)
|
||||
{
|
||||
for(int x=0;x<10;x++)
|
||||
{
|
||||
brush.setColor(car[y][x].color); //设定笔刷
|
||||
p.setBrush(brush); //笔刷交给画家
|
||||
p.drawRect(x*car_size,y*car_size,car_size,car_size);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
p.end();
|
||||
|
||||
}
|
||||
|
||||
void Widget::mainrun_timeout()
|
||||
{
|
||||
Widget::update();
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
#ifndef WIDGET_H
|
||||
#define WIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui { class Widget; }
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class Widget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Widget(QWidget *parent = nullptr);
|
||||
~Widget();
|
||||
void mainrun_timeout();
|
||||
protected:
|
||||
//重写绘图事件 虚函数
|
||||
//如果在窗口绘图,必须放在绘图事件里实现
|
||||
//绘图事件内部会自动调用,窗口需要重绘的时候(状态改变)
|
||||
void paintEvent(QPaintEvent *);
|
||||
private:
|
||||
Ui::Widget *ui;
|
||||
};
|
||||
#endif // WIDGET_H
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>Widget</class>
|
||||
<widget class="QWidget" name="Widget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Widget</string>
|
||||
</property>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
Reference in New Issue
Block a user