QT 编程问题,帮我看看这段代码,为什么写在构造函数中不运行,用按钮点击才能运行,我想实现自动访问网络
我想在构造函数中访问网络,返回数据在textbrowser中显示,代码如下CuifeiForm::CuifeiForm(QWidget*parent):QWidget(p...
我想在构造函数中访问网络,返回数据在textbrowser中显示,代码如下
CuifeiForm::CuifeiForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::CuifeiForm)
{
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint);
//统计催费信息
QString initHtml="正在进行催费统计,请稍后...";
ui->textBrowser->setText(initHtml);
QSettings *ConfigIni = new QSettings("/ppconfig.ini",QSettings::IniFormat);
QString serviceip=ConfigIni->value("serviceHost").toString();
QString posid=ConfigIni->value("cardid").toString();
QString httpUrl=tr("http://").append(serviceip).append("/pp/PayService?act=05&posid=").append(posid);
manager = new QNetworkAccessManager(this);
connect(manager,SIGNAL(finished(QNetworkReply *)),this,SLOT(replyCuifei(QNetworkReply *)));
manager->get(QNetworkRequest(QUrl(httpUrl))); //发送请求
}
// slot函数如下
void CuifeiForm::replyCuifei(QNetworkReply *reply){
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
//使用utf8编码,这样才可以显示中文
all = codec->toUnicode(reply->readAll());
//QTextStream out(stdout);
//out<<all;
ui->textBrowser->setText(all);
reply->deleteLater(); //最后要释放reply对象
}
我还有一个刷新按钮和构造函数中写的代码一样,如下:
void CuifeiForm::on_pushButton_clicked()
{
QString initHtml="正在进行催费统计,请稍后...";
ui->textBrowser->setText(initHtml);
QSettings *ConfigIni = new QSettings("/ppconfig.ini",QSettings::IniFormat);
QString serviceip=ConfigIni->value("serviceHost").toString();
QString posid=ConfigIni->value("cardid").toString();
QString httpUrl=tr("http://").append(serviceip).append("/pp/PayService?act=05&posid=").append(posid);
manager = new QNetworkAccessManager(this);
connect(manager,SIGNAL(finished(QNetworkReply *)),this,SLOT(replyCuifei(QNetworkReply *)));
manager->get(QNetworkRequest(QUrl(httpUrl))); //发送请求
}
现在的问题是,只有点击刷新按钮时,才能请求网络返回信息。刚打开这个窗口时不自动访问网络。不知道问题出在哪里,请这方面的高手帮助。 展开
CuifeiForm::CuifeiForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::CuifeiForm)
{
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint);
//统计催费信息
QString initHtml="正在进行催费统计,请稍后...";
ui->textBrowser->setText(initHtml);
QSettings *ConfigIni = new QSettings("/ppconfig.ini",QSettings::IniFormat);
QString serviceip=ConfigIni->value("serviceHost").toString();
QString posid=ConfigIni->value("cardid").toString();
QString httpUrl=tr("http://").append(serviceip).append("/pp/PayService?act=05&posid=").append(posid);
manager = new QNetworkAccessManager(this);
connect(manager,SIGNAL(finished(QNetworkReply *)),this,SLOT(replyCuifei(QNetworkReply *)));
manager->get(QNetworkRequest(QUrl(httpUrl))); //发送请求
}
// slot函数如下
void CuifeiForm::replyCuifei(QNetworkReply *reply){
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
//使用utf8编码,这样才可以显示中文
all = codec->toUnicode(reply->readAll());
//QTextStream out(stdout);
//out<<all;
ui->textBrowser->setText(all);
reply->deleteLater(); //最后要释放reply对象
}
我还有一个刷新按钮和构造函数中写的代码一样,如下:
void CuifeiForm::on_pushButton_clicked()
{
QString initHtml="正在进行催费统计,请稍后...";
ui->textBrowser->setText(initHtml);
QSettings *ConfigIni = new QSettings("/ppconfig.ini",QSettings::IniFormat);
QString serviceip=ConfigIni->value("serviceHost").toString();
QString posid=ConfigIni->value("cardid").toString();
QString httpUrl=tr("http://").append(serviceip).append("/pp/PayService?act=05&posid=").append(posid);
manager = new QNetworkAccessManager(this);
connect(manager,SIGNAL(finished(QNetworkReply *)),this,SLOT(replyCuifei(QNetworkReply *)));
manager->get(QNetworkRequest(QUrl(httpUrl))); //发送请求
}
现在的问题是,只有点击刷新按钮时,才能请求网络返回信息。刚打开这个窗口时不自动访问网络。不知道问题出在哪里,请这方面的高手帮助。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询