You are missing #include <QTimer>.
Printable View
You are missing #include <QTimer>.
There are different ways depending on how you want to do it:Quote:
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...
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.
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!:)
Code:
#include "BlinkingIcon.h" { /**********************************************/ connect(timer, SIGNAL(timeout()), this, SLOT(startBlinking())); timer->start(1000); startBlinking(); /**********************************************/ setWindowTitle(tr("My Blinking Icon")); resize(50,50); } void BlinkingIcon::startBlinking() { if ((time.second() % 2) == 0) { display_image1->resize(50,50); display_image1->setScaledContents(true); display_image2->clear(); } else { //display_image1->clear(); } //} } BlinkingIcon::~BlinkingIcon(){}
Try this:
Code:
#include "BlinkingIcon.h" { setWindowTitle(tr("My Blinking Icon")); resize(50,50); display_image->setScaledContents(true); connect(timer, SIGNAL(timeout()), this, SLOT(startBlinking())); startBlinking(); timer->start(1000); } void BlinkingIcon::startBlinking() { display_image->clear(); if ((time.second() % 2) == 0) { } else { } display_image->resize(50, 50); }
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.
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? :confused:
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...
Okay, I finally whipped up a test app, and found some problems. Here is the working program:
Code:
#ifndef BLINKINGICON_H #define BLINKINGICON_H #include <QtGui> #include <QtCore> { Q_OBJECT public: virtual ~BlinkingIcon(); public slots: void startBlinking(); private: QLabel * m_display_image; }; #endif // BLINKINGICON_H #include "blinkingicon.h" { this->setWindowTitle(tr("My Blinking Icon")); this->setFixedSize(50, 50); m_display_image->setScaledContents(true); this->startBlinking(); connect(timer, SIGNAL(timeout()), this, SLOT(startBlinking())); timer->start(1000); } BlinkingIcon::~BlinkingIcon() {} void BlinkingIcon::startBlinking() { static bool on = false; m_display_image->clear(); if (on) { on = false; } else { on = true; } m_display_image->resize(50, 50); }
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...
oh yes! it works perfect! :D
Thank you, JimDaniel!!!
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... :confused:
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. :o
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.
Here it is.:D
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.
I did not get your problem ? can you clarify it...............:confused:
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.:)
Well... the simplest (as in most trivial) possible solution is such as the following:
Code:
Q_OBJECT public: public slots: setPixmap(px1); m_px1 = px1; m_px2 = px2; } void start(int ms = 1000){ m_timer = startTimer(ms); } void stop(){ killTimer(m_timer); } protected: if(te->timerId()==m_timer){ if((*pixmap())==m_px1) setPixmap(m_px2); else setPixmap(m_px1); } } private: QPixmap m_px1, m_px2; int m_timer; };
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?
Yes, that's one of the options, but remember that world doesn't end at Qt Designer :)
Yes - QObject::killTimer().Quote:
is killTimer a function from the Qt libraries?
I think the code is self explainatory, there is the setPixmaps() method you should use.Quote:
Also, what are px1 and px2, as in, where do I set my pictures into px1 and px2?
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.