Page 2 of 2 FirstFirst 12
Results 21 to 34 of 34

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

  1. #21
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

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

    You are missing #include <QTimer>.

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

    mynahz (11th June 2008)

  3. #22
    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...?

    just want to find out, how do i make the picture's size to fit into the window size perfectly? I tried using maximum size(as shown) but the picture still remains at the left corner of the window...
    There are different ways depending on how you want to do it:

    If you want to make the window fit the image, then you can just fix the window size however big your image is: this->setFixedSize(QSize(int width, int height));

    Or, if you want your image to to fit the window, you can call display_image->resize(int width, int height); as QLabel inherits from QWidget and can call all those same methods...if you use this method and the QLabel is resized to the same size as the window, and yet the image itself is not that big, you would need to call display_image->setScaledContents(true) to make it stretch to fill that space.

    In Qt there are often several ways to approach things, so pick whatever suits your intention best.

    Edit: Have you been using Qt Assistant? Its a great tool for browsing the framework.
    Last edited by JimDaniel; 10th June 2008 at 15:11.

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

    mynahz (11th June 2008)

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

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

    yep, had been using QT Assistant for all the help I need. but sometimes find it hard to understand how to use certain functions...

    void QLabel::clear () [slot]
    Clears any label contents.

    when i use this here, i realise there is a problem. There's no error when compiling but when I run the program, there will be a pop-up window saying blinkingIcon has encountered a problem and it needs to close. this happens when the value of the second(from the current time) is even number. Because sometimes the program can work, giving me my picture in a window. Below is my code in .cpp. Is it because it's a [slot] and needs to be used with a connect(xxx) function? or I can use it w/o connect function (if I am using it the wrong way)?
    Thanks again in advance!

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

  6. #24
    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.

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

    mynahz (11th June 2008)

  8. #25
    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...

  9. #26
    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.

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

    mynahz (12th June 2008)

  11. #27
    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!!!

  12. #28
    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.

  13. #29
    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

  14. #30
    Join Date
    Jan 2008
    Posts
    72
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

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

    I did not get your problem ? can you clarify it...............

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

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

    Basically, I am trying to make an icon that blinks, using 2 png pictures. ( means i make them blink by switching from one picture to another). So, now my problem is to make the 2 pictures come out alternately. ie, i make one of them disappear and the other appear, and so on... I tried using the functions show() and hide() but they don't seem to work. I was thinking whether I had used them wrongly. Don't be mistaken, In the .zip file i uploaded, i put the 2 pictures side by side so that I can see whether the picture really disappear/show as expected.

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

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

    Well... the simplest (as in most trivial) possible solution is such as the following:
    Qt Code:
    1. class BlinkingLabel : public QLabel {
    2. Q_OBJECT
    3. public:
    4. BlinkingLabel(QWidget *parent=0) : QLabel(parent){}
    5. public slots:
    6. void setPixmaps(const QPixmap &px1, const QPixmap &px2){
    7. setPixmap(px1);
    8. m_px1 = px1;
    9. m_px2 = px2;
    10. }
    11. void start(int ms = 1000){
    12. m_timer = startTimer(ms);
    13. }
    14. void stop(){ killTimer(m_timer); }
    15. protected:
    16. void timerEvent(QTimerEvent *te){
    17. if(te->timerId()==m_timer){
    18. if((*pixmap())==m_px1)
    19. setPixmap(m_px2);
    20. else
    21. setPixmap(m_px1);
    22. }
    23. QLabel::timerEvent(te);
    24. }
    25. private:
    26. QPixmap m_px1, m_px2;
    27. int m_timer;
    28. };
    To copy to clipboard, switch view to plain text mode 

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

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

    Hi wysota,
    pardon me, for I dun quite get how to use your code. Do I add a Qlabel in Qt designer and promote it to your BlinkingLabel? And I have few more questions on the code... is killTimer a function from the Qt libraries? Also, what are px1 and px2, as in, where do I set my pictures into px1 and px2?

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

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

    Quote Originally Posted by mynahz View Post
    Do I add a Qlabel in Qt designer and promote it to your BlinkingLabel?
    Yes, that's one of the options, but remember that world doesn't end at Qt Designer

    is killTimer a function from the Qt libraries?
    Yes - QObject::killTimer().

    Also, what are px1 and px2, as in, where do I set my pictures into px1 and px2?
    I think the code is self explainatory, there is the setPixmaps() method you should use.

    As I said, the solution is trivial, so not everything is accessible from within Designer even if you had a full-featured plugin for it. You might extend it with making m_px1 and m_px2 accessible through properties, for example.

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.