Results 1 to 8 of 8

Thread: no matching function to call for to in custom Label

  1. #1
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Exclamation no matching function to call for to in custom Label

    Constructor:

    in mainwindow.h

    Qt Code:
    1. ...
    2. private:
    3. myLabel *l;
    4. .....
    To copy to clipboard, switch view to plain text mode 

    in mainwindow.cpp
    Qt Code:
    1. ...
    2. QString s = "l"+QString::number(i+1)+".png";
    3. l = new myLabel();
    4. l->setBackgroundRole(QPalette::Dark);
    5. l->setAutoFillBackground(true);
    6. image = new QImage(s);//Muss im debug-Ordner sein!!!
    7. l->setPixmap(QPixmap::fromImage(*image));
    8. l->setMaximumHeight(image->height());
    9. l->setMaximumWidth(image->width());
    10. l->setInfo(i+1);
    11. ...
    To copy to clipboard, switch view to plain text mode 
    --------------------------------------------------------------------
    myLabel.h
    Qt Code:
    1. #ifndef MYLABEL_H
    2. #define MYLABEL_H
    3.  
    4. class myLabel : public QLabel
    5. {
    6. Q_OBJECT
    7. public:
    8. myLabel( const QString & text, QWidget * parent = 0 );
    9. ~myLabel(){}
    10. void setInfo(int i);
    11. int getInfo();
    12. private:
    13. int zahl;
    14.  
    15. signals:
    16. void clicked();
    17.  
    18. public slots:
    19. void slotClicked();
    20.  
    21. protected:
    22. void mousePressEvent ( QMouseEvent * event ) ;
    23.  
    24. };
    25. #endif
    To copy to clipboard, switch view to plain text mode 
    myLabel.cpp
    Qt Code:
    1. #include "mylabel.h"
    2.  
    3. myLabel::myLabel( const QString & text, QWidget * parent = 0 )
    4. :QLabel(parent)
    5. {
    6. connect( this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) );
    7. }
    8.  
    9. void myLabel::slotClicked()
    10. {
    11. qDebug()<<"Clicked";
    12. }
    13.  
    14. void myLabel::mousePressEvent ( QMouseEvent * event )
    15. {
    16. emit clicked();
    17. }
    18. void myLabel::setInfo(int i)
    19. {
    20. zahl=i;
    21. }
    22. int myLabel::getInfo()
    23. {
    24. return zahl;
    25. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: no matching function to call for to in custom Label

    You don't have default ctor for myLabel.

    Path a string here
    Qt Code:
    1. ...
    2. l = new myLabel(); //<--- pass a string into ctor
    3. ...
    To copy to clipboard, switch view to plain text mode 
    or make default ctor:
    Qt Code:
    1. ...
    2. myLabel( const QString & text = QString(), QWidget * parent = 0 );
    3. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: no matching function to call for to in custom Label

    I don't really know how to do it the way you suggested

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: no matching function to call for to in custom Label

    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:
    Qt Code:
    1. #ifndef MYLABEL_H
    2. #define MYLABEL_H
    3.  
    4. #include <QLabel>
    5. //you need header in order to inherit from QLabel
    6.  
    7. class myLabel : public QLabel
    8. {//...
    To copy to clipboard, switch view to plain text mode 
    And also when you define the constructor you might want to pass the QString to the QLabel constructor too:
    Qt Code:
    1. myLabel::myLabel( const QString & text, QWidget * parent = 0 )
    2. :QLabel(text, parent) //here pass the text too
    3. {
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: no matching function to call for to in custom Label

    Changed it the way you said, even more errors ....
    constructor
    Qt Code:
    1. ...
    2. l = new myLabel("label");
    3. ...
    To copy to clipboard, switch view to plain text mode 

    mylabel.h
    Qt Code:
    1. #ifndef MYLABEL_H
    2. #define MYLABEL_H
    3. #include "QLabel"
    4.  
    5. class myLabel : public QLabel
    6. {
    7. Q_OBJECT
    8. public:
    9. myLabel( const QString & text, QWidget * parent = 0 );
    10. ~myLabel(){}
    11. void setInfo(int i);
    12. int getInfo();
    13. private:
    14. int zahl;
    15.  
    16. signals:
    17. void clicked();
    18.  
    19. public slots:
    20. void slotClicked();
    21.  
    22. protected:
    23. void mousePressEvent ( QMouseEvent * event ) ;
    24.  
    25. };
    26. #endif
    To copy to clipboard, switch view to plain text mode 

    mylabel.cpp

    Qt Code:
    1. #include "mylabel.h"
    2.  
    3. myLabel::myLabel( const QString & text, QWidget * parent = 0 )
    4. :QLabel(text,parent)
    5. {
    6. connect( this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) );
    7. }
    8.  
    9. void myLabel::slotClicked()
    10. {
    11. qDebug()<<"Clicked";
    12. }
    13.  
    14. void myLabel::mousePressEvent ( QMouseEvent * event )
    15. {
    16. emit clicked();
    17. }
    18. void myLabel::setInfo(int i)
    19. {
    20. zahl=i;
    21. }
    22. int myLabel::getInfo()
    23. {
    24. return zahl;
    25. }
    To copy to clipboard, switch view to plain text mode 

    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

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: no matching function to call for to in custom Label

    First, inclusion should be #include <QLabel> not #include "QLabel".
    Second, include QDebug as #include <QDebug> to use qDebug()<<"Clicked";.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: no matching function to call for to in custom Label

    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

  8. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: no matching function to call for to in custom Label

    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:
    Qt Code:
    1. myLabel::myLabel( const QString & text, QWidget * parent) //remove =0 from here, leave it only in the .h file
    2. :QLabel(text, parent) //here pass the text too
    3. {
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to Zlatomir for this useful post:

    joshy198 (23rd September 2012)

Similar Threads

  1. error: no matching function for call to ‘QTimer:
    By saman_artorious in forum Qt Programming
    Replies: 2
    Last Post: 21st May 2012, 15:27
  2. Replies: 1
    Last Post: 19th March 2011, 10:57
  3. about no matching function for call to
    By Alain Delon in forum General Programming
    Replies: 1
    Last Post: 5th March 2011, 21:30
  4. no matching function for call to setupUi
    By ctote in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2010, 15:20
  5. No Matching function to call...
    By weepdoo in forum Qt Programming
    Replies: 2
    Last Post: 7th November 2008, 17:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.