Setters and getters(for using pixmap of one class in another)
-
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
-
Hi,
Did you check if the returned pixmap is null ?
Use
@bool QPixmap::isNull () const@ -
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 -
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();@
-
Or you are checking for
@OpacityViewMovingPixmap_=MovingImageViewerObject.getMovingImagePixmap();@
Try after that the null condition. -
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?
-
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.
-
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
-
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?..
-
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? -
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 -
@ StationaryImageViewer StationaryImageViewerObject;@
StationaryImageViewerObject is of type StationaryImageViewer ... how do you mean by connecting them?
-
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;
@
? -
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 :)
-
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 :(
-
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
-
Hmmm, my problem here is how to exactly make the compiler know that i am pointing to the specific class?