2017-02-05 7 views
0

QLabelを関数から返そうとしていますが、エラー:エラー: '((const MyClass *)this) - > MyClass :: myLabel'から 'QLabel * const'を 'QLabel'に変更する

/media/root/5431214957EBF5D7/projects/c/qt/tools/plugandpaint/plugins/extrafilters/extrafiltersplugin.cpp:17: error: could not convert ‘((const ExtraFiltersPlugin*)this)->ExtraFiltersPlugin::retLabel’ from ‘QLabel* const’ to ‘QLabel’ 


       ^~~~~~~~ 

extrafiltersplugin.h

#ifndef EXTRAFILTERSPLUGIN_H 
#define EXTRAFILTERSPLUGIN_H 

#include <interfaces.h> 

#include <QObject> 
#include <QtPlugin> 
#include <QImage> 
#include <QLabel> 

class ExtraFiltersPlugin : 
     public QObject, 

     public FilterInterface, 
     public RevViewsInterface 
{ 
    Q_OBJECT 
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.Examples.PlugAndPaint.FilterInterface" FILE "extrafilters.json") 
    Q_INTERFACES(FilterInterface RevViewsInterface) 

public: 
    ExtraFiltersPlugin(); 

    // RevInterface 
    QLabel label() const override; 

private: 
    QLabel *retLabel; 
}; 

#endif 

extrafiltersplugin.cpp

#include <QtWidgets> 

#include <stdlib.h> 

#include "extrafiltersplugin.h" 

ExtraFiltersPlugin::ExtraFiltersPlugin() { 
    retLabel = new QLabel(); 
    retLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); 
    retLabel->setText("first line\nsecond line"); 
    retLabel->setAlignment(Qt::AlignBottom | Qt::AlignRight); 
} 

QLabel ExtraFiltersPlugin::label() const 
{ 
    return retLabel; 
} 

私が間違っているか紛失している可能性がありますか?これが完全に明らかであれば、私はC + +/Qt初心者です。

ありがとうございます。

+0

ポインタでないオブジェクトを返す関数からポインタ 'retLabel'を返そうとしています。 –

+0

これはすでに@ some-programmer-dudeのコメントで、以前の投稿に返答しましたhttp://stackoverflow.com/questions/42049893/error-label-was-not-declared-in-this-scope –

答えて

3

QLabel(またはQWidget派生クラスのインスタンス)は、コピーできないため、値で戻すことはできません。上記の両方が許可されていることを

あなたはそれがどちらかのポインタを返すように ExtraFiltersPlugin::labelの署名を変更する必要が

...

QLabel *ExtraFiltersPlugin::label() const 
{ 
    return retLabel; 
} 

または参照...

QLabel &ExtraFiltersPlugin::label() const 
{ 
    return *retLabel; 
} 

注意参照先を変更する呼び出し元QLabel。それが必要でない(または望ましい)場合は、戻りタイプはそれぞれconst QLabel *またはconst QLabel &である必要があります。

0

label()は、QLabelを返すためにheaderで定義されていますが、後で実装されるlabel()は、QLabelへのポインタを返します。関数であるため動作しません)

QLabel ExtraFiltersPlugin::label() const { return *retLabel; } 

ExtraFiltersPlugin ::ラベルの署名を(更新:

ソリューション:ラベルの

アップデートの実装()[cppのファイルに] QLabelオブジェクトを返しますオーバーライドとして装飾。

関連する問題