我是Qt和C ++的新手。我有一个全局QFile-Variables的问题,我需要在不同的函数中使用我的MainWindow-Class。
//mainwindow.h
[...]
public:
QFile *fIndex;
QFile *fString;
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
[...]
在mainwindow.cpp中的我试图实例化:
//mainwindow.cpp
[...]
void MainWindow::on_btn_load_load_released()
{
QString _index_ = this->ui->txt_load_index->text();
QString _string_ = this->ui->txt_load_str->text();
fIndex = new QFile(_index_);
fString = new QFile(_string_);
foreach(QString iList, xmlActions::GetXMLID(fIndex))
{
this->ui->lst_src_result->addItem(iList);
}
}
[...]
在循环中调用的类包含以下函数:
//mainheader.h
QList<QString> GetXMLID (QFile XMLIndex)
{
QList<QString> xList;
//QFile* xFile = new QFile(XMLFile);
if (XMLIndex.open(QIODevice::ReadOnly))
{
QXmlStreamReader reader(XMLIndex.readAll());
XMLIndex.close();
while(!reader.atEnd())
{
reader.readNext();
foreach(const QXmlStreamAttribute &attr, reader.attributes())
{
if (attr.name().toString() == QLatin1String("ID"))
{
//contList.addItem(attr.value().toString());
xList << attr.value().toString();
}
}
}
}
return xList;
}
某种程度上xmlActions :: getXMLID(fIndex)不喜欢指针或其他东西。编译器抱怨:
没有匹配函数来调用&#39; xmlActions :: GetXMLID(QFile *&amp;)&#39;
我正试图为驴子岁月奔波。我还尝试使用带有构造函数和析构函数的类来实例化。但是我只能在那个单独的on_btn_load_load_released() - 函数中使用它。我做错了什么?
答案 0 :(得分:0)
您使用指向QFile
的指针,但您的函数将QFile
对象作为参数。这就是你得到这个错误的原因。如果要使用指向QFile
的指针,则需要在参数中使用指向QFile
的指针重写函数。
试试这个:
QList<QString> GetXMLID (QFile *XMLIndex)//prototype should be with STAR too
{
QList<QString> xList;
//QFile* xFile = new QFile(XMLFile);
if (XMLIndex->open(QIODevice::ReadOnly))//it is a pointer so we should use ->(not .)
{
QXmlStreamReader reader(XMLIndex->readAll());
XMLIndex->close();
while(!reader.atEnd())
{
reader.readNext();
foreach(const QXmlStreamAttribute &attr, reader.attributes())
{
if (attr.name().toString() == QLatin1String("ID"))
{
//contList.addItem(attr.value().toString());
xList << attr.value().toString();
}
}
}
}
return xList;
}
或使用QFile fIndex;
代替QFile *fIndex;
在此之后,错误应该消失。
答案 1 :(得分:0)
有效!非常感谢。我还必须使函数变为静态,因为我没有创建类的实例。使用QFiles不作为指针不能以某种方式工作,所以我做了一个。也许我会在以后找出原因。目前这是有效的:)