很简单的一个程序,这也是我用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);
}
}
代码在windows7下编译通过,在linux下理论上应该也可以
Release版本下载:
该Release版本是动态编译后的程序,在没有装QT的XP电脑上提示不是有效的WIN32程序,本人不明白这是什么原因,也不知道在WIN7、WIN8下能否正常运行,欢迎反馈