如何调用此方法而不会得到C2227错误? [英] How do I call this method without getting C2227 error?

查看:238
本文介绍了如何调用此方法而不会得到C2227错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++ 11中编程一个Qt应用程序,我正在学习,所以这可能是一个很明显的答案的问题。我有一个应用程序,在主窗口中用缩略图(picts)填充图形场景,用户可以选择多个。我试图实现功能,当用户点击一个,上一个选择被清除。由于鼠标点击事件发生在pict对象上,我试图让它告诉整个窗口取消选择其余的。

I'm programming a Qt application in C++11 and I'm learning as I go, so this is probably a question with a really obvious answer. I've got an application that fills a graphics scene in the mainwindow with thumbnails (picts) that the user can select multiple of. I'm trying to implement functionality that when a user clicks on one, the previous selection is cleared. Since mouse click events occur on the pict object, I'm trying to get it to tell the whole window to deselect the rest.

这里是main.cpp:

Here's the main.cpp:

#include "mainwindow.h"
#include <QApplication>
#include <QtGui>
#include <QtWidgets>



int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow window;

    window.show();
    return a.exec();
}

这里是mainwindow.cpp的摘录:

here's an excerpt of mainwindow.cpp:

vector<Pict*> PictArray;  
vector<int> ActiveList;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);

    int viewwidth = ui->graphicsView->width();

    //test values
    int moment = 0;
    nitems = 47; 

    int maxitems_x = viewwidth/72; 
    int maxitems = 1000;
    if(nitems > maxitems){nitems = maxitems;}

    PictArray.resize(nitems);

    //fill the scene
    int i = 0, j = 0, k = 0;
    while(true){
        PictArray[k] = new Pict(moment, k);         
        PictArray[k]->setPos(QPointF(i*72, j*72 + 16));        
        scene->addItem(PictArray[k]);
        i++;
        k++;
        if(i == maxitems_x){i=0; j++;}
        if(k == nitems){break;}
    }

    qDebug() << "Drew" << nitems << "items" ;

    //test select some objects

    array<int, 4> v0_init = {3, 6, 9, 12};  //can't use list constructors with MSVC12)
    vector<int> v0(v0_init.begin(), v0_init.end());
    selectPicts(v0);
}

.
.
.

void MainWindow::deselectAllPicts()
{
    for(auto& i : ActiveList)
    {
        PictArray[i]->setSelected(false);
    }
    ActiveList.clear();
}

,然后从pict对象中删除此方法:

and then from the pict object, this method:

void Pict::mousePressEvent(QGraphicsSceneMouseEvent *event)
{   
    window->deselectAllPicts();
    qDebug() << "selected: set " << this->s << " img " << this->n;
    this->color.setRed((qrand() % 256 + 255)/2);        //test: on click, set border to a random color
    this->color.setGreen((qrand() % 256  + 255)/2);
    this->color.setBlue((qrand() % 256 + 255)/2);
    this->isactive = true;
    update();
    QGraphicsItem::mousePressEvent(event);
}

但我得到错误C2227( - > deselectAllPicts必须指向一个类/ struct / union / generic type)。如何解决这个问题?作者承认他从来没有任何善于OOP,所以我感谢任何帮助。

but I get error C2227 ("->deselectAllPicts must point to a class/struct/union/generic type"). How do I go about fixing this? The author admits he was never any good at OOP, so I appreciate any help.

(我不打算留在Mainwindow这一切 - 我要去

(I don't plan on leaving this all on Mainwindow - I'm going to make the scene into a custom object when I start adding more stuff than just this to the window)

推荐答案

假设您重新实现了<$在 Pict 类中可以添加一个额外的参数到 Pict 构造函数并将您的窗口传递给它:

Assuming you reimplemented QGraphicsItem in your Pict class, you can add an extra argument to Pict constructor and pass your window to it:

PictArray[k] = new Pict(moment, k, this); // this in MainWindow refers to window itself

将此窗口指针存储在 Pict 构造函数:

Store this window pointer in Pict constructor:

Pict::Pict(int moment, int k, MainWindow *window) : mainWindow(window), ... {...}

然后访问 Pict

mainWindow->deselectAllPicts();

编辑。您可以使用 QGraphicsScene 机制简单地选择/取消选择/获取所选项目的列表。看看 QGraphicsScene :: setSelectionArea()和相关方法。

Edit. P.S. you can simply select / deselect / get a list of selected items with QGraphicsScene mechanism. Have a look at QGraphicsScene::setSelectionArea() and related methods.

这篇关于如何调用此方法而不会得到C2227错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆