2017-08-08 68 views
0

如果我想在第一个地方甚至使用QGraphicsView::scene(),语法会是什么样子?我的目标是用滑块在我的图形视图对象中更改pixmap的比例。我想用QgraphicsView::scene()QScene::itemsAt()等找到原始pixmap,然后使用QPixmap::scaled()(我发现这将是唯一的方法来确保我的化妆品属性设置为pixmap成立)。但是,我遇到了QGraphicsView::scene()的语法问题。我的尝试如下。我还创建了一个Qt窗口小部件应用程序。使用QGraphicsView方法场景()来分配QGraphicsScene变量

QGraphicsViewScene graphicsScene = ui->PixmapView->scene(); 
QGraphicsPixmapItem graphicsPixmapItem = graphicsScene.itemAt(0, 0); 

编辑 如果我来存储我的QPixmap像素图*作为成员变量,我不能完全确定如何实现它范围仍然为我的插槽。

编辑 静态成员变量?

+0

这应该是QGraphicsScene * graphicsScene,不是的QGraphicsView –

+0

你为什么不只是存储你的'QGraphicsPixmapItem *'作为成员变量? – thuga

+0

@thuga我会怎么做对不起 – CrippledTable

回答

1

你可以让你的QGraphicsPixmapItem对象成为你的类的成员变量。然后你就可以从你的任何类的成员函数中访问它。

下面是一个简单的例子:

class MyClass : public QWidget 
{ 
    Q_OBJECT 
public: 
    MyClass(QWidget *parent = nullptr) : QWidget(parent) 
    { 
     // create graphics view, scene, etc.. 
    } 

public slots: 
    void openActionTriggered() 
    { 
     ... 
     myItem = scene->addPixmap(myPixmap); // you can create your item however you want.. this is just an example 
    } 
    void mySlot() 
    { 
     if(myItem) 
     { 
      // do something with myItem 
     } 
    } 

private: 
    QGraphicsPixmapItem *myItem = nullptr; // myItem is a member variable of 
    QGraphicsScene *scene = nullptr; // I made scene a member variable so it can be accessed from any member functions 
} 
+0

这是一个很好的观点,我希望能够使用我的实现。但我很欣赏周围的工作。我会测试它,并希望不会得到同样的问题。 – CrippledTable