Qt QNetworkAccessManager未使用此代码发出完成信号
因此,我以前一直在浏览有关此问题的问题,但我无法找到代码的解决方案Qt QNetworkAccessManager未使用此代码发出完成信号,qt,Qt,因此,我以前一直在浏览有关此问题的问题,但我无法找到代码的解决方案 cpp file of dialog ------------------------------------------------ #include "everesult.h" #include "ui_everesult.h" everesult::everesult(QWidget *parent) : QDialog(parent), ui1(new Ui::everesult) { ui1-
cpp file of dialog
------------------------------------------------
#include "everesult.h"
#include "ui_everesult.h"
everesult::everesult(QWidget *parent) :
QDialog(parent),
ui1(new Ui::everesult)
{
ui1->setupUi(this);
}
everesult::~everesult()
{
delete ui1;
}
void everesult::setmodel(QStandardItemModel *model)
{
ui1->listView->setModel(model);
}
void everesult::on_buttonBox_clicked(QAbstractButton *button)
{
EveReprocess M_;
QModelIndex Selectedindex = ui1->listView->currentIndex();
QModelIndex StationIdsindex = ui1->listView->model()->index(0, 1);
int typeID = 0;
int stationID = 0;
stationID = ui1->listView->model()->data(StationIdsindex, Qt::DisplayRole).toInt();
typeID = ui1->listView->model()->data(Selectedindex, Qt::DisplayRole).toInt();
M_.GetMaterials(typeID, stationID);
}
--------------------------------------------------
Getmaterial and replyFinished from main window.
--------------------------------------------------
void EveReprocess::GetMaterials(int typeId, int stationid)
{
//get typeid from material list
this->modelMaterial = new QSqlQueryModel();
modelMaterial->setQuery(QString("SELECT tm.quantity, tm.materialTypeID, t.typeName FROM invTypeMaterials tm INNER JOIN invTypes t ON t.TypeID = tm.materialTypeId WHERE tm.TypeID=%1 ").arg(typeId));
if (!modelMaterial->query().exec())
qDebug() << modelMaterial->query().lastError();
//Set eve Central Url with typeids
QUrl url = QUrl("http://api.eve-central.com/api/marketstat?");
QUrlQuery q;
int numRows = modelMaterial->rowCount();
for (int row = 0; row < numRows; ++row)
{
QModelIndex index = modelMaterial->index(row, 1);
q.addQueryItem( QString("typeid"), QString::number(modelMaterial->data(index, Qt::DisplayRole).toInt()));
}
q.addQueryItem( QString("usesystem"), QString::number(stationid));
//set created url and connect
url.setQuery(q);
qDebug() << url;
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply *)));
manager->get(QNetworkRequest(url) );
}
void EveReprocess::replyFinished(QNetworkReply *reply)
{
qDebug() << "replyFinished called";
if ( reply->error() != QNetworkReply::NoError ) {
qDebug() << "Request failed, " << reply->errorString();
emit replyFinished(false);
return;
}
qDebug() << "Request succeeded";
//process with xmlreader and get values
processSearchResult( reply);
}
有些代码在这里,我认为它应该在这里的某个地方,因为其余的工作正常
这个问题出现在我使用对话框让用户从列表中选择int之后
下面是调用我为此创建的对话框的函数。代码格式很抱歉,我会在它工作后清理它
void EveReprocess::Search_TypeId(QString ItemName, QString SystemName)
{
QList<int> TypeIdList;
QList<int> StationIdList;
modelIds = new QStandardItemModel(10,2,this);
if !(db.isOpen()) return;
this->queryItem = new QSqlQuery;
queryItem->prepare("SELECT typeID FROM invTypes WHERE invTypes.typeName LIKE ? AND invTypes.groupID NOT IN (268,269,270)AND published= 1");
ItemName.prepend("%");
ItemName.append("%");
queryItem->bindValue(0, ItemName);
this->queryStation = new QSqlQuery;
queryStation->prepare("SELECT solarSystemID FROM mapSolarSystems WHERE mapSolarSystems.solarSystemName LIKE ?");
SystemName.prepend("%");
SystemName.append("%");
queryStation->bindValue(0, SystemName);
if(!queryStation->exec() || !queryItem->exec() )
{
qDebug() << queryItem->lastError().text();
qDebug() << queryItem->lastQuery();
qDebug() << queryStation->lastError().text();
qDebug() << queryStation->lastQuery();
}
while( queryStation->next())
{
StationIdList.append(queryStation->value(0).toInt());
}
while(queryItem->next())
{
TypeIdList.append(queryItem->value(0).toInt());
}
for (int i = 0; i < StationIdList.count(); ++i)
{
modelIds->setItem(i,1,new QStandardItem(QString::number(StationIdList.at(i))));
}
for (int i = 0; i < TypeIdList.count(); ++i)
{
modelIds->setItem(i,0,new QStandardItem(QString::number(TypeIdList.at(i))));
}
//
everesult Dialog;
Dialog.setmodel(modelIds);
Dialog.exec();
}
在继续之前,您的一些代码是允许的。即使它不是一个安全漏洞,它仍然会导致错误。您应该使用绑定,而不是在SQL查询中使用字符串替换 你的问题是:
everesult Dialog;
Dialog.setmodel(modelIds);
Dialog.exec();
exec是一个阻塞函数-它阻塞主事件循环,直到对话框被取消。因此,来自线程化网络访问管理器的信号永远不会传递到对象
您应该异步显示对话框,如下所示:
everesult * dialog = new everesult;
dialog->setModel(modelIds);
dialog->show();
connect(dialog, SIGNAL(accepted()), dialog, SLOT(deleteLater());
connect(dialog, SIGNAL(rejected()), dialog, SLOT(deleteLater());
请注意,类型名称以小写开头,变量名称以大写开头会产生误导。Qt的约定正好相反,保留它是很有用的,除非你有很好的理由不这样做
在SQL查询中执行参数绑定
在SQL查询中不进行字符串替换
请不要使用pastebin。至少不是你做这件事的方式。您的粘贴将在6天后过期。在那之后,你的问题对其他任何人都没有用。因此,它不是一个仅为您提供的问答服务,而是一个为每个人提供答案和问题的存储库。我知道我会编辑我的postyeh,用于常规查询,但我不能用QSqlQueryModel绑定值?当然可以,您只需不在模型上绑定它,而是在查询上绑定它,然后在模型上设置查询,仅此而已。我已经更新了答案以显示这一点。好的,我更改了我的sql查询和对话框,但我仍然没有收到完成的信号。请检查连接到网络访问管理器是否成功。您还忽略了经理发出的其他信号。例如,需要身份验证。当然,我也会连接到network access manager的destroyed signal,这样您就知道它不会因为您的错误而被过早地删除,也就是说,在我将另一个模板添加到我的项目之前,这段代码曾经工作过。是请求url,正如您所看到的,它是一个xml文件,我希望在接收到完成的信号后解析它
QSqlQuery query("SELECT .. WHERE tm.TypeID=:typeid");
query.bindValue(":typeid", typeId);
QSqlQueryModel model;
model.setQuery(query);
setQuery(QString("SELECT ... WHERE tm.TypeID=%1 ").arg(typeId));