PDA

View Full Version : no matching function to call for to in custom Label



joshy198
22nd September 2012, 19:25
Constructor:

in mainwindow.h




...
private:
myLabel *l;
.....


in mainwindow.cpp


...
QString s = "l"+QString::number(i+1)+".png";
l = new myLabel();
l->setBackgroundRole(QPalette::Dark);
l->setAutoFillBackground(true);
image = new QImage(s);//Muss im debug-Ordner sein!!!
l->setPixmap(QPixmap::fromImage(*image));
l->setMaximumHeight(image->height());
l->setMaximumWidth(image->width());
l->setInfo(i+1);
...

--------------------------------------------------------------------
myLabel.h


#ifndef MYLABEL_H
#define MYLABEL_H

class myLabel : public QLabel
{
Q_OBJECT
public:
myLabel( const QString & text, QWidget * parent = 0 );
~myLabel(){}
void setInfo(int i);
int getInfo();
private:
int zahl;

signals:
void clicked();

public slots:
void slotClicked();

protected:
void mousePressEvent ( QMouseEvent * event ) ;

};
#endif


myLabel.cpp


#include "mylabel.h"

myLabel::myLabel( const QString & text, QWidget * parent = 0 )
:QLabel(parent)
{
connect( this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) );
}

void myLabel::slotClicked()
{
qDebug()<<"Clicked";
}

void myLabel::mousePressEvent ( QMouseEvent * event )
{
emit clicked();
}
void myLabel::setInfo(int i)
{
zahl=i;
}
int myLabel::getInfo()
{
return zahl;
}



The Error is:
no matching function for call to 'myLabel::myLabel()'
candidates are: myLabel::myLabel(const QString&,QWidget*)
note: myLabel::myLabel(const myLabel&)

spirit
22nd September 2012, 20:26
You don't have default ctor for myLabel.

Path a string here


...
l = new myLabel(); //<--- pass a string into ctor
...

or make default ctor:


...
myLabel( const QString & text = QString(), QWidget * parent = 0 );
...

joshy198
23rd September 2012, 11:44
I don't really know how to do it the way you suggested

Zlatomir
23rd September 2012, 12:09
spirit meant the code: l = new myLabel(); must be something like: l = new myLabel("A String is needed to construct your label");
And also this code should include QLabel header:


#ifndef MYLABEL_H
#define MYLABEL_H

#include <QLabel>
//you need header in order to inherit from QLabel

class myLabel : public QLabel
{//...

And also when you define the constructor you might want to pass the QString to the QLabel constructor too:


myLabel::myLabel( const QString & text, QWidget * parent = 0 )
:QLabel(text, parent) //here pass the text too
{

joshy198
23rd September 2012, 15:05
Changed it the way you said, even more errors ....
constructor


...
l = new myLabel("label");
...


mylabel.h


#ifndef MYLABEL_H
#define MYLABEL_H
#include "QLabel"

class myLabel : public QLabel
{
Q_OBJECT
public:
myLabel( const QString & text, QWidget * parent = 0 );
~myLabel(){}
void setInfo(int i);
int getInfo();
private:
int zahl;

signals:
void clicked();

public slots:
void slotClicked();

protected:
void mousePressEvent ( QMouseEvent * event ) ;

};
#endif



mylabel.cpp



#include "mylabel.h"

myLabel::myLabel( const QString & text, QWidget * parent = 0 )
:QLabel(text,parent)
{
connect( this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) );
}

void myLabel::slotClicked()
{
qDebug()<<"Clicked";
}

void myLabel::mousePressEvent ( QMouseEvent * event )
{
emit clicked();
}
void myLabel::setInfo(int i)
{
zahl=i;
}
int myLabel::getInfo()
{
return zahl;
}



ERRORS:
default argument given for parameter 2 of myLabel::myLabel(const QString&, QWidget*)
after previous specification in 'myLabel(const QString&,QWidget*)'
In memberfunction myLabel::slotClicked()':
invalid use of type 'struct QDebug'
at global scope
inline function QDebug qDebug()'used but never defined

spirit
23rd September 2012, 15:08
First, inclusion should be #include <QLabel> not #include "QLabel".
Second, include QDebug as #include <QDebug> to use qDebug()<<"Clicked";.

joshy198
23rd September 2012, 16:44
Thanks, but still 2 errors left :(
Errors:
default argument given for parameter 2 of 'myLabel(const QString&,QWidget*)'
after previous specification in 'myLabel::myLabel(const QString&,QWidget*)'

Sorry for beeing that irritating

Zlatomir
23rd September 2012, 16:53
Don't put the = 0 (the default parameter) both in declaration and definition (both in .cpp and in .h file), put it only on the declaration (in .h file).
LE: a little bit clearer:

myLabel::myLabel( const QString & text, QWidget * parent) //remove =0 from here, leave it only in the .h file
:QLabel(text, parent) //here pass the text too
{