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

#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()
{

}

c++

4个回答

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

这是界面的操作,建议看看MFC,很多代码都是MFC自动生成的,自己只是编写函数内代码

头文件中许多都是配置文件,自动生成的,你再重做一遍就知道哪些是自动生成的了

这个是QT编写的界面程序吧。提供一个学习界面逻辑的方法:在所有的函数第一行打上断点,然后运行程序,操作界面(点击按钮、下拉菜单等),程序在断点处停止,如果对界面熟悉的话,很快就能了解每一个函数的作用。至于QT库的函数,百度或者查询QT官方文档(建议)会有详细的说明。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐