Unsolved Error while initializing the Object in Qt
-
Re: Calling an object from another class
I want an object of qMRMLSliceControllerWidget in qMRMLSliceWidget.
- In the header file I declared the object (
qMRMLSliceControllerWidget* scw_temp1;
) and in the .cxx file I initialized the object
(scw_temp1 = new qMRMLSliceControllerWidget(q)
).
error: - I even tried creating a static
Get()
method inqMRMLSliceControllerWidget
which return object of this class, even then I am getting errors.
- In the header file I declared the object (
-
Hi
Are you sure you are using the right types?
The first error seems to say cannot make an A into B.
SlideWidget versus SlideControllerWidgetBut for test :
if you make ain qMRMLSliceWidget.cpp
void test () {
qMRMLSliceControllerWidget test;
}will it accept it ?
Ofc with the right include etc.
if still no. Then you might have a circular include where
qMRMLSliceWidget includes qMRMLSliceControllerWidget and reverse. -
I tried using
test()
method:
I have a doubt even intest()
method we are just declaring the object right, we are not initializing. My doubt is how to initialize the object.
For accessing the widgets inqMRMLSliceControllerWidget
I will have to create its object right? or is there any other method. -
@Meera-Hadid said in Error while initializing the Object in Qt:
I tried using
test()
methodThe error message indicates that, whatever code you have written, you are attempting to access a variable named
test
when you say you declared a method namedtest()
. -
using test object i tried calling thespinBox
inqMRMLSliceControllerWidget
code:qMRMLSliceControllerWidgetPrivate* qMRMLSliceWidget::test() { qMRMLSliceControllerWidgetPrivate* test; return test; }
test_obj = qMRMLSliceWidget::test(); this->ControllerLayout = new QVBoxLayout(q); this->ControllerLayout->setSpacing(2); QMargins cmargins = this->ControllerLayout->contentsMargins(); cmargins.setTop(0); cmargins.setBottom(0); cmargins.setLeft(0); cmargins.setRight(0); this->ControllerLayout->setContentsMargins(cmargins); this->ControllerLayout->addWidget(test_obj->spinBox); q->setLayout(this->ControllerLayout);
-
Hi
we should not name both object and function test. sorry my bad, too little coffe :)
I meant
void test () { qMRMLSliceControllerWidget mySC; }
it was just to see if it does know the type without errors.
Well its just a local variable so it is initialized as such. ( even there is no new )
It seems it does know the type and does not complain about it ? , correct ?
So this seems to be fine so you should be able to create
a qMRMLSliceWidget in qMRMLSliceWidget.I think what ever the q is you are giving it
new qMRMLSliceControllerWidget(q)) <<<< i think its not what it wantshence the error in the picture
-
@Meera-Hadid
Hi
the code you just posted will crash :)
Its dangling pointer.qMRMLSliceControllerWidgetPrivate* qMRMLSliceWidget::test() {
qMRMLSliceControllerWidgetPrivate* test; <<<<< dangling pointer !
return test;
}so that will not work 100%
it must be
qMRMLSliceControllerWidgetPrivate* qMRMLSliceWidget::test() {
qMRMLSliceControllerWidgetPrivate* test = new qMRMLSliceControllerWidgetPrivate( what ever it wants)
return test;
}ps. why you go from qMRMLSliceControllerWidget to
qMRMLSliceControllerWidgetPrivate `?
the private type seems its called that as one should not use it outside class ? -
Basically the spinBox is defined in
qMRMLSliceControllerWidgetPrivate
but it is a public variable. For better understandingqMRMLSliceControllerWidget_p.h:
#ifndef __qMRMLSliceControllerWidget_p_h #define __qMRMLSliceControllerWidget_p_h // CTK includes #include <ctkPimpl.h> #include <ctkVTKObject.h> // qMRML includes #include "qMRMLSliceControllerWidget.h" #include "qMRMLViewControllerBar_p.h" #include "ui_qMRMLSliceControllerWidget.h" // MRMLLogic includes #include <vtkMRMLSliceLogic.h> // VTK includes #include <vtkAlgorithmOutput.h> #include <vtkCollection.h> #include <vtkImageData.h> #include <vtkSmartPointer.h> #include <vtkWeakPointer.h> class ctkSignalMapper; class ctkDoubleSpinBox; class ctkVTKSliceView; class QSpinBox; class qMRMLSliderWidget; class vtkMRMLSliceNode; class vtkObject; class vtkMRMLSegmentationDisplayNode; //----------------------------------------------------------------------------- struct QMRML_WIDGETS_EXPORT qMRMLOrientation { QString Prefix; QString ToolTip; }; //----------------------------------------------------------------------------- class QMRML_WIDGETS_EXPORT qMRMLSliceControllerWidgetPrivate : public qMRMLViewControllerBarPrivate , public Ui_qMRMLSliceControllerWidget { Q_OBJECT QVTK_OBJECT Q_DECLARE_PUBLIC(qMRMLSliceControllerWidget); public: typedef qMRMLSliceControllerWidgetPrivate Self; typedef qMRMLViewControllerBarPrivate Superclass; qMRMLSliceControllerWidgetPrivate(qMRMLSliceControllerWidget& object); ~qMRMLSliceControllerWidgetPrivate() override; void init() override; void setColor(QColor color) override; void setupLinkedOptionsMenu(); void setupReformatOptionsMenu(); void setupLightboxMenu(); void setupCompositingMenu(); void setupSliceSpacingMenu(); void setupSliceModelMenu(); void setupSegmentationMenu(); void setupLabelMapMenu(); void setupMoreOptionsMenu(); void setupOrientationMarkerMenu(); void setupRulerMenu(); qMRMLOrientation mrmlOrientation(const QString& name); vtkSmartPointer<vtkCollection> saveNodesForUndo(const QString& nodeTypes); void enableLayerWidgets(); vtkMRMLSliceLogic* compositeNodeLogic(vtkMRMLSliceCompositeNode* node); vtkMRMLSliceLogic* sliceNodeLogic(vtkMRMLSliceNode* node); void setForegroundInterpolation(vtkMRMLSliceLogic* logic, bool interpolate); void setBackgroundInterpolation(vtkMRMLSliceLogic* logic, bool interpolate); /// Create a list of orientation containing the regular presets and also /// the "Reformat" string if sliceToRAS is different one of the preset. static void updateSliceOrientationSelector( vtkMRMLSliceNode* sliceNode, QComboBox *sliceOrientationSelector); //static qMRMLSliceControllerWidgetPrivate* Get(); public slots: /// Update widget state when the scene is modified void updateFromMRMLScene(); /// Update widget state using the associated MRML slice node void updateWidgetFromMRMLSliceNode(); /// Update widget state using the associated MRML slice composite node void updateWidgetFromMRMLSliceCompositeNode(); /// Called after a foreground layer volume node is selected /// using the associated qMRMLNodeComboBox void onForegroundLayerNodeSelected(vtkMRMLNode* node); /// Called after a background layer volume node is selected /// using the associated qMRMLNodeComboBox void onBackgroundLayerNodeSelected(vtkMRMLNode* node); /// Called after a label layer volume node is selected /// using the associated qMRMLNodeComboBox void onLabelMapNodeSelected(vtkMRMLNode* node); /// Called after a segmentation node is selected in the combobox void onSegmentationNodeSelected(vtkMRMLNode* node); /// Called after the currently selected segmentation node's display /// option is modified void onSegmentationNodeDisplayModifiedEvent(vtkObject* nodeObject); /// Called when segment visibility is changed from the segment combobox void onSegmentVisibilitySelectionChanged(QStringList selectedSegmentIDs); /// Update segmentation outline/fill button void updateSegmentationOutlineFillButton(); /// Utility function to get the display node of the current segmentation vtkMRMLSegmentationDisplayNode* currentSegmentationDisplayNode(); void updateFromForegroundDisplayNode(vtkObject* displayNode); void updateFromBackgroundDisplayNode(vtkObject* displayNode); void updateFromForegroundVolumeNode(vtkObject* volumeNode); void updateFromBackgroundVolumeNode(vtkObject* volumeNode); /// Called after the SliceLogic is modified void onSliceLogicModifiedEvent(); void applyCustomLightbox(); protected: void setupPopupUi() override; virtual void setMRMLSliceNodeInternal(vtkMRMLSliceNode* sliceNode); void setMRMLSliceCompositeNodeInternal(vtkMRMLSliceCompositeNode* sliceComposite); public: vtkMRMLSliceNode* MRMLSliceNode; vtkMRMLSliceCompositeNode* MRMLSliceCompositeNode; vtkSmartPointer<vtkMRMLSliceLogic> SliceLogic; vtkCollection* SliceLogics; vtkWeakPointer<vtkAlgorithmOutput> ImageDataConnection; QHash<QString, qMRMLOrientation> SliceOrientationToDescription; QString SliceViewName; QButtonGroup* ControllerButtonGroup; QToolButton* FitToWindowToolButton; qMRMLSliderWidget* SliceOffsetSlider; ctkDoubleSpinBox* spinBox; double LastLabelMapOpacity; double LastForegroundOpacity; double LastBackgroundOpacity; QMenu* LightboxMenu; QMenu* CompositingMenu; QMenu* SliceSpacingMenu; QMenu* SliceModelMenu; QMenu* SegmentationMenu; QMenu* LabelMapMenu; QMenu* OrientationMarkerMenu; QMenu* RulerMenu; ctkDoubleSpinBox* SliceSpacingSpinBox; ctkDoubleSpinBox* SliceFOVSpinBox; QSpinBox* LightBoxRowsSpinBox; QSpinBox* LightBoxColumnsSpinBox; ctkDoubleSpinBox* SliceModelFOVXSpinBox; ctkDoubleSpinBox* SliceModelFOVYSpinBox; ctkDoubleSpinBox* SliceModelOriginXSpinBox; ctkDoubleSpinBox* SliceModelOriginYSpinBox; QSpinBox* SliceModelDimensionXSpinBox; QSpinBox* SliceModelDimensionYSpinBox; QSize ViewSize; ctkSignalMapper* OrientationMarkerTypesMapper; ctkSignalMapper* OrientationMarkerSizesMapper; ctkSignalMapper* RulerTypesMapper; ctkSignalMapper* RulerColorMapper; }; #endif
-
qMRMLSliceControllerWidget.cxx:
void qMRMLSliceControllerWidgetPrivate::init() { Q_Q(qMRMLSliceControllerWidget); this->Superclass::init(); this->FitToWindowToolButton = new QToolButton(q); this->FitToWindowToolButton->setObjectName("FitToWindowToolButton"); //this->FitToWindowToolButton->setToolTip(tr("Adjust the Slice Viewer's field of view to match the extent of lowest non-None volume layer (bg, then fg, then label).")); //QIcon fitToWindowIcon(":/Icons/SlicesFitToWindow.png"); //this->FitToWindowToolButton->setIcon(fitToWindowIcon); this->FitToWindowToolButton->setAutoRaise(true); this->FitToWindowToolButton->setDefaultAction(this->actionFit_to_window); this->FitToWindowToolButton->setFixedSize(15, 15); this->BarLayout->insertWidget(2, this->FitToWindowToolButton); //Hide FitToWindowToolButton from the Barlayout this->FitToWindowToolButton->hide(); this->SliceOffsetSlider = new qMRMLSliderWidget(q); this->SliceOffsetSlider->setObjectName("SliceOffsetSlider"); this->SliceOffsetSlider->setTracking(false); this->SliceOffsetSlider->setToolTip(qMRMLSliceControllerWidget::tr("Slice distance from RAS origin")); this->SliceOffsetSlider->setQuantity("length"); this->SliceOffsetSlider->setUnitAwareProperties( qMRMLSliderWidget::Suffix|qMRMLSliderWidget::Precision|qMRMLSliderWidget::Scaling); this->SliceOffsetSlider->spinBox()->setDecimalsOption( ctkDoubleSpinBox::DecimalsByShortcuts | ctkDoubleSpinBox::DecimalsByKey | ctkDoubleSpinBox::DecimalsAsMin ); //this->SliceOffsetSlider->spinBox()->setParent(this->PopupWidget); spinBox = this->SliceOffsetSlider->spinBox(); spinBox->setFrame(false); spinBox->spinBox()->setButtonSymbols(QAbstractSpinBox::NoButtons); spinBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); int targetHeight = spinBox->parentWidget()->layout()->sizeHint().height();//setSizeConstraint(QLayout::SetMinimumSize); int fontHeight = spinBox->fontMetrics().height(); qreal heightRatio = static_cast<qreal>(targetHeight - 2) / fontHeight; if (heightRatio < 1.) { QFont stretchedFont(spinBox->font()); stretchedFont.setPointSizeF(stretchedFont.pointSizeF() * heightRatio); spinBox->setFont(stretchedFont); } // Connect Slice offset slider this->connect(this->SliceOffsetSlider, SIGNAL(valueChanged(double)), q, SLOT(setSliceOffsetValue(double)), Qt::QueuedConnection); this->connect(this->SliceOffsetSlider, SIGNAL(valueIsChanging(double)), q, SLOT(trackSliceOffsetValue(double)), Qt::QueuedConnection); this->connect(q, SIGNAL(mrmlSceneChanged(vtkMRMLScene*)), this->SliceOffsetSlider, SLOT(setMRMLScene(vtkMRMLScene*))); this->BarLayout->addWidget(this->SliceOffsetSlider); this->SliceOffsetSlider->hide(); this->BarLayout->addWidget(this->spinBox); //spinBox->hide(); // Move the spinbox in the popup instead of having it in the slider bar //dynamic_cast<QGridLayout*>(this->PopupWidget->layout())->addWidget( // this->SliceOffsetSlider->spinBox(), 0, 0, 1, 2); // Hide all buttons by default this->MoreButton->setChecked(false); vtkNew<vtkMRMLSliceLogic> defaultLogic; q->setSliceLogic(defaultLogic.GetPointer()); q->setSliceViewName("Red"); }
i want to use this spinBox in qMRMLSliceWidget
-
and the qMRMLSliceControllerWidget constructor is
qMRMLSliceControllerWidgetPrivate::qMRMLSliceControllerWidgetPrivate(qMRMLSliceControllerWidget& object) : Superclass(object) { this->SliceLogic = nullptr; this->MRMLSliceNode = nullptr; this->MRMLSliceCompositeNode = nullptr; this->SliceLogics = nullptr; this->ControllerButtonGroup = nullptr; qMRMLOrientation axialOrientation = {qMRMLSliceControllerWidget::tr("Axial:"), qMRMLSliceControllerWidget::tr("I <-----> S")}; qMRMLOrientation sagittalOrientation = {qMRMLSliceControllerWidget::tr("Sagittal:"), qMRMLSliceControllerWidget::tr("L <-----> R")}; qMRMLOrientation coronalOrientation = {qMRMLSliceControllerWidget::tr("Coronal:"), qMRMLSliceControllerWidget::tr("P <-----> A")}; qMRMLOrientation obliqueOrientation = {"", qMRMLSliceControllerWidget::tr("Oblique")}; this->SliceOrientationToDescription["Axial"] = axialOrientation; this->SliceOrientationToDescription["Sagittal"] = sagittalOrientation; this->SliceOrientationToDescription["Coronal"] = coronalOrientation; this->SliceOrientationToDescription["Reformat"] = obliqueOrientation; this->LastLabelMapOpacity = 1.; this->LastForegroundOpacity = 1.; this->LastBackgroundOpacity = 1.; this->FitToWindowToolButton = nullptr; this->SliceOffsetSlider = nullptr; this->spinBox = nullptr; this->LightboxMenu = nullptr; this->CompositingMenu = nullptr; this->SliceSpacingMenu = nullptr; this->SliceModelMenu = nullptr; this->SegmentationMenu = nullptr; this->LabelMapMenu = nullptr; this->OrientationMarkerMenu = nullptr; this->RulerMenu = nullptr; this->SliceSpacingSpinBox = nullptr; this->SliceFOVSpinBox = nullptr; this->LightBoxRowsSpinBox = nullptr; this->LightBoxColumnsSpinBox = nullptr; this->SliceModelFOVXSpinBox = nullptr; this->SliceModelFOVYSpinBox = nullptr; this->SliceModelOriginXSpinBox = nullptr; this->SliceModelOriginYSpinBox = nullptr; this->SliceModelDimensionXSpinBox = nullptr; this->SliceModelDimensionYSpinBox = nullptr; }
i want to place this spinBox in qMRMLSliceWidget, is there any method for doing this?
-
@Meera-Hadid
Do you mean its
ctkDoubleSpinBox* spinBox; ?well its public so i guess you can.
it sems to be allocated / assigned in
qMRMLSliceControllerWidgetPrivate::init()
....
spinBox = this->SliceOffsetSlider->spinBox();but im still not sure what that "q" is that it seems to use for constructor.
-
I Have a QToolButton in the first UI I don't want to display it over there but the functionalities/actions of the button are initialized in the first.cxx file. So when I create an object of the first class in the second class and try adding the button in a widget of the second class does the button get transferred?
QToolButton code in qMRMLSLiceControllerWidget.cxx:
this->FitToWindowToolButton = new QToolButton(q); this->FitToWindowToolButton->setObjectName("FitToWindowToolButton"); this->FitToWindowToolButton->setAutoRaise(true); this->FitToWindowToolButton->setDefaultAction(this->actionFit_to_window); this->FitToWindowToolButton->setFixedSize(15, 15); QObject::connect(this->actionFit_to_window, SIGNAL(triggered()), q, SLOT(fitSliceToBackground()));
Object of qMRMLSLiceControllerWidget in qMRMLSliceWidget: initialization in qMRMLSliceWidget_p.h:
qMRMLSliceControllerWidget* scw;
declaration in qMRMLSliceWidget.cxx:
scw = new qMRMLSliceControllerWidget(q);
when I create an object like this in my App I don't know for what reason 0.00 is getting displayed
-
Thank you and everyone, I also had the same problem.
Thanks