Results 1 to 4 of 4

Thread: Passing QLabel into a function as a parameter

  1. #1
    Join Date
    Jun 2008
    Posts
    23
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Windows

    Question Passing QLabel into a function as a parameter

    I am trying to 'blink' pictures by aternating between 2 QLabels. And since I will have to 'blink' many icons, I was think whether I am able to pass in QLabel like this:
    Qt Code:
    1. void Mainframe::blinkLabel1_2(QLabel* labelOne, QLabel* labelTwo)
    2. {
    3. static bool on = false;
    4.  
    5. if(on)
    6. {
    7. labelOne->show();
    8. labelTwo->hide();
    9. on = false;
    10. }
    11. else
    12. {
    13. labelTwo->show();
    14. labelOne->hide();
    15. on = true;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    but I face the following errors
    Qt Code:
    1. src\mainframe.cpp:42: error: no matching function for call to `Mainframe::blinkLabel1_2(QLabel*&, QLabel*&)'
    2. src\mainframe.h:15: note: candidates are: void Mainframe::blinkLabel1_2(QLabel, QLabel)
    3. src\mainframe.cpp: In member function `void Mainframe::on_canbus_RevBoolData(bool)':
    4. src\mainframe.cpp:85: error: no matching function for call to `Mainframe::blinkLabel1_2(QLabel*&, QLabel*&)'
    5. src\mainframe.h:15: note: candidates are: void Mainframe::blinkLabel1_2(QLabel, QLabel)
    6. src\mainframe.cpp: At global scope:
    7. src\mainframe.cpp:92: error: prototype for `void Mainframe::blinkLabel1_2(QLabel&, QLabel&)' does not match any in class `Mainframe'
    8. src\mainframe.h:15: error: candidate is: void Mainframe::blinkLabel1_2(QLabel, QLabel)
    9. src\mainframe.cpp: In member function `void Mainframe::blinkLabel1_2(QLabel&, QLabel&)':
    10. src\mainframe.cpp:97: error: base operand of `->' has non-pointer type `QLabel'
    11. src\mainframe.cpp:98: error: base operand of `->' has non-pointer type `QLabel'
    12. src\mainframe.cpp:103: error: base operand of `->' has non-pointer type `QLabel'
    13. src\mainframe.cpp:104: error: base operand of `->' has non-pointer type `QLabel'
    14. mingw32-make[1]: Leaving directory `C:/HmiProject/imitDemo1'
    15. mingw32-make[1]: *** [build\host\mainframe.o] Error 1
    16. mingw32-make: *** [release] Error 2
    To copy to clipboard, switch view to plain text mode 

    I tried changing the statement to
    void Mainframe::blinkLabel1_2(QLabel& labelOne, QLabel& labelTwo)
    void Mainframe::blinkLabel1_2(QLabel *labelOne, QLabel *labelTwo)
    void Mainframe::blinkLabel1_2(QLabel &labelOne, QLabel &labelTwo)
    but it still couldn't work.

    Appreciate if someone enlightens me on this.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing QLabel into a function as a parameter

    First of all it would be simpler if you exchanged pixmaps and not whole labels.

    Second of all, you have to show us how you call your function. It should be something like:
    Qt Code:
    1. QLabel *l1, *l2;
    2. //...
    3. blinkLabel1_2(l1, l2);
    To copy to clipboard, switch view to plain text mode 
    where
    Qt Code:
    1. void ...::blinkLabel1_2(QLabel*, QLabel*);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2008
    Posts
    23
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing QLabel into a function as a parameter

    Hi wysota!
    first of all, I use whole labels because of the convinence of setting the picture inside Qt designer.

    second, here's my code.
    where i call the function blinkLabel if I don't pass parameters.
    Qt Code:
    1. void Mainframe::on_canbus_RevBoolData(bool temp)
    2. {
    3. // TODO
    4. label->hide();
    5. label_2->hide();
    6.  
    7. if(temp)
    8. {
    9. label_2->show();
    10. label->hide();
    11. }
    12. else
    13. {
    14. /**********************************************/
    15. QTimer * timer = new QTimer(this);
    16. connect(timer, SIGNAL(timeout()), this, SLOT(blinkLabel1_2()));
    17. timer->start(1000);
    18. this->blinkLabel1_2();
    19. /**********************************************/
    20. }
    21.  
    22. }
    23.  
    24. void Mainframe::blinkLabel1_2()
    25. {
    26. static bool on = false;
    27.  
    28. if(on)
    29. {
    30. label->show();
    31. label_2->hide();
    32. on = false;
    33. }
    34. else
    35. {
    36. label_2->show();
    37. label->hide();
    38. on = true;
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 

    if I pass in parameters (from what u suggested)
    Qt Code:
    1. void Mainframe::on_canbus_RevBoolData(bool temp)
    2. {
    3. // TODO
    4. label->hide();
    5. label_2->hide();
    6.  
    7. if(temp)
    8. {
    9. label_2->show();
    10. label->hide();
    11. }
    12. else
    13. {
    14. /**********************************************/
    15. QTimer * timer = new QTimer(this);
    16. connect(timer, SIGNAL(timeout()), this, SLOT(blinkLabel1_2(label, label_2)));
    17. timer->start(1000);
    18. this->blinkLabel1_2(label, label_2);
    19. /**********************************************/
    20. }
    21.  
    22. }
    23.  
    24. void Mainframe::blinkLabel1_2(QLabel* l1, QLabel* l2)
    25. {
    26. static bool on = false;
    27.  
    28. if(on)
    29. {
    30. l1->show();
    31. l2->hide();
    32. on = false;
    33. }
    34. else
    35. {
    36. l2->show();
    37. l1->hide();
    38. on = true;
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 

    no errors encountered, but I have no idea now why it doesn't blink...

    Thanks for yr reply wysota!

  4. #4
    Join Date
    Dec 2007
    Posts
    40
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Passing QLabel into a function as a parameter

    Do you find any error printed at runtime regarding this connection? a Slot cannot have more parameters than a Signal. You have to figure out another way of passing the label1 and label2 to the slot. See the Signal Slot documentation for more information.
    Let your work talk for you

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  3. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52

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.