qq_31644951 2015-10-23 14:41 采纳率: 0%
浏览 1687
已结题

求C++大神注释一下这个代码

#include
#include
#include
#include
#include
#include "dialog.h"
#include "ui_dialog.h"
using namespace std;

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{

source_picture_show=false;
ui->setupUi(this);
ui->spinBox->setMinimum(2);//设置最小值 
ui->spinBox->setValue(4); //ui是界面,spinBox是界面中的一个控件,setValue是设置值。给界面中的spinBox设置一个值为4
n=ui->spinBox->value();//获取spinBox中的值,并将其赋给n
//n=4;
N=n*n;

a   =   new   int* [n+2];
for(int   i=0;   i <n+2;   i++)
    a[i]   =   new   int[n+2];

wsubsize=100;
hsubsize=100;

bak.load(":/new/prefix1/background3.png");
Bak=bak;

setbak();
ui->verticalWidget->setGeometry(n*wsubsize,10,100,150);
//wsize=400;
//hsize=400;
//move(100,10);
//max_n=5;




//initpicture();
//update();

}

Dialog::~Dialog()
{
delete ui;
}

void Dialog::setbak()
{

Bak=Bak.scaled( n*wsubsize , n*hsubsize,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QPainter pb(&Bak);
for (int i=0;i<=n*wsubsize/127;i++ )
    for (int j=0;j<=n*hsubsize/127;j++)
        pb.drawPixmap( i*127, j*127, bak);

}

void Dialog::paintEvent(QPaintEvent *)
{
//move( (QApplication::desktop()->width() - width())/2, (QApplication::desktop()->height() - height())/2);

//()<<"start paint";
//setFixedHeight(n*hsubsize);

//setFixedWidth(n*wsubsize+100);


int i,j,p;
QPainter painter(this);
if (source_picture_show)
    painter.drawPixmap( 0, 0, pix);
else
{



    painter.drawPixmap( 0, 0, Bak);
    for (int l=1;l<=pixmaps.size();l++)
    {
        i=(l-1)%n+1;
        j=(l-1)/n+1;
        p=a[i][j]-1;
        if (p<pixmaps.size()-1)
            painter.drawPixmap( (i-1)*wsubsize, (j-1)*hsubsize, pixmaps[p]);
    }
}

}

void Dialog::mousePressEvent(QMouseEvent *m)
{

if (source_picture_show)
    return;
else
{
    int x = m->pos().x();
    int y = m->pos().y();
    hit(x/wsubsize+1 , y/hsubsize+1);
}

}

void Dialog::hit(int i,int j)
{
if (in || j>n)
return;
if (a[i-1][j]==N)
{

    a[i-1][j]=a[i][j];
    a[i][j]=N;
}
else if (a[i+1][j]==N)
{
    a[i+1][j]=a[i][j];
    a[i][j]=N;
}
else if (a[i][j-1]==N)
{
    a[i][j-1]=a[i][j];
    a[i][j]=N;
}
else if (a[i][j+1]==N)
{
    a[i][j+1]=a[i][j];
    a[i][j]=N;
}

update();
if (check())
{
    QMessageBox msgBox(this);
     msgBox.setText(tr("success!"));
     msgBox.exec();
     //initrandom();
     //refresh();
}

}

void Dialog::randomp()
{
for(int i=0; i <n+2; i++)
for(int j=0; j <n+2; j++)
{
if (i==0 || j==0 || i==n+1 || j==n+1)
a[i][j]=0;
else
a[i][j]=(j-1)*n+i;
}
int i,j;
int p;
for(int l=1;l<100*N;l++)
{
p=findN();
i=(p-1)%n+1;
j=(p-1)/n+1;

    switch (rand()%4)
    {
    case 0:
        if ((i-1)>0)
            swap(a[i][j],a[i-1][j]);
        break;
    case 1:
        if ((i+1)<n+1)
            swap(a[i][j],a[i+1][j]);
        break;
    case 2:
        if ((j-1)>0)
            swap(a[i][j],a[i][j-1]);
        break;
    case 3:
        if ((j+1)<n+1)
            swap(a[i][j],a[i][j+1]);
        break;
    }
}

}

void Dialog::initpicture()
{

QString tempfile;
//tempfile = QFileDialog::getOpenFileName(this, tr("Open"));
tempfile = QFileDialog::getOpenFileName(this,
     tr("Open Image"), "./pictures", tr("Image Files (*.png *.jpg *.bmp)"));

if (!pix.load(tempfile))
{
    ////()<<"?";
    return;
}
n=ui->spinBox->value();
N=n*n;
//()<<"ok4";
a   =   new   int* [n+2];
for(int   i=0;   i <n+2;   i++)
    a[i]   =   new   int[n+2];


wsize = pix.width();
hsize = pix.height();
if (hsize>700)
{
    pix=pix.scaled( wsize*700/hsize,  700, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    wsize = pix.width();
    hsize = pix.height();
}
else if (hsize<400)
{
    pix=pix.scaled( wsize*400/hsize,  400, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    wsize = pix.width();
    hsize = pix.height();
}

wsubsize = wsize/n;
hsubsize = hsize/n;
pix = pix.copy(0,0, n*wsubsize, n*hsubsize);

pixmaps.clear();

//()<<pixmaps.length();

for (int y = 0; y < n; y++)
{
    for (int x = 0; x < n; x++)
    {
        QPixmap pieceImage = pix.copy(x*wsubsize, y*hsubsize, wsubsize, hsubsize);
        pixmaps.append(pieceImage);
    }
}
//pixmaps[pixmaps.size()-1]=QPixmap(subsize, subsize);
//pixmaps[pixmaps.length()-1].fill(QColor(200, 200, 200));
randomp();
setbak();
setGeometry( (QApplication::desktop()->width() - (n*wsubsize+100))/2, (QApplication::desktop()->height() - n*hsubsize)/2,
             n*wsubsize+100,n*hsubsize);
//ui->pushButton  ->setGeometry(n*wsubsize+10,10,80,23);
//ui->pushButton_2->setGeometry(n*wsubsize+10,50,80,23);
ui->verticalWidget->setGeometry(n*wsubsize,10,100,150);
update();
//move(  (QApplication::desktop()->width() - width())/2, (QApplication::desktop()->height() - height())/2);
//qDebug()<<QApplication::desktop()->width()<<width();
//pix.fill( &pixmaps[pixmaps.length()-1],0,0);

}

int Dialog::findN()
{
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (a[i][j]==N)
return n*(j-1)+i;
//return 0;
}

bool Dialog::check()
{
bool good=true;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
good=good&&(a[i][j]==n*(j-1)+i);
return good;
}

void Dialog::on_pushButton_clicked()
{
initpicture();
//()<<"ok";

}

void Dialog::on_pushButton_2_clicked()
{
//setGeometry(x(),y(),2*n*subsize+100, n*subsize);
if (source_picture_show)
{
//setFixedWidth(n*wsubsize+100);
ui->pushButton_2->setText(tr("show image"));
source_picture_show=false;
update();
}
else
{
//setFixedWidth(2*n*wsubsize+100);
ui->pushButton_2->setText(tr("Back game"));
source_picture_show=true;
update();
}

}

void Dialog::on_spinBox_editingFinished()
{

}

  • 写回答

4条回答 默认 最新

  • threenewbee 2015-10-23 15:21
    关注

    没必要注释,你mfc懂不懂。对照界面看每个按钮做什么很容易搞清楚。没有界面说了你也觉得糊涂。

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名