zoukankan      html  css  js  c++  java
  • QT函数库总结

    /*****************Qt显示中文(主要在main函数实现)***************************/
     #include <QTextCodec>   // 编码头文件
     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("gb18030")); // 窗口里面可以接收或写中文文字
     // 这个和上面那个是等级的 QTextCodec::setCodecForLocale(QTextCodec::codecForName("gb18030"));
     QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030")); // 窗口部件里(如button)可以中文显示  
    
     int main(int argc, char *argv[]) 
     { 
     QApplication app(argc, argv); 
     QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));
     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("gb18030"));
     
     QWidget *pWidget = new QWidget; 
     QLabel label(pWidget); 
     label.setText(QObject::tr("同一个世界,同一个梦想")); 
     // 或label.setText("同一个世界,同一个梦想");
     pWidget->show(); 
     return app.exec();
     }
     
     QApplication::setQuitOnLastWindowClosed(false);
     // 后台运行,按关闭按钮关闭不了
    
    /***************************主窗口操作***********************************************/
     // 设置屏幕大小,这里固定为330*210
        this->setMinimumSize(330, 210);
        this->setMaximumSize(330, 210);
     
        this->setWindowIcon(QIcon(":/new/prefix1/QQ_PIC/Icon_1.ico")); // 主窗口设置icon, "……"为icon图片路径
        this->setWindowTitle("QQ2012-Qt版本"); // 设置窗口的名字
     
        int w = this->width(); // 获取其宽度
        int h = this->height(); // 获取其高度
     
     QWidget *parent = this->parentWidget(); // 获得自己的父对象
     if (NULL != parent)
     {
     
     }
     
     this->setWindowFlags(Qt::FramelessWindowHint); // 无边框
     
    /*****************文本编辑框TexiEdit(#include <QTextEdit>)*********************/
     ui->textEdit->setTextColor(Qt::red);    // 把显示的文字设置为红色
     ui->textEdit->setColor(QColor(0, 255, 0)); // 设置字体的另外一个方法,后面的值对应着R G B
     ui->textEdit->setText("hello, Qt!"); // 在文本编辑框设置内容
     ui->textEdit->append("abc"); // 另起一行追加“abc”
     ui->textEdit->setFontPointSize(20); // 设置字体大小,对后面的代码有效,前面的不能改变
     ui->textEdit->insertPlainText("333333");    // 开始“333333”
        QString str1 = ui->textEdit->toPlainText(); // 获得textEdit的内容
    
    /******************随机时间(#include <QTime>)**************************/
     int rand = 0;
        qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
     //以0时0分0秒到现在的秒数为种子, 调用全局的qrand()函数生成随机数
        rand = qrand()%10000; //对10000取余,保证位于10000的范围内
     
    /******************调色板的使用(#include <QPalette>)*********************/
     QPalette::Window, 通常指窗口部件的背景色
     QPalette::WindowText,通常指窗口部件的前景色
     QPalette::Base,指文本输入窗口部件的背景色
     QPalette::Text,指文本输入的窗口部件的前景色
     QPalette::Button,指按钮窗口部件的背景色
     QPalette::ButtonText,指按钮窗口的前景色
    
     QPalette p; // 定义对象
     p.setColor(QPalette::Window, Qt::blue); // Window为大写
     p.setColor(QPalette::WindowText, QColor(255, 0, 0)); // 颜色设置的另外一种方式
     // 到第一步,调色板调好了
     
     ui->lcdNumber->setPalette(p); // 选择lcdNumber使用这个调试板
     ui->lcdNumber->setAutoFillBackground(true); // 自动填充背景色
    
    /*********************按钮操作(#include <QPushButton>)****************************/
     QPushButton *buttonSet; // 对象指针
     buttonSet = new QPushButton("设置", this); // 为对象分配空间,并设置内容
        buttonSet->setGeometry(15, 185, 75, 22); // 设置button的位置大小
     
     ui->pushButton->setGeometry(QRect(0,0,100,100)); // 设置button位置大小
     ui->pushButton->setGeometry(0,0,100,100); // 设置button位置大小的另一种方法
        ui->pushButton->setText("Ensure"); // 设置按钮内容
        qDebug()<<"button text = "<<ui->pushButton->text(); // 获取button里的内容
        ui->pushButton->setIcon(QIcon("../image/on.png")); // 设置button里的icon
        ui->pushButton->setIconSize(QSize(50,50)); // 设置icon的大小
     ui->pushButton->resize(100,50); // 重新设置button的大小 
     qDebug()<<__FILE__<<__LINE__; // 打印文件名和所在行数
        int pw = ui->pushButton->width(); // 获取宽和高的两种方式
        int ph = ui->pushButton->height();
        int x = ui->pushButton->geometry().x();
        int y = ui->pushButton->geometry().y();
        ui->pushButton->move(100,100); // 移动button的位置
        ui->pushButton->hide(); // 隐藏button
     ui->pushButton->show(); // 显示button
        ui->pushButton->setEnabled(false); // button不使能
        ui->pushButton->setEnabled(true); // button使能
     
     /**********************按下button的操作***************************/
     QString msg = this->sender()->objectName(); // 按下button时,获取button的名字,假如名字为“abc”,获取信号发出者的名字
        QPushButton *button; // 定义一个指针对象
        button = (QPushButton *)this->sender(); // 确定哪个按钮被按下,并接收这个信号的发出者,最好先判断button是否为空,不为空才操作
        msg += "######"+ button->text(); // 字符串连接,button->text()为获取button里面的内容,如内容为“123” 
        ui->labelDisplay->setText(msg); // 在label显示出来,结果为:abc######123
     
     int Num = this->sender()->objectName().at(10).digitValue(); // 假如名字为toolButton1, at(10)就指向1了,这字符就转化为数字
     
    /**********************字符串处理(#include <QString>)*********************************/
     QString str = "123";
        bool OK;
        int m = str.toInt(&OK,16); // 字符串转整型, 16表示“123”里面的数字是16进制,10就为10进制,操作成功后,OK返回true
        str = QString::number(m); // 整型转换成字符串
        str.append("abc"); // 追加
        str += "hello"; // 追加的另外一种方式
        QString str2;
        str2 = QString("**%1##%2&&%3").arg(123).arg("!!!!").arg(“654”); // 组包相当于sprintf,结果为123!!!!654 
     
    /***********************标签操作(#include <QLabel>)***************************************/
     // 操作和pushButton差不多
     labelImage = new QLabel(this); // 为label分配一个空间,也可以同时添加名字,操作和pushButton一样
        labelImage->setGeometry(QRect(0, 180, 330, 30)); // 设置label的位置
        labelImage->setPixmap(QPixmap(":/new/prefix1/QQ_PIC/qq3.jpg")); // 在label上设置图片
     ui->labelImage->setScaledContents(true); // 图片自动适应label大小
     ui->labelImage->setText("hello Qt world"); // 设置label里的内容
     QFont font; // 需要头文件(#include <QFont>)
        font.setPointSize(10); // 设置大小
        ui->labelImage->setFont(font); // label设置字体大小
     ui->labelImage->setText(QString("Mike")); // 设置label内容的另外一种方式
        QString test = ui->labelImage->text();              // 获得label里面的内容
     ui->labelImage->show(); // 显示label
     this->showFullScreen(); // 全屏显示
     ui->labelImage->resize(20, 20); // 改变label的大小
     
    /************************************combobox(#include <QComboBox>)*****************************************/
     comboBoxAccount = new QComboBox(this); // 为combobox分配空间
        comboBoxAccount->setGeometry(95, 90, 115, 20); // 设置位置
        comboBoxAccount->setEditable(true); // 设置状态为可编辑的
        comboBoxAccount->insertItem(0, "1234567"); // 在第0行(通常所说的第一行)插入内容
        comboBoxAccount->insertItem(1, "231435353"); // 在第1行(通常所说的第二行)插入内容
     
        ui->comboBox->setCurrentIndex(0); // 显示第0行的内容
        QString currText = ui->comboBox->currentText(); // 获取combobox当前的内容
        int index = ui->comboBox->currentIndex(); // 换取当前的索引号
     
    /***************************************行编辑(#include <QLineEdit>)**************************************************/
     lineEditPassword = new QLineEdit(this); // 分配空间
        lineEditPassword->setGeometry(95, 115, 115, 20); // 设置位置
        lineEditPassword->setEchoMode(QLineEdit::Password); // 状态设置为密码模式
     
     ui->lineEdit->setText("aaaaaa"); // 设置内容
        ui->lineEdit->insert("bbbbbb"); // 插入内容
        QString str = ui->lineEdit->text(); // 获取内容
    
    /*************************************checkBox(#include <QCheckBox>)***********************************************/
        checkBoxRemQQ = new QCheckBox("记住密码", this); // 分配空间
        checkBoxRemQQ->setGeometry(95, 150, 70, 20); // 设置位置
     if( ui->checkBoxRemQQ->isChecked()) // 判断是否按下
            qDebug()<<"checkBoxRemQQ is Checked";
     
    /***********************动画操作(#include <QMovie>)********************************/
     movie = new QMovie;
        movie->setFileName("../image/boy.gif"); // 设置gif动画,“……”为图片路径
     // 或者QMovie *movie = new QMovie("../boom.gif");
        ui->label->setScaledContents(true); // 自适应大小
        ui->label->setMovie(movie); // 在label设置动画
        movie->start(); // 开启动画
     
    /****************数码管操作(#include <QLCDNumber>)**********************/
        ui->lcdNumber->setDigitCount(5); // 设置数码管显示5个数字
        ui->lcdNumber->setNumDigits(5);  // 等价上面
        ui->lcdNumber->display("ABC"); // 让数码管显示内容
     
    /************************进度条操作(#include <QProgressBar>)*******************************/   
     ui->progressBar->setMinimum(0); // 设置进度条的最小值
        ui->progressBar->setMaximum(200); // 设置进度条的最大值
        ui->progressBar->setValue(99); // 在进度条所占的比例,并显示出来
    
    /***********************************布局操作******************************************/
     QPushButton *button;
     this->button.setParent(this);  // 用this不易出错, 指定父对象,
     this->button.setText("click me!"); // 设置button内容
     ui->horizontalLayout->addWidget(&button); // 将部件加到水平布局管理器
     ui->verticalLayout->addWidget(&button);      // 将部件加到垂直布局管理器
     ui->gridLayout->addWidget(&button, 1, 1, 1, 1); // 将部件加到网格布局管理器
    
    /************************************定时器操作(#include <timer>)***********************************************/
     /********************* 在构造函数里定义*********************************/
     QTimer *timer = new QTimer(); // 括号里加不加this都可以
     // 定时器start之后每隔1s发送一个timeout()信号,每隔1S会执行一次mySlot()槽函数
        connect(timer, SIGNAL(timeout()), this, SLOT(mySlot() ));
        timer->start(1000); // 以ms为单位,启动定时器,此次即为1秒
     
     /***************************槽函数mySlot()的处理********************************/
     static count = 11; // 静态变量,count的值每次改变都记录下来
     count--; // 10,9,8,7……倒计
        ui->lcdNumber->display(count); // 在lcd上显示
        if (0 == count)
        {
            timer->stop(); // 关闭定时器
            ui->lcdNumber->hide(); // 隐藏LCD
            ui->label->show(); // 显示label
            this->showFullScreen(); // 全屏显示
            ui->label->movie()->start();// 动画启动
        }
    
    /**********************信号和槽的使用(两个ui切换)**********************************/
     // 两个ui,分别为mainWidgt, secondaryWidgt,其中mainWidgt为主界面,界面对应类的名字和ui名字相同
     
     // secondaryWidgt类中声明信号
     signals:
     void goBack();
     
     // 在mainWidgt类,定义一个secondaryWidgt的指针对象 *w, 接着在其构造函数操作
     this->w = new  secondaryWidgt; // 分配空间
     // 信号和槽连接,当接收到secondaryWidgt发出goBack()信号,就显示mainWidgt窗口
        this->connect(w, SIGNAL(goBack()), this, SLOT(show())); 
     
     // mainWidgt窗口按下按钮时,隐藏自己(this->hide()),显示secondaryWidgt窗口(w->show())
     
     // secondaryWidgt窗口按下按钮时,隐藏自己(this->hide()),发送goBack()信号(emit this->goBack())
     
    /**********************************常用对话框的使用************************************************/
     // 通过按钮按下发出信号,在对应的槽函数处理
     
     /***********************文件操作(#include <QFileDialog>)*******************************/
       QString str;
       str = QFileDialog::getOpenFileName(this, "MyOpenFile", "../",
                "debug (*.o *.cpp);;txtFile(*.txt);;All File(*.*)");
     // "MyOpenFile"为打开新窗口的名字, "../"为文件打开的上级路径
     // "debug (*.o *.cpp);;txtFile(*.txt);;All File(*.*)"为文件打开的格式
     // str接收的是打开文件的路径
    
     /********************************颜色设定(#include <QColorDialog>)********************************************/
        QColor mycolor;
        mycolor = QColorDialog::getColor(); // 获取颜色
     if ( false == corlor.isValid) // 判断颜色是否有效, 不加这个判断,颜色变回默认值
     return;
        QPalette palette; // 画笔
     palette.setColor(QPalette::Base, mycolor); // 设置颜色
     
     // 设置颜色的另外一种方式
        QBrush brush(mycolor); // 使用颜色
        palette.setBrush(QPalette::Active, QPalette::Base, brush); // 使用笔刷
        ui->colerLineEdit->setPalette(palette); // 用画笔画
    
     /************************字体设置(#include <QFontDialog>)**********************************************/
        bool ok = false;
        QFont font = QFontDialog::getFont(&ok); // 得到字体
        if(true == ok) // 如果字体有效,防止字体恢复成默认值
            ui->fontLineEdit->setFont(font); // 设置字体
    
    
     /************************常用对话框(#include <QMessageBox>)*****************************************************/
        /**********************询问**********************************/
     int button = QMessageBox::question(this,"MyQuestion", "Are you OK?",
                     QMessageBox::No | QMessageBox::Yes| QMessageBox::Cancel);
        switch(button){
        case QMessageBox::No:
            ui->label->setText("NO");
            break;
        case QMessageBox::Yes:
            ui->label->setText("Yes");
            break;
        case QMessageBox::Cancel:
            ui->label->setText("Cancel");
            break;
     /*********************信息对话框*****************************/
        QMessageBox::information(this, "my information", "Game over");
    
     /*********************警告对话框******************************/
        QMessageBox::warning(this, "my Warning", "Your system have a serious problem !!
    It Will turn off in 3 min.");
     
    /************************************************常用事件处理**********************************************/
     
     /**************************************鼠标点击事件(#include <QMouseEvent>)*************************************/
        void mouseMoveEvent ( QMouseEvent * e );
        void mousePressEvent ( QMouseEvent * e );
        void mouseReleaseEvent ( QMouseEvent * e );
        void mouseDoubleClickEvent( QMouseEvent * e );
     
     // 鼠标移动事件, 默认是按下移动才启动事件
     void Widget::mouseMoveEvent(QMouseEvent *e) // 事件函数名字必须这样,不能改变,因为这个是虚函数  
        ui->label->setText("("+QString::number(e->x())+","+QString::number(e->y())+")"); // 显示其坐标
     // 要想不需要按下移动,也能启动事件,在构造函数里加下面的函数
     this->setMouseTracking(true);
    
     // 鼠标点击事件
     void Widget::mousePressEvent(QMouseEvent *e)
        QString s="";
        if(e->button()==Qt::LeftButton) // 左击
        {
            s  = "LeftButton Pressed
    ";
        }    
        if(e->button()==Qt::RightButton) // 右击
        {
            s  = "RightButton Pressed
    ";
        }
        if(e->button()==Qt::MidButton) // 中间滑轮点击
        {
            s = "MidButton Pressed
    ";
        }
       
     // 鼠标释放事件,操作和点击一样
     void Widget::mouseReleaseEvent(QMouseEvent *e)
    
     // 滑轮滚动事件
     void Widget::wheelEvent(QWheelEvent *e)
        QString s;
        if(e->orientation()== Qt::Vertical) // 判断滚轮是否垂直滚动
        {
            if(e->delta()>0) // 大于0为滚轮向上
                s += " go (head)";
            else // 小于0即为向下
                s += " go (back)";
        }
     
     /*******************************键盘事件(#include <QKeyEvent>)*************************************/
     // 修饰键操作
     void MykeyEvent::keyPressEvent(QKeyEvent *e)
        QString s = "";
        switch(e->modifiers()){ // 修饰键选择,可以达到Ctrl+A这样的效果
        case Qt::ControlModifier: // Ctrl键
            s = tr("Ctrl+");
            break;
        case Qt::AltModifier: // Alt键
            s = tr("Alt+");
            break;
        }
    
        switch(e->key()) // 普通按键
        {
        case Qt::Key_Left: // 方向键的向左
            s += tr("Left_Key Press");
            break;
        case Qt::Key_Right: // 方向键的向右
            s += tr("Rigth_Key Press");
            break;
        case Qt::Key_Up: // 方向键的向上
            s += tr("Up_Key Press");
            break;
        case Qt::Key_Down: // 方向键的向下
            s += tr("Down_Key Press");
            break;
        case Qt::Key_Z: // Z键
            s += tr("Z_Key Press");
            break;
        }
        ui->label->setText(tr(s.toAscii()));
     // 转化为ASCII码,再显示
    
     // 主窗口大小发生变化时被调用
     void MykeyEvent::resizeEvent(QResizeEvent *e)
        qDebug()<<"new size = "<<e->size(); // 变化后的窗口大小
        qDebug()<<"old size = "<<e->oldSize(); // 变化前的窗口大小
     
     /*****************************绘图事件(#include <QPaintEvent>)********************************/
     // 利用QPainter绘制各种图形,在Void paintEvent(QPaintEvent *event)中实现
     // 当窗口被绘制时被调用,也可以通过update()产生paintEvent(……)事件
     // 绘制的内容以背景的形式出现在窗口中,QPainter 一般要放在paintEvent()里,否则会初始化失败
     
     event->rect()  --- 可得到需要重新绘制的区域
     QPainter painter(this); --- 创建对象
     painter.setPen(); --- 设置画笔
     painter.setBrush() --- 设置画刷
     patiner.drawXXX(); --- 画
     drawPoint()         画点
        drawLine() 画直线
     drawRect() 画矩形
        drawEllipse() 画椭圆
        drawPicture() 画图片
        drawImage() 绘图片
        drawPixmap() 绘图片
        drawText() 绘文本
     
     // 绘图事件
     void PaintEvent::paintEvent(QPaintEvent * event)
     qDebug()<< event->rect(); // 运行结果为:QRect(0,0 400x300)
     QPainter p(this); // 创建画笔对象,需要头文件#include <QPainter>
     QPen pen; // #include <QPen>,创建一支画笔,下面是配置画笔
        pen.setColor(Qt::red); // 画笔的颜色
        pen.setStyle(Qt::DashDotDotLine); // 画笔画线的类型,虚线
        pen.setWidth(3); // 画笔画线的宽度
        p.setPen(pen); // 把画笔交给画家画
        p.drawLine(10,10,260,10); // 以(10, 10)为起点,以(260, 10)为终点,画直线
     p.drawRect(10,20,200,150);  // 以(10, 20)为起点,宽为200, 高为150, 画矩形
     
     QBrush brush; // 创建画刷,在上面刷东西,需要头文件#include <QBrush>
        brush.setColor(Qt::magenta); // 设置颜色,粉红色
        brush.setStyle(Qt::DiagCrossPattern); // 画刷的风格,网格风格
        p.setBrush(brush); // 装画刷交给画家
        p.drawRect(10,20,200,150); // 以(10, 20)为起点,宽为200, 高为150的矩形里面刷东西
     p.setPen(QPen()); // 画笔恢复默认值
     p.setBrush(QBrush()); // 画刷恢复默认值
     p.drawEllipse(230,30,150,200);  // 以(230, 30)为起点,宽为150, 高为200的矩形画内切圆
     p.setBrush(QBrush(QPixmap("../image/test.jpg"))); // 画刷里面的内容是图片
     
     p.drawPixmap(0, 0, this->width(), this->height(), QPixmap("../image/on.png")); // 以窗口大小画图
     // 画图事件调用update()会使整个窗口的图片刷新,不要再绘图事件调用update(),播放动画,设置button的图片
     
     // 设置字体
     QFont font; // 需要头文件#include <QFont>
        font.setFamily("幼圆"); // 设置字体类型
        font.setPointSize(30); // 设置字体大小
        font.setBold(true); // 是否加粗
        font.setUnderline(true); // 是否加下划线
     font.setStrikeOut(true); // 设置横穿文字中间的线
        p.setFont(font); // 把字体交给画家
        p.setPen(Qt::red); // 设置画笔颜色
        p.drawText(20, 190, "你好,Qt!"); // 在(20, 190)为起点,写字
     
     // 这和绘图事件无关,截图
     QPixmap image = QPixmap::grabWidget(this, 0, 0, this->width(), this->height());
     image.save("1.png");    // 保存图片
     
     
    /********************************播放音乐(#include <QSound>)*************************************/
     // 一种方法
     QSound::play("mysounds/bells.wav");
     // 另一种方法
     QSound bells("mysounds/bells.wav");
      bells.play();
     // 也就是说
     QSound *bells = new QSound("mysounds/bells.wav");
     bells->play();
     bells->setLoops(-1); // 无限循环
     
    /***************************枚举的使用************************************/
     // 首先在一个类中定义一个枚举,如下
     class A
     {
     public:
     enum B{Empty, Black, White};
     };
     // 在类中的成员函数了,可以直接操作,如:
     B c = Empty;
     // 假如在别的类中,创建类A的对象指针a, 如 a = new A(this);
     // 这里有两种使用方法
     A::B test = a->Black;
     A::B test = A::Black;
     
     
    /********************************文件夹操作(#include <QDir>)************************************/
     QDir tempDir;   // 文件夹变量
        QString str = QCoreApplication::applicationDirPath(); //获取当前应用程序路径(#include <QCoreApplication>)
        str += "/outputImage";
        if (false == tempDir.exists(str))   // 当这个文件夹不存在时,才创建
        {
            bool ok = tempDir.mkdir(str);
            // 循环创建,直到成功创建
            while (false == ok)
            {
                ok = tempDir.mkdir(str);
            }
        }
     
    /******************************文件操作(#include <QFile>)********************************/
     // 写操作
     
     QFile file("1.txt"); // 创建文件对象
     
     //一般不要IO_ReadWrite,很容易出现赃数据
        //如果要在文件的后面添加内容要IO_WriteOnly|IO_Append
        //如果要清空原来的内容,只要IO_WriteOnly
        if(file.open(QIODevice::WriteOnly)) // 只写方式打开文件
        {
            QTextStream out(&file);
            out.setCodec(QTextCodec::codecForName("gb18030")); // 写之前设置编码
     out << i << "
    "<<"image"<<"
    " << Label[i]->x() << "
    "<<Label[i]−>y()<<"
    "
                            << Label[i]->width() << "
    "<<Label[i]−>height()<<"
    "<< *this->picPath[i]
                            << "$$"<< this->cardBackgound << "
    ";
     file.close();
     }
     
     // 读操作
     QFile file(fileTempPath);
        QString dataLine;
     // 只读方式打开
        if (file.open(QIODevice::ReadOnly))
        {
            QTextStream readData(&file);
            readData.setCodec(QTextCodec::codecForName("gb18030")); // 读之前也设置编码
    
            while (false == readData.atEnd())    // 是否是到文件的结尾
            {
                dataLine = readData.readLine();  // 先读一行
                if (dataLine.size() != 1)
                {
                    this->flag = dataLine.section("
    ",0,0).toInt();//坐标下标this−>data[flag][0]=dataLine.section("
    ", 2, 2).toInt();  // x
                    this->data[flag][1] = dataLine.section("
    ",3,3).toInt();//ythis−>data[flag][2]=dataLine.section("
    ", 4, 4).toInt();  // w
                    this->data[flag][3] = dataLine.section("$$", 5, 5).toInt();  // h
                }
     }
     
     file.close();
     }
     
    // 宏定义
     #define print qDebug()<<__FILE__<<__LINE__<<":"
    
     
    /************************memcpy的使用**********************************/
     // 函数的原型:void  *memcpy(void *dest,   const void *src,   size_t count); 
     int directioncpy[8][2] = {{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1}};
     int direction[8][2];
     // 函数的功能是将directioncpy的内容拷贝到direction
     // sizeof(int)*16,注意这个长度
        memcpy(direction,directioncpy,sizeof(int)*16);
     
     int chessHistroy[64][8][8];
     int chess[8][8]; 
     memcpy(chessHistroy[histroy],chess,sizeof(int)*64);
    
    /*******************设置按钮背景色**************************/
    setStyleSheet("background-color: rgb(255, 255, 0)");
    
    /************重写鼠标事件******************/
    QPoint dragPosition;     // 在类中增加这一个成员
    
      void Widget::mouseMoveEvent(QMouseEvent *e)
    {
        if(e->buttons() & Qt::LeftButton){ //当满足鼠标左键被点击时
            move(e->globalPos()-this->dragPosition); //移动窗口
        }
    }
    void Widget::mousePressEvent(QMouseEvent *e)
    {
        if(e->button() == Qt::LeftButton)  //点击左边鼠标
        {
             //globalPos()获取根窗口的相对路径,frameGeometry().topLeft()获取主窗口左上角的位置,
            // frameGeometry().topLeft()相当于起点坐标: this->x(), this->y();
            this->dragPosition=e->globalPos()-this->frameGeometry().topLeft();    // 和下面操作等价的
            // this->dragPosition.setX(e->x());
            // this->dragPosition.setY(e->y());
    
            //qDebug() << this->frameGeometry().topLeft();  // 等价下面
           //qDebug() << this->x() << "," << this->y();
      
        //qDebug() << e->globalPos();    // 等价下面
            //qDebug() << e->x()+x() << ","  << e->y()+y();
        }
        else if(e->button() == Qt::RightButton){
            this->close();
        }
    }
    
    /*************************QStringList*********************************/
        QStringList list("hello");
        qDebug()<<list; // ("hello")
    
        //追加
        list.append("word");
        qDebug()<<list; //("hello", "word")
    
        list<<"qt"<<"listen";
        qDebug()<<list; // ("hello", "word", "qt", "listen")
    
        //合并
        QString str;
        str = list.join(",");
        qDebug()<<str;  // "hello,word,qt,listen"
    
        //拆分
        str = "hello,word,,qt";
        QStringList list1;
        list1 = str.split(",");
        qDebug()<<list1; // ("hello", "word", "", "qt")
    
        //去掉空格
        list1 = str.split(",",QString::SkipEmptyParts);
        qDebug()<<list1; // ("hello", "word", "qt")
    
        //索引
        int num;
        num = str.indexOf(QRegExp("o"));
        qDebug()<<"num = "<<num; // num =  4
    
        num = list1.indexOf(QRegExp("hello"));
        qDebug()<<"QStringList list1 = "<<num; // QStringList list1 = 0
    
        num = str.lastIndexOf(QRegExp("o"));
        qDebug()<<"last num = "<<num; // last num =  7
    
        num = list1.lastIndexOf(QRegExp("hello"));
        qDebug()<<"last QStringList = "<<num; //last QStringList =  0
    
        //替换 ("hello", "word", "qt", "listen")
        list.replaceInStrings("o","eee");
        qDebug()<<"replace list = "<<list;
        // ("helleee", "weeerd", "qt", "listen")
    
        list.replace(2,"replace");
        qDebug()<<"replace only one list = "<<list;
        // ("helleee", "weeerd", "replace", "listen")
    
        //过滤 ("hello", "word", "qt")
        QStringList result;
        result = list1.filter("o");
        qDebug()<<"filter result = "<<result; // ("hello", "word")
    
        //查找
        if(list1.contains("hello")) // true
            qDebug()<<"true";
    

      

    /*****************Qt显示中文(主要在main函数实现)***************************/
     #include <QTextCodec>   // 编码头文件
     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("gb18030")); // 窗口里面可以接收或写中文文字
     // 这个和上面那个是等级的 QTextCodec::setCodecForLocale(QTextCodec::codecForName("gb18030"));
     QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030")); // 窗口部件里(如button)可以中文显示  

     int main(int argc, char *argv[]) 
     { 
     QApplication app(argc, argv); 
     QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));
     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("gb18030"));
     
     QWidget *pWidget = new QWidget; 
     QLabel label(pWidget); 
     label.setText(QObject::tr("同一个世界,同一个梦想")); 
     // 或label.setText("同一个世界,同一个梦想");
     pWidget->show(); 
     return app.exec();
     }
     
     QApplication::setQuitOnLastWindowClosed(false);
     // 后台运行,按关闭按钮关闭不了

    /***************************主窗口操作***********************************************/
     // 设置屏幕大小,这里固定为330*210
        this->setMinimumSize(330, 210);
        this->setMaximumSize(330, 210);
     
        this->setWindowIcon(QIcon(":/new/prefix1/QQ_PIC/Icon_1.ico")); // 主窗口设置icon, "……"为icon图片路径
        this->setWindowTitle("QQ2012-Qt版本"); // 设置窗口的名字
     
        int w = this->width(); // 获取其宽度
        int h = this->height(); // 获取其高度
     
     QWidget *parent = this->parentWidget(); // 获得自己的父对象
     if (NULL != parent)
     {
     
     }
     
     this->setWindowFlags(Qt::FramelessWindowHint); // 无边框
     
    /*****************文本编辑框TexiEdit(#include <QTextEdit>)*********************/
     ui->textEdit->setTextColor(Qt::red);    // 把显示的文字设置为红色
     ui->textEdit->setColor(QColor(0, 255, 0)); // 设置字体的另外一个方法,后面的值对应着R G B
     ui->textEdit->setText("hello, Qt!"); // 在文本编辑框设置内容
     ui->textEdit->append("abc"); // 另起一行追加“abc”
     ui->textEdit->setFontPointSize(20); // 设置字体大小,对后面的代码有效,前面的不能改变
     ui->textEdit->insertPlainText("333333");    // 开始“333333”
        QString str1 = ui->textEdit->toPlainText(); // 获得textEdit的内容

    /******************随机时间(#include <QTime>)**************************/
     int rand = 0;
        qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
     //以0时0分0秒到现在的秒数为种子, 调用全局的qrand()函数生成随机数
        rand = qrand()%10000; //对10000取余,保证位于10000的范围内
     
    /******************调色板的使用(#include <QPalette>)*********************/
     QPalette::Window, 通常指窗口部件的背景色
     QPalette::WindowText,通常指窗口部件的前景色
     QPalette::Base,指文本输入窗口部件的背景色
     QPalette::Text,指文本输入的窗口部件的前景色
     QPalette::Button,指按钮窗口部件的背景色
     QPalette::ButtonText,指按钮窗口的前景色

     QPalette p; // 定义对象
     p.setColor(QPalette::Window, Qt::blue); // Window为大写
     p.setColor(QPalette::WindowText, QColor(255, 0, 0)); // 颜色设置的另外一种方式
     // 到第一步,调色板调好了
     
     ui->lcdNumber->setPalette(p); // 选择lcdNumber使用这个调试板
     ui->lcdNumber->setAutoFillBackground(true); // 自动填充背景色

    /*********************按钮操作(#include <QPushButton>)****************************/
     QPushButton *buttonSet; // 对象指针
     buttonSet = new QPushButton("设置", this); // 为对象分配空间,并设置内容
        buttonSet->setGeometry(15, 185, 75, 22); // 设置button的位置大小
     
     ui->pushButton->setGeometry(QRect(0,0,100,100)); // 设置button位置大小
     ui->pushButton->setGeometry(0,0,100,100); // 设置button位置大小的另一种方法
        ui->pushButton->setText("Ensure"); // 设置按钮内容
        qDebug()<<"button text = "<<ui->pushButton->text(); // 获取button里的内容
        ui->pushButton->setIcon(QIcon("../image/on.png")); // 设置button里的icon
        ui->pushButton->setIconSize(QSize(50,50)); // 设置icon的大小
     ui->pushButton->resize(100,50); // 重新设置button的大小 
     qDebug()<<__FILE__<<__LINE__; // 打印文件名和所在行数
        int pw = ui->pushButton->width(); // 获取宽和高的两种方式
        int ph = ui->pushButton->height();
        int x = ui->pushButton->geometry().x();
        int y = ui->pushButton->geometry().y();
        ui->pushButton->move(100,100); // 移动button的位置
        ui->pushButton->hide(); // 隐藏button
     ui->pushButton->show(); // 显示button
        ui->pushButton->setEnabled(false); // button不使能
        ui->pushButton->setEnabled(true); // button使能
     
     /**********************按下button的操作***************************/
     QString msg = this->sender()->objectName(); // 按下button时,获取button的名字,假如名字为“abc”,获取信号发出者的名字
        QPushButton *button; // 定义一个指针对象
        button = (QPushButton *)this->sender(); // 确定哪个按钮被按下,并接收这个信号的发出者,最好先判断button是否为空,不为空才操作
        msg += "######"+ button->text(); // 字符串连接,button->text()为获取button里面的内容,如内容为“123” 
        ui->labelDisplay->setText(msg); // 在label显示出来,结果为:abc######123
     
     int Num = this->sender()->objectName().at(10).digitValue(); // 假如名字为toolButton1, at(10)就指向1了,这字符就转化为数字
     
    /**********************字符串处理(#include <QString>)*********************************/
     QString str = "123";
        bool OK;
        int m = str.toInt(&OK,16); // 字符串转整型, 16表示“123”里面的数字是16进制,10就为10进制,操作成功后,OK返回true
        str = QString::number(m); // 整型转换成字符串
        str.append("abc"); // 追加
        str += "hello"; // 追加的另外一种方式
        QString str2;
        str2 = QString("**%1##%2&&%3").arg(123).arg("!!!!").arg(“654”); // 组包相当于sprintf,结果为123!!!!654 
     
    /***********************标签操作(#include <QLabel>)***************************************/
     // 操作和pushButton差不多
     labelImage = new QLabel(this); // 为label分配一个空间,也可以同时添加名字,操作和pushButton一样
        labelImage->setGeometry(QRect(0, 180, 330, 30)); // 设置label的位置
        labelImage->setPixmap(QPixmap(":/new/prefix1/QQ_PIC/qq3.jpg")); // 在label上设置图片
     ui->labelImage->setScaledContents(true); // 图片自动适应label大小
     ui->labelImage->setText("hello Qt world"); // 设置label里的内容
     QFont font; // 需要头文件(#include <QFont>)
        font.setPointSize(10); // 设置大小
        ui->labelImage->setFont(font); // label设置字体大小
     ui->labelImage->setText(QString("Mike")); // 设置label内容的另外一种方式
        QString test = ui->labelImage->text();              // 获得label里面的内容
     ui->labelImage->show(); // 显示label
     this->showFullScreen(); // 全屏显示
     ui->labelImage->resize(20, 20); // 改变label的大小
     
    /************************************combobox(#include <QComboBox>)*****************************************/
     comboBoxAccount = new QComboBox(this); // 为combobox分配空间
        comboBoxAccount->setGeometry(95, 90, 115, 20); // 设置位置
        comboBoxAccount->setEditable(true); // 设置状态为可编辑的
        comboBoxAccount->insertItem(0, "1234567"); // 在第0行(通常所说的第一行)插入内容
        comboBoxAccount->insertItem(1, "231435353"); // 在第1行(通常所说的第二行)插入内容
     
        ui->comboBox->setCurrentIndex(0); // 显示第0行的内容
        QString currText = ui->comboBox->currentText(); // 获取combobox当前的内容
        int index = ui->comboBox->currentIndex(); // 换取当前的索引号
     
    /***************************************行编辑(#include <QLineEdit>)**************************************************/
     lineEditPassword = new QLineEdit(this); // 分配空间
        lineEditPassword->setGeometry(95, 115, 115, 20); // 设置位置
        lineEditPassword->setEchoMode(QLineEdit::Password); // 状态设置为密码模式
     
     ui->lineEdit->setText("aaaaaa"); // 设置内容
        ui->lineEdit->insert("bbbbbb"); // 插入内容
        QString str = ui->lineEdit->text(); // 获取内容

    /*************************************checkBox(#include <QCheckBox>)***********************************************/
        checkBoxRemQQ = new QCheckBox("记住密码", this); // 分配空间
        checkBoxRemQQ->setGeometry(95, 150, 70, 20); // 设置位置
     if( ui->checkBoxRemQQ->isChecked()) // 判断是否按下
            qDebug()<<"checkBoxRemQQ is Checked";
     
    /***********************动画操作(#include <QMovie>)********************************/
     movie = new QMovie;
        movie->setFileName("../image/boy.gif"); // 设置gif动画,“……”为图片路径
     // 或者QMovie *movie = new QMovie("../boom.gif");
        ui->label->setScaledContents(true); // 自适应大小
        ui->label->setMovie(movie); // 在label设置动画
        movie->start(); // 开启动画
     
    /****************数码管操作(#include <QLCDNumber>)**********************/
        ui->lcdNumber->setDigitCount(5); // 设置数码管显示5个数字
        ui->lcdNumber->setNumDigits(5);  // 等价上面
        ui->lcdNumber->display("ABC"); // 让数码管显示内容
     
    /************************进度条操作(#include <QProgressBar>)*******************************/   
     ui->progressBar->setMinimum(0); // 设置进度条的最小值
        ui->progressBar->setMaximum(200); // 设置进度条的最大值
        ui->progressBar->setValue(99); // 在进度条所占的比例,并显示出来

    /***********************************布局操作******************************************/
     QPushButton *button;
     this->button.setParent(this);  // 用this不易出错, 指定父对象,
     this->button.setText("click me!"); // 设置button内容
     ui->horizontalLayout->addWidget(&button); // 将部件加到水平布局管理器
     ui->verticalLayout->addWidget(&button);      // 将部件加到垂直布局管理器
     ui->gridLayout->addWidget(&button, 1, 1, 1, 1); // 将部件加到网格布局管理器

    /************************************定时器操作(#include <timer>)***********************************************/
     /********************* 在构造函数里定义*********************************/
     QTimer *timer = new QTimer(); // 括号里加不加this都可以
     // 定时器start之后每隔1s发送一个timeout()信号,每隔1S会执行一次mySlot()槽函数
        connect(timer, SIGNAL(timeout()), this, SLOT(mySlot() ));
        timer->start(1000); // 以ms为单位,启动定时器,此次即为1秒
     
     /***************************槽函数mySlot()的处理********************************/
     static count = 11; // 静态变量,count的值每次改变都记录下来
     count--; // 10,9,8,7……倒计
        ui->lcdNumber->display(count); // 在lcd上显示
        if (0 == count)
        {
            timer->stop(); // 关闭定时器
            ui->lcdNumber->hide(); // 隐藏LCD
            ui->label->show(); // 显示label
            this->showFullScreen(); // 全屏显示
            ui->label->movie()->start();// 动画启动
        }

    /**********************信号和槽的使用(两个ui切换)**********************************/
     // 两个ui,分别为mainWidgt, secondaryWidgt,其中mainWidgt为主界面,界面对应类的名字和ui名字相同
     
     // secondaryWidgt类中声明信号
     signals:
     void goBack();
     
     // 在mainWidgt类,定义一个secondaryWidgt的指针对象 *w, 接着在其构造函数操作
     this->w = new  secondaryWidgt; // 分配空间
     // 信号和槽连接,当接收到secondaryWidgt发出goBack()信号,就显示mainWidgt窗口
        this->connect(w, SIGNAL(goBack()), this, SLOT(show())); 
     
     // mainWidgt窗口按下按钮时,隐藏自己(this->hide()),显示secondaryWidgt窗口(w->show())
     
     // secondaryWidgt窗口按下按钮时,隐藏自己(this->hide()),发送goBack()信号(emit this->goBack())
     
    /**********************************常用对话框的使用************************************************/
     // 通过按钮按下发出信号,在对应的槽函数处理
     
     /***********************文件操作(#include <QFileDialog>)*******************************/
       QString str;
       str = QFileDialog::getOpenFileName(this, "MyOpenFile", "../",
                "debug (*.o *.cpp);;txtFile(*.txt);;All File(*.*)");
     // "MyOpenFile"为打开新窗口的名字, "../"为文件打开的上级路径
     // "debug (*.o *.cpp);;txtFile(*.txt);;All File(*.*)"为文件打开的格式
     // str接收的是打开文件的路径

     /********************************颜色设定(#include <QColorDialog>)********************************************/
        QColor mycolor;
        mycolor = QColorDialog::getColor(); // 获取颜色
     if ( false == corlor.isValid) // 判断颜色是否有效, 不加这个判断,颜色变回默认值
     return;
        QPalette palette; // 画笔
     palette.setColor(QPalette::Base, mycolor); // 设置颜色
     
     // 设置颜色的另外一种方式
        QBrush brush(mycolor); // 使用颜色
        palette.setBrush(QPalette::Active, QPalette::Base, brush); // 使用笔刷
        ui->colerLineEdit->setPalette(palette); // 用画笔画

     /************************字体设置(#include <QFontDialog>)**********************************************/
        bool ok = false;
        QFont font = QFontDialog::getFont(&ok); // 得到字体
        if(true == ok) // 如果字体有效,防止字体恢复成默认值
            ui->fontLineEdit->setFont(font); // 设置字体


     /************************常用对话框(#include <QMessageBox>)*****************************************************/
        /**********************询问**********************************/
     int button = QMessageBox::question(this,"MyQuestion", "Are you OK?",
                     QMessageBox::No | QMessageBox::Yes| QMessageBox::Cancel);
        switch(button){
        case QMessageBox::No:
            ui->label->setText("NO");
            break;
        case QMessageBox::Yes:
            ui->label->setText("Yes");
            break;
        case QMessageBox::Cancel:
            ui->label->setText("Cancel");
            break;
     /*********************信息对话框*****************************/
        QMessageBox::information(this, "my information", "Game over");

     /*********************警告对话框******************************/
        QMessageBox::warning(this, "my Warning", "Your system have a serious problem !! It Will turn off in 3 min.");
     
    /************************************************常用事件处理**********************************************/
     
     /**************************************鼠标点击事件(#include <QMouseEvent>)*************************************/
        void mouseMoveEvent ( QMouseEvent * e );
        void mousePressEvent ( QMouseEvent * e );
        void mouseReleaseEvent ( QMouseEvent * e );
        void mouseDoubleClickEvent( QMouseEvent * e );
     
     // 鼠标移动事件, 默认是按下移动才启动事件
     void Widget::mouseMoveEvent(QMouseEvent *e) // 事件函数名字必须这样,不能改变,因为这个是虚函数  
        ui->label->setText("("+QString::number(e->x())+","+QString::number(e->y())+")"); // 显示其坐标
     // 要想不需要按下移动,也能启动事件,在构造函数里加下面的函数
     this->setMouseTracking(true);

     // 鼠标点击事件
     void Widget::mousePressEvent(QMouseEvent *e)
        QString s="";
        if(e->button()==Qt::LeftButton) // 左击
        {
            s  = "LeftButton Pressed ";
        }    
        if(e->button()==Qt::RightButton) // 右击
        {
            s  = "RightButton Pressed ";
        }
        if(e->button()==Qt::MidButton) // 中间滑轮点击
        {
            s = "MidButton Pressed ";
        }
       
     // 鼠标释放事件,操作和点击一样
     void Widget::mouseReleaseEvent(QMouseEvent *e)

     // 滑轮滚动事件
     void Widget::wheelEvent(QWheelEvent *e)
        QString s;
        if(e->orientation()== Qt::Vertical) // 判断滚轮是否垂直滚动
        {
            if(e->delta()>0) // 大于0为滚轮向上
                s += " go (head)";
            else // 小于0即为向下
                s += " go (back)";
        }
     
     /*******************************键盘事件(#include <QKeyEvent>)*************************************/
     // 修饰键操作
     void MykeyEvent::keyPressEvent(QKeyEvent *e)
        QString s = "";
        switch(e->modifiers()){ // 修饰键选择,可以达到Ctrl+A这样的效果
        case Qt::ControlModifier: // Ctrl键
            s = tr("Ctrl+");
            break;
        case Qt::AltModifier: // Alt键
            s = tr("Alt+");
            break;
        }

        switch(e->key()) // 普通按键
        {
        case Qt::Key_Left: // 方向键的向左
            s += tr("Left_Key Press");
            break;
        case Qt::Key_Right: // 方向键的向右
            s += tr("Rigth_Key Press");
            break;
        case Qt::Key_Up: // 方向键的向上
            s += tr("Up_Key Press");
            break;
        case Qt::Key_Down: // 方向键的向下
            s += tr("Down_Key Press");
            break;
        case Qt::Key_Z: // Z键
            s += tr("Z_Key Press");
            break;
        }
        ui->label->setText(tr(s.toAscii()));
     // 转化为ASCII码,再显示

     // 主窗口大小发生变化时被调用
     void MykeyEvent::resizeEvent(QResizeEvent *e)
        qDebug()<<"new size = "<<e->size(); // 变化后的窗口大小
        qDebug()<<"old size = "<<e->oldSize(); // 变化前的窗口大小
     
     /*****************************绘图事件(#include <QPaintEvent>)********************************/
     // 利用QPainter绘制各种图形,在Void paintEvent(QPaintEvent *event)中实现
     // 当窗口被绘制时被调用,也可以通过update()产生paintEvent(……)事件
     // 绘制的内容以背景的形式出现在窗口中,QPainter 一般要放在paintEvent()里,否则会初始化失败
     
     event->rect()  --- 可得到需要重新绘制的区域
     QPainter painter(this); --- 创建对象
     painter.setPen(); --- 设置画笔
     painter.setBrush() --- 设置画刷
     patiner.drawXXX(); --- 画
     drawPoint()         画点
        drawLine() 画直线
     drawRect() 画矩形
        drawEllipse() 画椭圆
        drawPicture() 画图片
        drawImage() 绘图片
        drawPixmap() 绘图片
        drawText() 绘文本
     
     // 绘图事件
     void PaintEvent::paintEvent(QPaintEvent * event)
     qDebug()<< event->rect(); // 运行结果为:QRect(0,0 400x300)
     QPainter p(this); // 创建画笔对象,需要头文件#include <QPainter>
     QPen pen; // #include <QPen>,创建一支画笔,下面是配置画笔
        pen.setColor(Qt::red); // 画笔的颜色
        pen.setStyle(Qt::DashDotDotLine); // 画笔画线的类型,虚线
        pen.setWidth(3); // 画笔画线的宽度
        p.setPen(pen); // 把画笔交给画家画
        p.drawLine(10,10,260,10); // 以(10, 10)为起点,以(260, 10)为终点,画直线
     p.drawRect(10,20,200,150);  // 以(10, 20)为起点,宽为200, 高为150, 画矩形
     
     QBrush brush; // 创建画刷,在上面刷东西,需要头文件#include <QBrush>
        brush.setColor(Qt::magenta); // 设置颜色,粉红色
        brush.setStyle(Qt::DiagCrossPattern); // 画刷的风格,网格风格
        p.setBrush(brush); // 装画刷交给画家
        p.drawRect(10,20,200,150); // 以(10, 20)为起点,宽为200, 高为150的矩形里面刷东西
     p.setPen(QPen()); // 画笔恢复默认值
     p.setBrush(QBrush()); // 画刷恢复默认值
     p.drawEllipse(230,30,150,200);  // 以(230, 30)为起点,宽为150, 高为200的矩形画内切圆
     p.setBrush(QBrush(QPixmap("../image/test.jpg"))); // 画刷里面的内容是图片
     
     p.drawPixmap(0, 0, this->width(), this->height(), QPixmap("../image/on.png")); // 以窗口大小画图
     // 画图事件调用update()会使整个窗口的图片刷新,不要再绘图事件调用update(),播放动画,设置button的图片
     
     // 设置字体
     QFont font; // 需要头文件#include <QFont>
        font.setFamily("幼圆"); // 设置字体类型
        font.setPointSize(30); // 设置字体大小
        font.setBold(true); // 是否加粗
        font.setUnderline(true); // 是否加下划线
     font.setStrikeOut(true); // 设置横穿文字中间的线
        p.setFont(font); // 把字体交给画家
        p.setPen(Qt::red); // 设置画笔颜色
        p.drawText(20, 190, "你好,Qt!"); // 在(20, 190)为起点,写字
     
     // 这和绘图事件无关,截图
     QPixmap image = QPixmap::grabWidget(this, 0, 0, this->width(), this->height());
     image.save("1.png");    // 保存图片
     
     
    /********************************播放音乐(#include <QSound>)*************************************/
     // 一种方法
     QSound::play("mysounds/bells.wav");
     // 另一种方法
     QSound bells("mysounds/bells.wav");
      bells.play();
     // 也就是说
     QSound *bells = new QSound("mysounds/bells.wav");
     bells->play();
     bells->setLoops(-1); // 无限循环
     
    /***************************枚举的使用************************************/
     // 首先在一个类中定义一个枚举,如下
     class A
     {
     public:
     enum B{Empty, Black, White};
     };
     // 在类中的成员函数了,可以直接操作,如:
     B c = Empty;
     // 假如在别的类中,创建类A的对象指针a, 如 a = new A(this);
     // 这里有两种使用方法
     A::B test = a->Black;
     A::B test = A::Black;
     
     
    /********************************文件夹操作(#include <QDir>)************************************/
     QDir tempDir;   // 文件夹变量
        QString str = QCoreApplication::applicationDirPath(); //获取当前应用程序路径(#include <QCoreApplication>)
        str += "/outputImage";
        if (false == tempDir.exists(str))   // 当这个文件夹不存在时,才创建
        {
            bool ok = tempDir.mkdir(str);
            // 循环创建,直到成功创建
            while (false == ok)
            {
                ok = tempDir.mkdir(str);
            }
        }
     
    /******************************文件操作(#include <QFile>)********************************/
     // 写操作
     
     QFile file("1.txt"); // 创建文件对象
     
     //一般不要IO_ReadWrite,很容易出现赃数据
        //如果要在文件的后面添加内容要IO_WriteOnly|IO_Append
        //如果要清空原来的内容,只要IO_WriteOnly
        if(file.open(QIODevice::WriteOnly)) // 只写方式打开文件
        {
            QTextStream out(&file);
            out.setCodec(QTextCodec::codecForName("gb18030")); // 写之前设置编码
     out << i << "

    "<<"image"<<""<<"image"<<"

    " << Label[i]->x() << "

    "<<Label[i]>y()<<""<<Label[i]−>y()<<"

    "
                            << Label[i]->width() << "

    "<<Label[i]>height()<<""<<Label[i]−>height()<<"

    "<< *this->picPath[i]
                            << "$$"<< this->cardBackgound << " ";
     file.close();
     }
     
     // 读操作
     QFile file(fileTempPath);
        QString dataLine;
     // 只读方式打开
        if (file.open(QIODevice::ReadOnly))
        {
            QTextStream readData(&file);
            readData.setCodec(QTextCodec::codecForName("gb18030")); // 读之前也设置编码

            while (false == readData.atEnd())    // 是否是到文件的结尾
            {
                dataLine = readData.readLine();  // 先读一行
                if (dataLine.size() != 1)
                {
                    this->flag = dataLine.section("

    ",0,0).toInt();//this>data[flag][0]=dataLine.section("",0,0).toInt();//坐标下标this−>data[flag][0]=dataLine.section("

    ", 2, 2).toInt();  // x
                    this->data[flag][1] = dataLine.section("

    ",3,3).toInt();//ythis>data[flag][2]=dataLine.section("",3,3).toInt();//ythis−>data[flag][2]=dataLine.section("

    ", 4, 4).toInt();  // w
                    this->data[flag][3] = dataLine.section("$$", 5, 5).toInt();  // h
                }
     }
     
     file.close();
     }
     
    // 宏定义
     #define print qDebug()<<__FILE__<<__LINE__<<":"

     
    /************************memcpy的使用**********************************/
     // 函数的原型:void  *memcpy(void *dest,   const void *src,   size_t count); 
     int directioncpy[8][2] = {{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1}};
     int direction[8][2];
     // 函数的功能是将directioncpy的内容拷贝到direction
     // sizeof(int)*16,注意这个长度
        memcpy(direction,directioncpy,sizeof(int)*16);
     
     int chessHistroy[64][8][8];
     int chess[8][8]; 
     memcpy(chessHistroy[histroy],chess,sizeof(int)*64);

    /*******************设置按钮背景色**************************/
    setStyleSheet("background-color: rgb(255, 255, 0)");

    /************重写鼠标事件******************/
    QPoint dragPosition;     // 在类中增加这一个成员

      void Widget::mouseMoveEvent(QMouseEvent *e)
    {
        if(e->buttons() & Qt::LeftButton){ //当满足鼠标左键被点击时
            move(e->globalPos()-this->dragPosition); //移动窗口
        }
    }
    void Widget::mousePressEvent(QMouseEvent *e)
    {
        if(e->button() == Qt::LeftButton)  //点击左边鼠标
        {
             //globalPos()获取根窗口的相对路径,frameGeometry().topLeft()获取主窗口左上角的位置,
            // frameGeometry().topLeft()相当于起点坐标: this->x(), this->y();
            this->dragPosition=e->globalPos()-this->frameGeometry().topLeft();    // 和下面操作等价的
            // this->dragPosition.setX(e->x());
            // this->dragPosition.setY(e->y());

            //qDebug() << this->frameGeometry().topLeft();  // 等价下面
           //qDebug() << this->x() << "," << this->y();
      
        //qDebug() << e->globalPos();    // 等价下面
            //qDebug() << e->x()+x() << ","  << e->y()+y();
        }
        else if(e->button() == Qt::RightButton){
            this->close();
        }
    }

    /*************************QStringList*********************************/
        QStringList list("hello");
        qDebug()<<list; // ("hello")

        //追加
        list.append("word");
        qDebug()<<list; //("hello", "word")

        list<<"qt"<<"listen";
        qDebug()<<list; // ("hello", "word", "qt", "listen")

        //合并
        QString str;
        str = list.join(",");
        qDebug()<<str;  // "hello,word,qt,listen"

        //拆分
        str = "hello,word,,qt";
        QStringList list1;
        list1 = str.split(",");
        qDebug()<<list1; // ("hello", "word", "", "qt")

        //去掉空格
        list1 = str.split(",",QString::SkipEmptyParts);
        qDebug()<<list1; // ("hello", "word", "qt")

        //索引
        int num;
        num = str.indexOf(QRegExp("o"));
        qDebug()<<"num = "<<num; // num =  4

        num = list1.indexOf(QRegExp("hello"));
        qDebug()<<"QStringList list1 = "<<num; // QStringList list1 = 0

        num = str.lastIndexOf(QRegExp("o"));
        qDebug()<<"last num = "<<num; // last num =  7

        num = list1.lastIndexOf(QRegExp("hello"));
        qDebug()<<"last QStringList = "<<num; //last QStringList =  0

        //替换 ("hello", "word", "qt", "listen")
        list.replaceInStrings("o","eee");
        qDebug()<<"replace list = "<<list;
        // ("helleee", "weeerd", "qt", "listen")

        list.replace(2,"replace");
        qDebug()<<"replace only one list = "<<list;
        // ("helleee", "weeerd", "replace", "listen")

        //过滤 ("hello", "word", "qt")
        QStringList result;
        result = list1.filter("o");
        qDebug()<<"filter result = "<<result; // ("hello", "word")

        //查找
        if(list1.contains("hello")) // true
            qDebug()<<"true";

  • 相关阅读:
    宠物的生长(多态)
    程序员和程序狗
    表彰优秀学生(多态)
    [leetcode] Anagrams
    [leetcode] Add Two Numbers
    [leetcode] Add Binary
    [leetcode] 4Sum
    [leetcode] 3Sum Closest
    [leetcode] 3Sum
    函数成员修饰之私有方式
  • 原文地址:https://www.cnblogs.com/WinkJie/p/13568672.html
Copyright © 2011-2022 走看看