main.cpp

#include "mainwindow.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w; w.setFixedSize(760,500); w.setWindowFlags(Qt::WindowCloseButtonHint); w.show(); return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h" #include <QDebug>
#include <QFileDialog>
#include <QPushButton>
#include <QMouseEvent>
#include <QMessageBox>
#include <QtGlobal>//随意数头文件
#include <QTimer>
#include <QTime> #define SMALL_W 130
#define SMALL_H 100
#define PHOTO_X 20
#define PHOTO_Y 110 MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this); timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),
SLOT(onTimerOut())); ui->label_3->setText((QString::number(bushusum)));//步数写入
ui->time_label->setText((QString::number(tim)));//时间
this->setStyleSheet(
"MainWindow{border-image: url(:/beijing.jpg);}");//增加背景图片; // QPixmap tep(":/beijing.jpg");
// MainWindow->setPixmap(QPixmap::fromImage(tep));
// ui->btn->setText("nicai");//最按钮进行命名; pSourceImage=NULL;//制空
// if(pSourceImage==NULL)
// {
// return ;
// }
for(int i=0;i<3;i++){ //将图片划分区域;
for(int j=0;j<3;j++){
pLbImage[i*3+j] = new QLabel(this);
pLbImage[i*3+j]->setGeometry(0,0,SMALL_W,SMALL_H);
pLbImage[i*3+j]->move(PHOTO_X+SMALL_W*i,
PHOTO_Y+SMALL_H*j);
pLbImage[i*3+j]->setFrameShape(QFrame::Box);
} }
connect(ui->btn,SIGNAL(clicked()),this,SLOT(test())); //按键 信号(有按键按下) 链接 槽函数
} MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btn_clicked()
{
//打开对话窗
strFileName =
QFileDialog::getOpenFileName(this,
"select pictures",
"/home/newcapec",
"Images(*.png *.jpg)");
if(strFileName==NULL) return ;
if(NULL!=pSourceImage) //把原来的原来的空间释放掉
{
delete pSourceImage;
pSourceImage = NULL;
}
pSourceImage =new QImage(strFileName);
if(pSourceImage == NULL) return ; QImage tep=pSourceImage ->scaled(ui->label->width(),
ui->label->height());
ui->label->setPixmap(QPixmap::fromImage(tep)); cutimage(); } void MainWindow::onTimerOut()
{
tim++;
ui->time_label->setText((QString::number(tim)));
} void MainWindow::cutimage()//九宫格分割
{
QImage temp = pSourceImage->scaled(SMALL_W*3,
SMALL_H*3);//缩放图片
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
pImage[i][j]=temp.copy(i*SMALL_W, //复制图片开始的坐标;
j*SMALL_H,
SMALL_W,
SMALL_H);//复制图片的大小 pLbImage[i*3+j]->setPixmap(QPixmap::fromImage(pImage[i][j])); //加载图片
pCompare[i][j]=i*3+j;
}
}
QPixmap tep(":/blank.jpg");//挖空
pLbImage[8]->setPixmap(tep);
Random(); } void MainWindow::Random()//打乱
{
bushusum=0;
ui->label_3->setText((QString::number(bushusum)));
tim=0;
ui->time_label->setText((QString::number(tim)));;
int x=-1;
int y=-1;
for(int w=0;w<3;w++){//找到空白格
for(int j=0;j<3;j++){
if(pCompare[w][j]==8){
x=w;
y=j;
break;
}
}
if(x!=-1)
break;
} if(x==-1||y==-1)
return ;
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));//产生一个随机数
for(int i=0;i<1000;i++){ //随机空白格的四个方向
int direction = qrand()%4; switch (direction) {
case 0://上
if(x<2){
pCompare[x][y]=pCompare[x+1][y];
pCompare[x+1][y]=8;
x++;
}
break;
case 1://下
if(x>0){
pCompare[x][y]=pCompare[x-1][y];
pCompare[x-1][y]=8;
x--;
}
break;
case 2://左
if(y<2){
pCompare[x][y]=pCompare[x][y+1];
pCompare[x][y+1]=8;
y++;
} break;
case 3://右
if(y>0){
pCompare[x][y]=pCompare[x][y-1];
pCompare[x][y-1]=8;
y--;
}
break;
default:
break;
}
}
moveImage();
if(timer->isActive())
{
timer->stop();
}
timer->start(1000);
} void MainWindow::moveImage()//图片移动
{
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int index = pCompare[i][j];
pLbImage[index]->move(PHOTO_X+i*SMALL_W,
PHOTO_Y+j*SMALL_H);
}
}
} int MainWindow::panduanwancheng ()
{
int y=1;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(pCompare[i][j]!=i*3+j)
{
y=0;
break;
}
}
if(!y) break;
}
return y;
} void MainWindow::mousePressEvent(QMouseEvent *event)
{
int num=0;
if(pSourceImage==NULL) return ;
if(panduanwancheng()) return ;
if(event->button() == Qt::LeftButton
||event->button() == Qt::RightButton)
{
QPoint pressPoint = event->pos();
if(pressPoint.x()>PHOTO_X&&pressPoint.y()>PHOTO_Y
&&pressPoint.x()<PHOTO_X+SMALL_W*3&&pressPoint.y()<PHOTO_Y+SMALL_H*3)
{
int x = (pressPoint.x()-PHOTO_X)/SMALL_W;
int y = (pressPoint.y()-PHOTO_Y)/SMALL_H; //判断向上移
if(x>0&&pCompare[x-1][y]==8)
{
pCompare[x-1][y]=pCompare[x][y];
pCompare[x][y]=8;
num++;
}else if(x<2&&pCompare[x+1][y]==8)//判断向下移
{
pCompare[x+1][y]=pCompare[x][y];
pCompare[x][y]=8;
num++; }else if(y>0&&pCompare[x][y-1]==8)//判断向左移
{
pCompare[x][y-1]=pCompare[x][y];
pCompare[x][y]=8;
num++;
}else if(y<2 && pCompare[x][y+1] == 8)//判断向右移动
{
pCompare[x][y+1] = pCompare[x][y];
pCompare[x][y] = 8;
num++;
}
}
}
bushusum+=num;
ui->label_3->setText((QString::number(bushusum)));
moveImage();//图片移动
if(panduanwancheng()){
timer->stop();
huifu();
} } void MainWindow::huifu ()
{
pLbImage[8]->setPixmap(QPixmap::fromImage(pImage[2][2])); //加载图片
QMessageBox::about(this,"successful","***************************************");
} void MainWindow::on_btn2_clicked()
{
if(pSourceImage==NULL) return ;
QPixmap tep(":/blank.jpg");//挖空
pLbImage[8]->setPixmap(tep);
Random();
} void MainWindow::on_btn2_2_clicked()
{
if (!(QMessageBox::information(this,tr("CT Control View"),tr("Do you really want to quit the game?"),tr("Yes"),tr("No"))))
{
this->close();
}
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QLabel>
#include <QMainWindow> namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow(); void cutimage();//九宫格分割 void Random();//打乱 void moveImage();//图片移动 void mousePressEvent(QMouseEvent *event);//鼠标点击事件 int panduanwancheng();//判断完成 void huifu();//在判填充原来挖空的地方 int bushusum;//这个变量储存总步数; QTimer *timer;
int tim; private slots: //在槽函数中 只能包含函数 void on_btn_clicked();
void on_btn2_clicked();
void on_btn2_2_clicked();
void onTimerOut(); private:
Ui::MainWindow *ui;
QString strFileName;//文件名称 QImage* pSourceImage;//原图 QLabel* pLbImage[9];//九宫格label QImage pImage[3][3]; int pCompare[3][3]; //标记数组; //QImage QFrame;
}; #endif // MAINWINDOW_H

QT环境下实现UI界面的“拼图游戏”的更多相关文章

  1. qt 环境下mapx组件的鼠标跟踪

    经过两天的研究mapx组件人坐标转换还是没有转换成功,因为不管怎么变,定点转换的经纬度坐标始终与期望的值有较大的偏差.最后还是想老大请教了一下,划了半天功夫就研究出来了(不愧是老大,仰慕之情犹如滔滔江 ...

  2. 多线程环境下的UI异步操作

    转自原文 多线程环境下的UI异步操作 解决VS中,线程间不可互操作的问题,一揽子解决方案: 一.首先,定义一个类:SetControlProperty using System.Reflection; ...

  3. 在桌面Linux环境下开发图形界面程序的方案对比

    在Linux下开发GUI程序的方法有很多,比如Gnome桌面使用GTK+作为默认的图形界面库,KDE桌面使用Qt作为默认的图形界面库,wxWidgets则是另一个使用广泛的图形库,此外使用Java中的 ...

  4. 在 Windows 和 Linux(Gnome) 环境下 从命令界面打开网页的方式

    1.在 Windows 中 通过命令 可以打开网页是很简单的: start http://www.baidu.com 或 start iexplorer http://www.baidu.com 第二 ...

  5. Qt 环境下的mapx控件-------2

    今天花了一天的时间去查找mapx相关的资料,但是到最后想要的东西还是一无所获,不过还是学到了很多东西.下面以大家分享一下: mapx软件的安装:下载后安装mapx软件,成功后会在安装路径下存在acti ...

  6. Qt 环境下的activex控件编程-------1

    本人第一次接触这种activeX控件的东西,参考了网上很多的教程,终于耗时三个多小时初步理解并编写了一个小demo,现在分享给大家,希望大家少走弯路.步骤如下: 1>像平常创建项目一样创建一个d ...

  7. qt环境下Mapx组建的编程---------regoin

    #include "widget.h" #include "ui_widget.h" #include <QPushButton> #include ...

  8. qt 环境下mapx组件打包后编译产生c2248和c2512错误

    C:\Qt\Qt5.6.0\5.6\msvc2013\include\QtCore\qmetatype.h:760: error: C2248: “MapSpace::IRowCursor::IRow ...

  9. Qt 环境下MAPX组件的编程

    使用mapx打包文件可以方便的迅速开发,今天介绍一种不使用打包文件,直接使用mapx组件的编程方法. 就像之前介绍flash控件编程的方法,首先建立一个qt demo.基于那个的窗口都可以. 本den ...

随机推荐

  1. POJ 1740 A New Stone Game(博弈)题解

    题意:有n个石子堆,每一个都可以轮流做如下操作:选一个石堆,移除至少1个石子,然后可以把这堆石子随便拿几次,随便放到任意的其他石子数不为0的石子堆,也可以不拿.不能操作败. 思路:我们先来证明,如果某 ...

  2. 51nod 1266 蚂蚁

    蚂蚁这道题 就是 不管两只蚂蚁相撞  他们会朝自己的反方向走 不过可以这么想  有蚂蚁1 和 蚂蚁2   并且相向而行 如果撞了以后 蚂蚁1和蚂蚁2 就往回走   ,这里可以理解成蚂蚁1,蚂蚁2 继续 ...

  3. Unity3D学习笔记(二十四):MVC框架

    MVC:全名是Model-View-Controller View(视图层 - 顶层) Controller(控制层 - 中层) Model(数据层 - 底层) View(视图层) 说明:展现给玩家的 ...

  4. Cocos2d-x学习笔记(三)main方法

    0. UNREFERENCED_PARAMETER(X)避免编译器关于未引用参数的警告. 下边代码用于屏蔽单个函数的警告: #pragma warning( push ) #pragma warnin ...

  5. Ubuntu 14.04 编写service 服务

    有时我们需要将特定操作封装成服务,通过服务启动停止,例如nginx的启动停止,service nginx start 或者service nginx stop 下面我们将编写一个demo cd /et ...

  6. 安装 mysql8.0.13 (Ubuntu 16.04 desktop amd64)

    1.下载mysql deb https://dev.mysql.com/downloads/mysql/ #移动到/usr/local/src/目录,解压 sudo mv mysql-server_8 ...

  7. 查看 rospkg 变量

    echo $ROS_PACKAGE_PATH

  8. Python 爬虫-Robots协议

    2017-07-25 21:08:16 一.网络爬虫的规模 二.网络爬虫的限制 • 来源审查:判断User‐Agent进行限制 检查来访HTTP协议头的User‐Agent域,只响应浏览器或友好爬虫的 ...

  9. linux编译安装mysql5.1.x

    安装mysql,安装前准备 如果mysql用户不存在,那么添加mysql用户 groupadd mysql useradd -g mysql mysql mysql编译安装 make时间特别长 wge ...

  10. <yii 框架学习> yii 框架改为中文提示

    工作需要用到yii框架,但发现yii框架自带的提示都是英文的.上网找资料才发现其实可以自己陪置 . 将项目protected/config/main.php里的app配置加上language=> ...