Results 1 to 20 of 34

Thread: make a .jpg or .bmp picture into a widget...?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: make a .jpg or .bmp picture into a widget...?

    Try this:

    Qt Code:
    1. #include "BlinkingIcon.h"
    2.  
    3. BlinkingIcon::BlinkingIcon(QWidget * parent) : QWidget(parent)
    4. {
    5. setWindowTitle(tr("My Blinking Icon"));
    6. resize(50,50);
    7.  
    8. display_image = new QLabel(this);
    9. display_image->setScaledContents(true);
    10.  
    11. QTimer *timer = new QTimer(this);
    12. connect(timer, SIGNAL(timeout()), this, SLOT(startBlinking()));
    13. startBlinking();
    14. timer->start(1000);
    15. }
    16.  
    17. void BlinkingIcon::startBlinking()
    18. {
    19. display_image->clear();
    20.  
    21. QTime time = QTime::currentTime();
    22. if ((time.second() % 2) == 0)
    23. {
    24. display_image->setPixmap(QPixmap("C:/my_first_image.png"));
    25. }
    26. else
    27. {
    28. display_image->setPixmap(QPixmap("C:/my_second_image.png"));
    29. }
    30.  
    31. display_image->resize(50, 50);
    32. }
    To copy to clipboard, switch view to plain text mode 

    Althought I haven't compiled your code, I think you're crashing because each timeout you were creating new objects without deleting the previous object.
    Last edited by JimDaniel; 11th June 2008 at 05:57.

  2. The following user says thank you to JimDaniel for this useful post:

    mynahz (11th June 2008)

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

    Default Re: make a .jpg or .bmp picture into a widget...?

    Hello JimDaniel! Thanks for the code! it does not crash anymore... but... it doesn't blink... It will stay at the picture without changing to the other one...
    is it because we are dealing with pixmap and we should use this?

    void QPixmapCache::clear () [static]
    Removes all pixmaps from the cache.

    because i searched for "clear" in Assistant and this seem to be somehow close related...

  4. #3
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: make a .jpg or .bmp picture into a widget...?

    Okay, I finally whipped up a test app, and found some problems. Here is the working program:

    Qt Code:
    1. #ifndef BLINKINGICON_H
    2. #define BLINKINGICON_H
    3.  
    4. #include <QtGui>
    5. #include <QtCore>
    6.  
    7. class BlinkingIcon : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. BlinkingIcon(QWidget * parent = 0);
    13. virtual ~BlinkingIcon();
    14.  
    15. public slots:
    16. void startBlinking();
    17.  
    18. private:
    19. QLabel * m_display_image;
    20. };
    21.  
    22. #endif // BLINKINGICON_H
    23.  
    24.  
    25. #include "blinkingicon.h"
    26.  
    27. BlinkingIcon::BlinkingIcon(QWidget * parent) : QWidget(parent)
    28. {
    29. this->setWindowTitle(tr("My Blinking Icon"));
    30. this->setFixedSize(50, 50);
    31.  
    32. m_display_image = new QLabel(this);
    33. m_display_image->setScaledContents(true);
    34.  
    35. this->startBlinking();
    36.  
    37. QTimer * timer = new QTimer(this);
    38. connect(timer, SIGNAL(timeout()), this, SLOT(startBlinking()));
    39. timer->start(1000);
    40. }
    41.  
    42. BlinkingIcon::~BlinkingIcon()
    43. {}
    44.  
    45. void BlinkingIcon::startBlinking()
    46. {
    47. static bool on = false;
    48.  
    49. m_display_image->clear();
    50.  
    51. if (on)
    52. {
    53. m_display_image->setPixmap(QPixmap("C:/my_second_image.png"));
    54. on = false;
    55. }
    56. else
    57. {
    58. m_display_image->setPixmap(QPixmap("C:/my_first_image.png"));
    59. on = true;
    60. }
    61.  
    62. m_display_image->resize(50, 50);
    63. }
    To copy to clipboard, switch view to plain text mode 

    The first problem was that in your header file, you need to put your slot methods under:

    public slots:

    (or protected/private, depending who you want to be able to call)

    After I changed that it worked fine, although sometimes the timer.second() test you were doing would not work as expected, so I changed the test to what you see now...
    Last edited by JimDaniel; 11th June 2008 at 15:28.

  5. The following user says thank you to JimDaniel for this useful post:

    mynahz (12th June 2008)

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

    Thumbs up Re: make a .jpg or .bmp picture into a widget...?

    oh yes! it works perfect!
    Thank you, JimDaniel!!!

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

    Default Re: make a .jpg or .bmp picture into a widget...?

    Hi to all once again... I have a question related to this again... I have another way, of putting the picture(.png) into the QLabel within QT designer, but I am having problems making the image show/hide when I want them to. Do I use the functions from QWidget, show() and hide()? If so, then I have no idea why it doesn't hide my label when i call the hide function...
    I will try to upload my project file soon just clarify how I get my picture up into QT, as currently I can only compress it into .rar file.
    Also, just want to find out, anyone using canbus in QT? I am currently working on a project to configure a display for a vehicle and I have problems getting candata from the canbus and processing the candata such that my icon will blink when specific candata are received...

    Thanks in advance to all.

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

    Default Re: make a .jpg or .bmp picture into a widget...?

    Here it is.
    Take a look, I have tried to show/hide the icons using the timer (same mtd mentioned above.) but the functions show() / hide() doesn't seem to work.
    Attached Files Attached Files

Similar Threads

  1. Compiling with Qmake/Make
    By VireX in forum Newbie
    Replies: 25
    Last Post: 22nd February 2007, 05:57

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
  •  
Qt is a trademark of The Qt Company.