传递对 QPixmap ctor 的引用

发布于 2024-12-20 00:43:06 字数 1038 浏览 1 评论 0原文

当我尝试传递似乎是对我的 QPixmap 的引用时,我收到错误:

 error: no matching function for call to ‘QGraphicsScene::addItem(QGraphicsPixmapItem (&)(QPixmap))’

问题是我不知道该引用来自哪里,尽管我猜测它来自来自迭代器。

void MainWindow::addItems(std::map<std::string, QString> * items)
{
    std::map<std::string, QString>::const_iterator current;

    for(current = items->begin(); current != items->end(); ++current)
    {
        QString cur = current->second;
        QGraphicsPixmapItem item(QPixmap(cur));
        _scene->addItem(item);

    }

}

如果是这种情况,有没有办法取消引用迭代器?不然的话,我到底做错了什么?

调用它的代码

int main(int argc, char *argv[])
{
    std::map<std::string, QString> * items;

    items->insert(std::pair<std::string, QString>("ozone", ":/images/ozone_sprite.png"));

    QApplication a(argc, argv);
    MainWindow window;
    window.addItems(items);
    window.show();

    delete items;

    return a.exec();
}

When I try to pass what appears to be a reference to my QPixmap, I get an error:

 error: no matching function for call to ‘QGraphicsScene::addItem(QGraphicsPixmapItem (&)(QPixmap))’

The problem is that I don't know where this reference is coming from, though I'm guessing it's coming from the iterator.

void MainWindow::addItems(std::map<std::string, QString> * items)
{
    std::map<std::string, QString>::const_iterator current;

    for(current = items->begin(); current != items->end(); ++current)
    {
        QString cur = current->second;
        QGraphicsPixmapItem item(QPixmap(cur));
        _scene->addItem(item);

    }

}

If that's the case, is there a way to de-reference the iterator? Otherwise, what is it that I'm doing wrong?

The code which calls it

int main(int argc, char *argv[])
{
    std::map<std::string, QString> * items;

    items->insert(std::pair<std::string, QString>("ozone", ":/images/ozone_sprite.png"));

    QApplication a(argc, argv);
    MainWindow window;
    window.addItems(items);
    window.show();

    delete items;

    return a.exec();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日裸衫吸 2024-12-27 00:43:06

您已经犯了所谓的 C++ 的“最令人烦恼的解析”。具体来说,它:

QGraphicsPixmapItem item(QPixmap(cur));

声明一个名为 item 的函数,该函数采用 QPixmap 类型的单个参数并返回一个 QGraphicsPixmapItem。要解决此问题,请写入:

QPixmap temp(cur);
QGraphicsPixmapItem item(temp);

请参阅此处:

http://en.wikipedia.org/wiki/Most_veshing_parse

就错误而言,请注意您正在尝试使用 QGraphicsPixmapItem (&)(QPixmap) 类型的参数调用 addItem - 也就是说,引用一个采用QPixmap并返回QGraphicsPixmapItem的函数(这是表达式item的类型)。

You've fallen foul of what's known as C++'s 'most vexing parse'. Specifically, this:

QGraphicsPixmapItem item(QPixmap(cur));

declares a function called item that takes a single parameter of type QPixmap and returns a QGraphicsPixmapItem. To fix this, write:

QPixmap temp(cur);
QGraphicsPixmapItem item(temp);

See here:

http://en.wikipedia.org/wiki/Most_vexing_parse

As far as the error goes, note that you're trying to call addItem with an argument of type QGraphicsPixmapItem (&)(QPixmap) - that is, reference to a function taking a QPixmap and returning a QGraphicsPixmapItem (which is the type of the expression item).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文