Method to Use The Pixmap Currently Visible Inside a QGraphicsScene/View in an Unrelated slot



  • I woud like to somehow gain access to the Pixmap currently displayed in my QGraphics view so that I may use QPixmap::scaled() with the scaled value corresponding to the value_changed() slot for a slider. I just can't seem to find a way to keep the Pixmap I once assigned to my GraphicsScene in scope, or another way to access it. Below was my attempt at using GraphicsView::scene() I am not entirely sure how to implement this method though, in my example it gives me an error message "QGraphicsSCene::QGraphicsScene(const QGraphicsScene&)" is private as well as use of deleted function "QgraphicsScene(constScene&)". Any idea how I would access the Pixmap currently displayed in my QGraphicsView Widget?

    code inside my slider_value_changed() slot

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

  • Qt Champions 2016

    @brazz said in Method to Use The Pixmap Currently Visible Inside a QGraphicsScene/View in an Unrelated slot:

    QGraphicsPixmapItem

    This seems to do it
    QPixmap QGraphicsPixmapItem::pixmap() const



  • If I'm right in interpreting your code there's still the issue that I can't actually access the scene viewed inside the graphics view, and in turn the graphics item. So I can't use QGraphicsPixmapItem::pixmap() due to not having the original GraphicsPixmapItem in scope still. The line QGraphicsScene graphicsscene = ui->PixmapView->scene(); doesn't compile and spits out the errors above.


  • Qt Champions 2016

    @brazz
    Oh, its private. I used it in a subclass so did not notice.
    Just keep the scene around then.



  • @mrjj I'm just not sure how I would go about keeping the scene around because it will be out of scope in the slots I am accessing the pixmap/scene in. Essentially what happens is the user opens a file, the actionOpen triggered() slot creates the pixMap and adds it to the scene and the graphicsView widget. After that though I need to scale the original pixmap using QPixmap::Scaled() in a slider_value_changed() slot, and that slot does not have access to the scene or the orginal pixmap. One possiblity would be make a class with a static pixmap memeber variable. But I have no idea if this would work or what the syntax would look like. I have been trying this possiblity for a while now.


  • Qt Champions 2016

    Hi
    Im not sure why you cant make it a member of the class that has the slot.
    Like he does here.
    http://www.bogotobogo.com/Qt/Qt5_QGraphicsView_QGraphicsScene.php

    It is completely other class than where you create the scene?



  • @mrjj If I use this line in each slot scene = new QGraphicsScene(this); will it be referring to the same scene everytime? I'm not entirely what "this" points to specifically in Qt.


  • Qt Champions 2016

    @brazz
    No no that would create a new one each time.
    You just do it ONCE. Like he does in sample
    ("this" is used as parent/owner)

    class Dialog : public QDialog
    {
    private:
        QGraphicsScene *scene; <<< define
    
    and then in constructor
    Dialog::Dialog(QWidget *parent) :
    {
        scene = new QGraphicsScene(this); <<< set it to point to an instance
    

    Then in any slot in Dialog class, you can use the variable scene to access it

    In your case QDialog is most likely QMainwindow but 100% same way.



  • @mrjj Works perfectly, thank you. I figured it would have to do with adding code to the Qt auto generated stuff, I just didn't have to toolset/knowledge to do that yet. This trick will be make my life so much easier now.


  • Qt Champions 2016

    @brazz
    Super. :)
    Well adding code to any of the auto generated files is
    not fun as they are overwritten
    often. (like moc_xx files)

    But the file the project wizard creates for you are for your use.
    So that is fine to add code to. its never re-created again.



  • @mrjj any idea why this bit of code says that the List is never used/declared

    QList<QGraphicsItem*> graphicsItemList(wavesScene->items());
       QGraphicsPixmapItem pixmapItem(graphicsItemList.at(0));
    

  • Qt Champions 2016

    @brazz said in Method to Use The Pixmap Currently Visible Inside a QGraphicsScene/View in an Unrelated slot:

    QList<QGraphicsItem*> graphicsItemList(wavesScene->items());
    QGraphicsPixmapItem pixmapItem(graphicsItemList.at(0));

    Ehh no as it's both declared and used.

    What is the exact error ?
    (you can right click and get it)

    Same code in test project is not showing any warning.

    also just to be sure u mean it
    QGraphicsPixmapItem pixmapItem(graphicsItemList.at(0))
    This makes a new QGraphicsPixmapItem (locally) and set
    graphicsItemList.at(0) as parent.
    Its not same as
    QGraphicsPixmapItem *pixmapItem = graphicsItemList.at(0);
    That would point to the first item in the list.



  • @mrjj D:\SEGYView\segyview.cpp:209: error: 'graphicsItemList' was not declared in this scope
    QGraphicsPixmapItem* pixmapItem = graphicsItemList.at(0);

                                       ^
    

    I tried I did mean to use the line that you provided, however it still gave the same error



  • this is how I added the first item in my actionOpen triggered() slot

            wavesScene->addItem(pixmapGraphicsItem);
    
            ui->PixmapView->setScene(wavesScene);

  • Qt Champions 2016

    @brazz

    D:\SEGYView\segyview.cpp:209: error: 'graphicsItemList' was not declared in this scope

    but those two lines are RIGHT next to each other or ?

    just like you show ?

    QList<QGraphicsItem*> graphicsItemList(wavesScene->items());
    QGraphicsPixmapItem pixmapItem(graphicsItemList.at(0));
    


  • Exactly like that. Could it be that I'm missing some inclusions? I included <QGraphicsPixmapItem> <QList> <QGraphicsItem>


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.