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 !!\nIt 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\n";
    }    
    if(e->button()==Qt::RightButton) // 右擊
    {
        s  = "RightButton Pressed\n";
    }
    if(e->button()==Qt::MidButton) // 中間滑輪點擊
    {
        s = "MidButton Pressed\n";
    }
   
 // 鼠標釋放事件,操作和點擊一樣
 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 << "\n";
 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";

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章