2017-08-08 72 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