【源码】QT获取QQ昵称

很简单的一个程序,这也是我用Qt写的第一个程序,会的人5分钟就能写完,而我却整整花了一个晚上!!!

程序截图:


代码:

void MainWindow::on_pushButton_clicked()
{
    //^[1-9][0-9]{4,10}$
    QString reg("^[1-9][0-9]{4,10}$");
    QRegExp rex(reg);
    bool isQQ=rex.exactMatch(ui->lineEdit->text());
    qDebug()<<isQQ;
    if(!isQQ)
    {
        QString title="提示";
        QString info="请输入正确的QQ号码!  ";
        QMessageBox::information(NULL,title,info);
    }
    else
    {
       const QString URLSTR="http://qq.ico.la/qq"+ui->lineEdit->text()+".html";
       const QString FILE_NAME="code.txt";
       ui->nameLabel->setText("正在查询,请稍后...");

       QUrl url(URLSTR);
       QNetworkAccessManager manager;
       QEventLoop loop;
       qDebug()<<"Reading code from "<<URLSTR;
       QNetworkReply *reply=manager.get(QNetworkRequest(url));
       QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
       loop.exec();

       QString code = reply->readAll();
       //QFile file(FILE_NAME);
       //file.open(QIODevice::WriteOnly);
       //QTextStream out(&file);
       //out << code << endl;
       //file.close();
       QString pattern("用户昵称</td>");
       QRegExp rx(pattern);
       int pos =code.indexOf(rx) ;           // 0, position of the first match.
                                                   // Returns -1 if str is not found.
                                                   // You can also use rx.indexIn(str);
       QString str="";
       qDebug() << pos;
           if ( pos >= 0 )
           {
              pos+=34;
              while(code[pos]!='<')
              {
                str+=code[pos];
                pos++;
              }
              //qDebug()<<str;
              str="该用户的昵称为:"+str;
           }
           else str="查询失败,请确定是否为正确的QQ号!";
       //qDebug() << "Finished, the code have written to " << FILE_NAME;/**/
       ui->nameLabel->setText(str);
    }
}


代码下载(Desktop Qt 5.3 MSVC2012 OpenGL 32bit):

代码在windows7下编译通过,在linux下理论上应该也可以
Release版本下载:

该Release版本是动态编译后的程序,在没有装QT的XP电脑上提示不是有效的WIN32程序,本人不明白这是什么原因,也不知道在WIN7、WIN8下能否正常运行,欢迎反馈

http://pan.baidu.com/s/1sjnsZ9b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值