Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setters and getters(for using pixmap of one class in another)
Forum Updated to NodeBB v4.3 + New Features

Setters and getters(for using pixmap of one class in another)

Scheduled Pinned Locked Moved General and Desktop
17 Posts 3 Posters 4.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    venkatesh
    wrote on last edited by
    #1

    Hi,
    I wanted to implement a getter and setter function to get the pixmap from one class and set the pixmap in another class. But when i execute the program nothing happens.

    The code

    overlayview.h

    @
    public slots:
    void setOpacityView();
    @

    @
    MovingImageViewer MovingImageViewerObject;//creating objects
    StationaryImageViewer StationaryImageViewerObject;
    @

    overlayview.cpp

    @
    void OverlayView::setOpacityView()
    {
    // OpacityViewMovingItem_->resetMatrix();
    // MovingVtkImageItem_->resetMatrix();
    // ManualRegistrationScene_->removeItem(OpacityViewMovingItem_);
    // ManualRegistrationScene_->removeItem(OpacityViewStationaryItem_);
    // ManualRegistrationScene_->removeItem(DifferenceImageItem_);
    // ManualRegistrationScene_->clear();

    OpacityViewStationaryPixmap_=StationaryImageViewerObject.getStationaryImagePixmap();
    OpacityViewStationaryItem_->setPixmap(OpacityViewStationaryPixmap_);
    OpacityViewStationaryItem_->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    ManualRegistrationScene_->addItem(OpacityViewStationaryItem_);
    setScene(ManualRegistrationScene_);
    OpacityViewMovingItem_->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    OpacityViewMovingPixmap_=MovingImageViewerObject.getMovingImagePixmap();
    OpacityViewMovingItem_->setPixmap(OpacityViewMovingPixmap_);
    OpacityViewMovingItem_->setOpacity(0.7);
    ManualRegistrationScene_->addItem(OpacityViewMovingItem_);
    setScene(ManualRegistrationScene_);
    fitInView(ManualRegistrationScene_->itemsBoundingRect(),Qt::IgnoreAspectRatio);
    update();
    show();
    }
    @

    movingimageviewer.h

    @
    QPixmap getMovingImagePixmap()const;
    public slots:
    void setMovingImage(QListWidgetItem *MovingItem);
    @

    movingimageviewer.cpp

    @
    #include "movingimageviewer.h"
    #include<QFileInfo>
    #include<QString>

    MovingImageViewer::MovingImageViewer(QWidget *parent):
    QGraphicsView(parent)
    {

    }
    void MovingImageViewer::setMovingImage(QListWidgetItem *MovingItem)
    {
    MovingImageScene_->removeItem(MovingImageItem_);
    MovingImageScene_->clear();
    QVariant MovingListData=MovingItem->data(Qt::UserRole);
    MovingImagePath_=MovingListData.toString();
    MovingPixmap_.load(MovingImagePath_);
    MovingImageItem_->setPixmap(MovingPixmap_);
    MovingImageScene_->addItem(MovingImageItem_);
    fitInView(MovingImageScene_->itemsBoundingRect(),Qt::IgnoreAspectRatio);
    setScene(MovingImageScene_);
    show();
    update();
    }

    QPixmap MovingImageViewer::getMovingImagePixmap()const
    {
    return MovingPixmap_;
    }
    @

    I have implemented the other class too in the same way. I am not sure why pixmap is not having a valid data. Is the image set in MovingPixmap only valid to the particular function ? (here : setMovingImage)or is there any other mistake in my implementation

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Did you check if the returned pixmap is null ?
      Use
      @bool QPixmap::isNull () const@

      157

      1 Reply Last reply
      0
      • V Offline
        V Offline
        venkatesh
        wrote on last edited by
        #3

        Hi,
        I tried saving the pixmap inside the set function. It works, it means the pixmap inside the set function is valid but accessing it outside the set function doesn't work. Is the value of pixmap valid only inside the set function ?? I have declared it as public and also tried declaring the variables as protected but no valid result. I guess the pixmap becomes null when I access it at the get function. I couldn't figure what's the reason for it

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          Yes thats what i'm talking you checked inside the set function but did you checked after the get function ?
          In this case after,
          @OpacityViewStationaryPixmap_=StationaryImageViewerObject.getStationaryImagePixmap();@

          check

          @OpacityViewStationaryPixmap_.isNull();@

          157

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            Or you are checking for
            @OpacityViewMovingPixmap_=MovingImageViewerObject.getMovingImagePixmap();@
            Try after that the null condition.

            157

            1 Reply Last reply
            0
            • N Offline
              N Offline
              NicuPopescu
              wrote on last edited by
              #6

              Hi,

              beside checking if pixmap is null you could print out MovingImagePath_ ... may be there is something wrong with the item's data and conversion from QVariant?

              1 Reply Last reply
              0
              • V Offline
                V Offline
                venkatesh
                wrote on last edited by
                #7

                Hi,

                Hey i found that the pixmap is empty.Why is this happening?. What am i doing wrong in the getter function?I think there is no problem in the items conversion as i could display the image in the original class which is setMovingImage(...)but not when i return it and access it in another class. I think some how the pixmap becomes null when i return it.

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  NicuPopescu
                  wrote on last edited by
                  #8

                  have a look at "Implicit Data Sharing ":http://qt-project.org/doc/qt-5.0/qtcore/implicit-sharing.html

                  even though it seems it should work you could try to use copy() instead of =operator which according to doc does not do a deep copy of pixmap data but just returns a reference untill a paint event occurs on the QPixmap object getting the reference

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    venkatesh
                    wrote on last edited by
                    #9

                    Hey the link is not opening i don't know why. How can i use the copy()it needs some parameters which i don't use in my program. But i am puzzled why doesn't my pixmap contain the image and why is it null?..

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      venkatesh
                      wrote on last edited by
                      #10

                      Hi,
                      I am sure the problem is with the getData(); i think the other class which receives the pixmap is not receiving the data. Is the problem with the object created in overlayview?

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        venkatesh
                        wrote on last edited by
                        #11

                        Hi,
                        I found out that the object created @StationaryImageViewerObject@ has no information, as i didn't connect it with the @StationaryImageViewer@ though i have created the object i learnt i need to connect it with the StationaryImageViewer class, I am not so sure how to do it but. Would be privileged to have any of your guidances

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          NicuPopescu
                          wrote on last edited by
                          #12

                          @ StationaryImageViewer StationaryImageViewerObject;@

                          StationaryImageViewerObject is of type StationaryImageViewer ... how do you mean by connecting them?

                          1 Reply Last reply
                          0
                          • V Offline
                            V Offline
                            venkatesh
                            wrote on last edited by
                            #13

                            Hi,
                            The problem is though i have created a object of the class. But i don't know how to make the Qt understand that i am pointing to the particular class.

                            will this help

                            @
                            StationaryImageViewer StationaryImageViewer;
                            @
                            ?

                            1 Reply Last reply
                            0
                            • N Offline
                              N Offline
                              NicuPopescu
                              wrote on last edited by
                              #14

                              and btw I have tested returning QPixmap from a function, passing as value or copying by = operator and all work just fine! you're doing something wrong elsewhere :)

                              1 Reply Last reply
                              0
                              • V Offline
                                V Offline
                                venkatesh
                                wrote on last edited by
                                #15

                                The problem is it doesn't know which class i am calling, though i am creating an object of the class. It's not getting connected to the class. I am not really sure why is this happening though :(

                                1 Reply Last reply
                                0
                                • N Offline
                                  N Offline
                                  NicuPopescu
                                  wrote on last edited by
                                  #16

                                  bq. Hi,
                                  The problem is though i have created a object of the class. But i don’t know how to make the Qt understand that i am pointing to the particular class.
                                  will this help
                                  StationaryImageViewer StationaryImageViewer;

                                  ?

                                  this phrase reveals more about your c++/qt understanding: I think you are still confusing a class type with its instance (in memory created object, static or dynamic) etc. ... better for you to review some c++ stuffs ... qt does not understand anything it's a framework, you could say the compiler does

                                  1 Reply Last reply
                                  0
                                  • V Offline
                                    V Offline
                                    venkatesh
                                    wrote on last edited by
                                    #17

                                    Hmmm, my problem here is how to exactly make the compiler know that i am pointing to the specific class?

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved