PDA

View Full Version : make a .jpg or .bmp picture into a widget...?



mynahz
4th June 2008, 03:00
Hi all, this is my first post here... :)
I have a question, how do we load a picture file into a widget? My ultimate aim is to enable an icon to blink when there is positive integer data input... else the icon should be in disabled mode. How can I load my pictures into widgets without actually drawing it out using code?
Also, to enable the blinking of the icon, the input data should fufill the condtion of being a positive integer. So is one of the way to do it is, create a widget with a conditional slot?

Many Thanks in advance. :)

JimDaniel
4th June 2008, 03:45
I'm not entirely sure what you're going for, but from what you wrote, I think your best bet will be to subclass whatever widget you want and implement the extra functionality you need, like if you're wanting an icon to have different states than the norm.

mynahz
4th June 2008, 05:41
subclass widgets that I want...? hmm.. sorry i don't quite get what you mean. Can you elaborate?
As for myself, let me explain myself clearer... :) apologies if I am still not clear enough as I am new to QT and sometimes I am not sure what terms are used. :confused:
Currently I am trying to load my picture file (.jpg or .bmp) and make it into a widget. I was thinking of inheriting from the movie widget but still i am stuck with the question as to how to upload my file and make it a widget... :o

JimDaniel
4th June 2008, 06:11
Okay, if you're wanting a class to hold an image file, lookup QPixmap or QImage. If you're wanting to display an image file on screen, lookup QLabel.

aamer4yu
4th June 2008, 06:16
For blinking an icon, u can use a QTimer.
on the timeout event, check the current state of the icon and change the bitmap/png accordingly.

mynahz
5th June 2008, 05:17
Hi JimDaniel, I have looked through the QPixmap and QImage, but I have a problem though, for I have no idea how to use the functions to hold a image file. Do I use the following function?:confused:

QPixmap ( const QString & fileName, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )

Is it possible to show me a simple example of showing a picture using QPixmap?:o

Hi aamer4yu,

For blinking an icon, u can use a QTimer.
on the timeout event, check the current state of the icon and change the bitmap/png accordingly.

Do you mean to code in the QTimer within the code, or I am able to create a Picture Widget with a slot to connect to in the QT Designer?

Many thanks. :)

aamer4yu
5th June 2008, 09:54
something like -


connect(m_timer,timeout(),this, onTimer());
if(input.isNegative())
{
___startBlinking();
}

void startBlinking()
{
m_timer.start();
}

void onTimer()
{ iconNumber = (iconNumber +1) % 2;
if(iconNumber)
_____m_label->setIcon(icon1);
else
_____m_label->setIcon(icon2);
}


hope u get the idea :)

JimDaniel
5th June 2008, 15:43
Hi JimDaniel, I have looked through the QPixmap and QImage, but I have a problem though, for I have no idea how to use the functions to hold a image file. Do I use the following function?

QPixmap ( const QString & fileName, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )

Is it possible to show me a simple example of showing a picture using QPixmap?:o



This is the constructor, and yes, as you can see, the first parameter takes in a string filename for the pixmap. See below for an example.

Keep in mind, you can use QPixmap to store an image file, but if you want to display an image on screen, you need to use QLabel. But to show you a quick example.

QPixmap:



QPixmap pixmap("c:/my_image.jpg");

//or

QPixmap pixmap;
pixmap.load("c:/my_image.jpg");


QLabel:



QLabel label("c:/my_image.jpg");

//or

QLabel label;
label.setPixmap("c:/my_image.jpg");

//or more often

QLabel * label = new QLabel("c:/my_image.jpg");

mynahz
6th June 2008, 04:18
Hi aamer4yu,
looking at your example, I think I get the idea already. Will work on it after i get the picture up. :D Thanks thanks!!

Hi JimDaniel,
thanks for the examples! but... I tried this and i get an empty window...


/*************main.cpp****************/
#include <QApplication>

#include "blinkingicon.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
blinkingicon picIcon;
picIcon.show();
return app.exec();
}

/*************BlinkingIcon.h*******************/
#ifndef blinkingicon_H
#define blinkingicon_H

#include <QWidget>

class blinkingicon : public QWidget
{
Q_OBJECT

public:
blinkingicon(QWidget *parent = 0);
void displayIcon();
//protected:
};

#endif


/*************BlinkingIcon.cpp*****************/

#include <QtGui>

#include "blinkingicon.h"

blinkingicon::blinkingicon(QWidget *parent)
: QWidget(parent)
{
setWindowTitle(tr("My Pic"));
resize(200, 200);
//Doesn't show even if I put this here...
//QPixmap pixmap("C:/HmiProject/blinkingIcon/sprites/ship/ship0000.png");
}

void blinkingicon::displayIcon()
{
QPixmap pixmap;
pixmap.load("C:/HmiProject/blinkingIcon/sprites/ship/ship0000.png");
}

Any idea what went wrong?:crying:

JimDaniel
6th June 2008, 05:44
Yes, there are a few problems. QPixmap has no on-screen representation. It is purely used for holding and manipulating an image file. Also, when you create a pixmap like this,

QPixmap pixmap("c:/my_image.png");

or

QPixmap pixmap;
pixmap.load("c:/my_image.png");

It only has local scope, when the function where its declared ends, the pixmap is destroyed, which does you no good for your intention, besides the fact that a QPixmap is the wrong object to create. That is why you use (class-scope) pointers to objects created on the heap with "new". (See my example below)

To accomplish what you want to do here, you need to use a QLabel. Something like this should get you started.




#ifndef _BLINKING_ICON_
#define _BLINKING_ICON_

#include <QWidget>
#include <QLabel>

class BlinkingIcon : public QWidget
{
Q_OBJECT

public:
BlinkingIcon(QWidget * parent = 0);
virtual ~BlinkingIcon();

private:
QLabel * display_image;
}

#endif

BlinkingIcon::BlinkingIcon(QWidget * parent) : QWidget(parent)
{
this->setWindowTitle(tr("My Blinking Icon"));
this->resize(200, 200);

display_image = new QLabel(this);
display_image->setPixmap(QPixmap("c:/my_image.png"));
display_image->adjustSize();
}

BlinkingIcon::~BlinkingIcon(){}



For your purposes, you'll probably also want to research QLayouts...

wysota
6th June 2008, 08:55
Maybe you should also consider a simpler approach?

For example you can use a checkbox and style it with stylesheets, like shown here:
http://doc.trolltech.com/latest/stylesheet-examples.html#customizing-qcheckbox

You can do it with a radio button too. Just make sure those buttons are not clickable.

Using QLabel with QMovie is also a possible solution.

mynahz
8th June 2008, 16:27
Hi JimDaniel,
I have tried using your example but why do I get errors like this?:confused:

src\blinkingicon.cpp:4: error: new types may not be defined in a return type
src\blinkingicon.cpp:4: note: (perhaps a semicolon is missing after the definition of `BlinkingIcon')
src\blinkingicon.cpp:4: error: return type specification for constructor invalid
src\blinkingicon.cpp:10:35: warning: unknown escape sequence '\H'
mingw32-make[1]: *** [build\host\blinkingicon.o] Error 1
mingw32-make: *** [release] Error 2

The actual code i used is exactly the same as what you have given me, only difference is just that i split it into a .h and a .cpp... like this...


#ifndef __BLINKINGICON_H__
#define __BLINKINGICON_H__

#include <QWidget>
#include <QLabel>

class BlinkingIcon : public QWidget
{
Q_OBJECT

public:
BlinkingIcon(QWidget * parent = 0);
virtual ~BlinkingIcon();

private:
QLabel * display_image;
}


#endif // __BLINKINGICON_H__


#include "blinkingicon.h"

BlinkingIcon::BlinkingIcon(QWidget * parent)
: QWidget(parent)
{
this->setWindowTitle(tr("My Blinking Icon"));
this->resize(200, 200);

display_image = new QLabel(this);
display_image->setPixmap(QPixmap("C:\HmiProject\test\nyjc.bmp"));
display_image->adjustSize();
}

BlinkingIcon::~BlinkingIcon(){}

Once again, thanks for your patient help given. :)

Hi wysota,
pardon me for my ignorance, but what does it mean by "The main difference is that a tristate QCheckBox has an indeterminate state." from the website? Is the example using an existing class QCheckBox, and giving it more functions to act as an indicator? the code... does it mean that the QCheckBox named indicator has a function indeterminate:pressed? I am having difficultity understanding the codes...:eek: Can you explain briefly?

QCheckBox::indicator:indeterminate:pressed {
image: url(:/images/checkbox_indeterminate_pressed.png);
}

Thanks!!

jacek
8th June 2008, 17:14
The compiler has already suggested what's wrong:
src\blinkingicon.cpp:4: note: (perhaps a semicolon is missing after the definition of `BlinkingIcon')

mynahz
9th June 2008, 04:43
i actually tried putting the semicolon but the error still resides... :(

and when i start another project and did the same thing, i actually get another set of errors! :confused:
let me show u all my codes in the whole project..
here is the main.cpp...


#include <QApplication>

#include "BlinkingIcon.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
BlinkingIcon picIcon;
picIcon.show();
return app.exec();
}

next is the blinkingicon.h...

#ifndef __BLINKINGICON_H__
#define __BLINKINGICON_H__

#include <QWidget>
#include <QLabel>

class BlinkingIcon : public QWidget
{
Q_OBJECT

public:
BlinkingIcon(QWidget *parent = 0);
virtual ~BlinkingIcon();

private:
QLabel * display_image;
}

#endif //__BLINKINGICON_H__

following which, is the blinkingicon.cpp...


#include "BlinkingIcon.h"

BlinkingIcon::BlinkingIcon(QWidget * parent)
: QWidget(parent)
{
this->setWindowTitle(tr("My Blinking Icon"));
this->resize(200,200);

display_image = new QLabel(this);
display_image->setPixmap(QPixmap("C:\HmiProject\blinkingIcon\sprites\ship\ship0000.p ng"));
display_image->adjustSize();
}

BlinkingIcon::~BlinkingIcon(){}

the new set of errors i am getting now is...

src\main.cpp:7: error: new types may not be defined in a return type
src\main.cpp:7: note: (perhaps a semicolon is missing after the definition of `BlinkingIcon')
src\main.cpp:7: error: extraneous `int' ignored
src\main.cpp: In function `BlinkingIcon qMain(int, char**)':
src\main.cpp:11: error: invalid conversion from `int' to `QWidget*'
src\main.cpp:11: error: initializing argument 1 of `BlinkingIcon::BlinkingIcon(QWidget*)'
src\main.cpp: In copy constructor `BlinkingIcon::BlinkingIcon(const BlinkingIcon&)':
../../HMIDeveloper/Qt/include/QtGui/../../src/gui/kernel/qwidget.h:720: error: `QWidget::QWidget(const QWidget&)' is private
src\main.cpp:11: error: within this context
src\main.cpp: In function `BlinkingIcon qMain(int, char**)':
src\main.cpp:11: error: initializing temporary from result of `BlinkingIcon::BlinkingIcon(QWidget*)'
mingw32-make[1]: *** [build\host\main.o] Error 1
mingw32-make: *** [release] Error 2

I don't quite get it... line 7 of main.cpp is a "{", no BlinkingIcon definition until line 9... as for line 11 i tried changing the return type to "int" type by "int(app.exec() )" but it also doesn't work. another thing i tried was to put "QWidget main(int argc, char *argv[])" or "void main(int argc, char *argv[])" instead of "int main(int argc, char *argv[])" but it doesn't help either.
As for the other errors at line 11 (regarding qMain), I am totally lost.:confused:
Hope you guys can enlighten me... Thanks loads!!!

JimDaniel
9th June 2008, 07:10
Try putting a semi-colon after the BlinkingIcon class defintion, in your header file. I was getting compile errors with the code until I did that. Now it works fine.

wysota
9th June 2008, 09:02
pardon me for my ignorance, but what does it mean by "The main difference is that a tristate QCheckBox has an indeterminate state." from the website?
It means the checkbox can be checked, unchecked or "partially checked".

mynahz
9th June 2008, 09:11
Try putting a semi-colon after the BlinkingIcon class defintion, in your header file. I was getting compile errors with the code until I did that. Now it works fine.

Hi JimDaniel,
Do you mind posting your code here for me to compare? because I did what you did too, but I am still stuck. I show you orig. and edited (add semicolon after BlinkingIcon) here and their error messages...


#ifndef __BLINKINGICON_H__
#define __BLINKINGICON_H__

#include <QWidget>
#include <QLabel>

class BlinkingIcon : public QWidget
{
Q_OBJECT

public:
BlinkingIcon(QWidget *parent = 0);
virtual ~BlinkingIcon();

private:
QLabel * display_image;
}

#endif //__BLINKINGICON_H__

for this, error messages are as follows:
src\main.cpp:7: error: new types may not be defined in a return type
src\main.cpp:7: note: (perhaps a semicolon is missing after the definition of `BlinkingIcon')
src\main.cpp:7: error: extraneous `int' ignored
src\main.cpp: In function `BlinkingIcon qMain(int, char**)':
src\main.cpp:11: error: invalid conversion from `int' to `QWidget*'
src\main.cpp:11: error: initializing argument 1 of `BlinkingIcon::BlinkingIcon(QWidget*)'
src\main.cpp: In copy constructor `BlinkingIcon::BlinkingIcon(const BlinkingIcon&)':
../../HMIDeveloper/Qt/include/QtGui/../../src/gui/kernel/qwidget.h:720: error: `QWidget::QWidget(const QWidget&)' is private
src\main.cpp:11: error: within this context
src\main.cpp: In function `BlinkingIcon qMain(int, char**)':
src\main.cpp:11: error: initializing temporary from result of `BlinkingIcon::BlinkingIcon(QWidget*)'
mingw32-make[1]: *** [build\host\main.o] Error 1
mingw32-make: *** [release] Error 2

for the edited version,

#ifndef __BLINKINGICON_H__
#define __BLINKINGICON_H__

#include <QWidget>
#include <QLabel>

class BlinkingIcon :: public QWidget
{
Q_OBJECT

public:
BlinkingIcon(QWidget *parent = 0);
virtual ~BlinkingIcon();

private:
QLabel * display_image;
}

#endif //__BLINKINGICON_H__

and the error messages are...
src\BlinkingIcon.h:7: error: expected identifier before "public"
src\BlinkingIcon.h:7: error: expected unqualified-id before "public"
src\main.cpp: In function `int qMain(int, char**)':
src\main.cpp:9: error: `BlinkingIcon' was not declared in this scope
src\main.cpp:9: error: expected `;' before "picIcon"
src\main.cpp:10: error: `picIcon' was not declared in this scope
src\main.cpp:9: warning: unused variable 'BlinkingIcon'
src\main.cpp:10: warning: unused variable 'picIcon'
mingw32-make[1]: Leaving directory `C:/HmiProject/blinkingIcon'
mingw32-make[1]: *** [build\host\main.o] Error 1
mingw32-make: *** [release] Error 2

jacek
9th June 2008, 12:49
Do you mind posting your code here for me to compare? because I did what you did too, but I am still stuck.
":" is called colon, a semicolon is ";" and you are supposed to put it in line #17 of that code snippet.

JimDaniel
9th June 2008, 17:13
#ifndef __BLINKINGICON_H__
#define __BLINKINGICON_H__

#include <QWidget>
#include <QLabel>

class BlinkingIcon : public QWidget
{
Q_OBJECT

public:
BlinkingIcon(QWidget *parent = 0);
virtual ~BlinkingIcon();

private:
QLabel * display_image;
};

#endif //__BLINKINGICON_H__

mynahz
10th June 2008, 04:14
gosh, silly me! i mixed up colon and semicolon! thanks a lot to all of you! :o


/***********blinkingicon.h***************/
#ifndef __BLINKINGICON_H__
#define __BLINKINGICON_H__

#include <QWidget>
#include <QLabel>

class BlinkingIcon : public QWidget
{
Q_OBJECT

public:
BlinkingIcon(QWidget *parent = 0);

virtual ~BlinkingIcon();

private:
QLabel * display_image1;
QLabel * display_image2;
QTimer * m_timer;
void startBlinking();
};

#endif //__BLINKINGICON_H__

/***********blinkingicon.cpp**************/

#include "BlinkingIcon.h"

BlinkingIcon::BlinkingIcon(QWidget * parent)
: QWidget(parent)
{
setWindowTitle(tr("My Blinking Icon"));
resize(200,200);

display_image1 = new QLabel(this);
display_image1->setPixmap(QPixmap("C:/HmiProject/blinkingIcon/sprites/ship/ship0030.png"));
display_image1->adjustSize();

display_image1->maximumSize(); //this widget maxsize is referring to picture or window max size?
}

BlinkingIcon::~BlinkingIcon(){}

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... :confused:

and another question, i tried to make the picture blink using QTimer like what aamer4yu suggested (with some changes) but i am having some errors that i can't understand...

blinkingicon.cpp


#include "BlinkingIcon.h"

BlinkingIcon::BlinkingIcon(QWidget * parent)
: QWidget(parent)
{
setWindowTitle(tr("My Blinking Icon"));
resize(200,200);
/**********************************************/
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(startBlinking()));
timer->start(1000);
startBlinking();
/**********************************************/

}

void BlinkingIcon::startBlinking()
{
QTime time = QTime::currentTime();
if ((time.second() % 2) == 0)
{
display_image1 = new QLabel(this);
display_image1->setPixmap(QPixmap("C:/HmiProject/blinkingIcon/sprites/ship/ship0030.png"));
display_image1->adjustSize();
display_image2->clear();
}
else
{
display_image2 = new QLabel(this);
display_image2->setPixmap(QPixmap("C:/HmiProject/blinkingIcon/sprites/ship/ship0000.png"));
display_image2->adjustSize();
display_image1->clear();
}
}

BlinkingIcon::~BlinkingIcon(){}

here are the errors...
///////ERROR/////////////////////
src\BlinkingIcon.cpp: In constructor `BlinkingIcon::BlinkingIcon(QWidget*)':
src\BlinkingIcon.cpp:10: error: invalid use of undefined type `struct QTimer'
../../HMIDeveloper/Qt/include/QtGui/../../src/gui/kernel/qwindowdefs.h:78: error: forward declaration of `struct QTimer'
src\BlinkingIcon.cpp:11: error: no matching function for call to `BlinkingIcon::connect(QTimer*&, const char[11], BlinkingIcon* const, const char[17])'
../../HMIDeveloper/Qt/include/QtCore/../../src/corelib/kernel/qobject.h:191: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
../../HMIDeveloper/Qt/include/QtCore/../../src/corelib/kernel/qobject.h:293: note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
src\BlinkingIcon.cpp:12: error: invalid use of undefined type `struct QTimer'
../../HMIDeveloper/Qt/include/QtGui/../../src/gui/kernel/qwindowdefs.h:78: error: forward declaration of `struct QTimer'
src\BlinkingIcon.cpp: In member function `void BlinkingIcon::startBlinking()':
src\BlinkingIcon.cpp:20: error: variable `QTime time' has initializer but incomplete type
src\BlinkingIcon.cpp:20: error: incomplete type `QTime' used in nested name specifier
mingw32-make[1]: Leaving directory `C:/HmiProject/blinkingIcon'
mingw32-make[1]: *** [build\host\BlinkingIcon.o] Error 1
mingw32-make: *** [release] Error 2

This method works well for blinking of the colon in a digital clock... hope you guys can help me out... :o

jacek
10th June 2008, 14:47
You are missing #include <QTimer>.

JimDaniel
10th June 2008, 16:02
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.

mynahz
11th June 2008, 05:34
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!:)


#include "BlinkingIcon.h"

BlinkingIcon::BlinkingIcon(QWidget * parent)
: QWidget(parent)
{
/**********************************************/
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(startBlinking()));
timer->start(1000);
startBlinking();
/**********************************************/

setWindowTitle(tr("My Blinking Icon"));
resize(50,50);
}

void BlinkingIcon::startBlinking()
{
QTime time = QTime::currentTime();
if ((time.second() % 2) == 0)
{
display_image1 = new QLabel(this);
display_image1->setPixmap(QPixmap("C:/HmiProject/blinkingIcon/sprites/ship/ship0007.png"));
display_image1->resize(50,50);
display_image1->setScaledContents(true);
display_image2->clear();
}
else
{
display_image2 = new QLabel(this);
display_image2->setPixmap(QPixmap("C:/HmiProject/blinkingIcon/sprites/ship/ship0000.png"));
//display_image1->clear();
}
//}
}

BlinkingIcon::~BlinkingIcon(){}

JimDaniel
11th June 2008, 06:16
Try this:



#include "BlinkingIcon.h"

BlinkingIcon::BlinkingIcon(QWidget * parent) : QWidget(parent)
{
setWindowTitle(tr("My Blinking Icon"));
resize(50,50);

display_image = new QLabel(this);
display_image->setScaledContents(true);

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(startBlinking()));
startBlinking();
timer->start(1000);
}

void BlinkingIcon::startBlinking()
{
display_image->clear();

QTime time = QTime::currentTime();
if ((time.second() % 2) == 0)
{
display_image->setPixmap(QPixmap("C:/my_first_image.png"));
}
else
{
display_image->setPixmap(QPixmap("C:/my_second_image.png"));
}

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.

mynahz
11th June 2008, 07:57
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...

JimDaniel
11th June 2008, 16:19
Okay, I finally whipped up a test app, and found some problems. Here is the working program:



#ifndef BLINKINGICON_H
#define BLINKINGICON_H

#include <QtGui>
#include <QtCore>

class BlinkingIcon : public QWidget
{
Q_OBJECT

public:
BlinkingIcon(QWidget * parent = 0);
virtual ~BlinkingIcon();

public slots:
void startBlinking();

private:
QLabel * m_display_image;
};

#endif // BLINKINGICON_H


#include "blinkingicon.h"

BlinkingIcon::BlinkingIcon(QWidget * parent) : QWidget(parent)
{
this->setWindowTitle(tr("My Blinking Icon"));
this->setFixedSize(50, 50);

m_display_image = new QLabel(this);
m_display_image->setScaledContents(true);

this->startBlinking();

QTimer * timer = new QTimer(this);
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)
{
m_display_image->setPixmap(QPixmap("C:/my_second_image.png"));
on = false;
}
else
{
m_display_image->setPixmap(QPixmap("C:/my_first_image.png"));
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...

mynahz
12th June 2008, 11:00
oh yes! it works perfect! :D
Thank you, JimDaniel!!!

mynahz
7th July 2008, 09:32
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.

mynahz
7th July 2008, 09:41
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.

MrShahi
7th July 2008, 10:27
I did not get your problem ? can you clarify it...............:confused:

mynahz
7th July 2008, 10:43
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.:)

wysota
7th July 2008, 15:20
Well... the simplest (as in most trivial) possible solution is such as the following:

class BlinkingLabel : public QLabel {
Q_OBJECT
public:
BlinkingLabel(QWidget *parent=0) : QLabel(parent){}
public slots:
void setPixmaps(const QPixmap &px1, const QPixmap &px2){
setPixmap(px1);
m_px1 = px1;
m_px2 = px2;
}
void start(int ms = 1000){
m_timer = startTimer(ms);
}
void stop(){ killTimer(m_timer); }
protected:
void timerEvent(QTimerEvent *te){
if(te->timerId()==m_timer){
if((*pixmap())==m_px1)
setPixmap(m_px2);
else
setPixmap(m_px1);
}
QLabel::timerEvent(te);
}
private:
QPixmap m_px1, m_px2;
int m_timer;
};

mynahz
8th July 2008, 03:47
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?

wysota
8th July 2008, 08:23
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.