make a .jpg or .bmp picture into a widget...?
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. :)
Re: make a .jpg or .bmp picture into a widget...?
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.
Re: make a .jpg or .bmp picture into a widget...?
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
Re: make a .jpg or .bmp picture into a widget...?
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.
Re: make a .jpg or .bmp picture into a widget...?
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.
Re: make a .jpg or .bmp picture into a widget...?
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,
Quote:
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. :)
Re: make a .jpg or .bmp picture into a widget...?
something like -
Code:
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 :)
Re: make a .jpg or .bmp picture into a widget...?
Quote:
Originally Posted by
mynahz
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:
Code:
//or
pixmap.load("c:/my_image.jpg");
QLabel:
Code:
QLabel label
("c:/my_image.jpg");
//or
label.setPixmap("c:/my_image.jpg");
//or more often
Re: make a .jpg or .bmp picture into a widget...?
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...
Code:
/*************main.cpp****************/
#include <QApplication>
#include "blinkingicon.h"
int main(int argc, char *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:
void displayIcon();
//protected:
};
#endif
/*************BlinkingIcon.cpp*****************/
#include <QtGui>
#include "blinkingicon.h"
blinkingicon
::blinkingicon(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()
{
pixmap.load("C:/HmiProject/blinkingIcon/sprites/ship/ship0000.png");
}
Any idea what went wrong?:crying:
Re: make a .jpg or .bmp picture into a widget...?
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.
Code:
#ifndef _BLINKING_ICON_
#define _BLINKING_ICON_
#include <QWidget>
#include <QLabel>
class BlinkingIcon
: public QWidget{
Q_OBJECT
public:
BlinkingIcon
(QWidget * parent
= 0);
virtual ~BlinkingIcon();
private:
}
#endif
{
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...
Re: make a .jpg or .bmp picture into a widget...?
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/styl...zing-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.
Re: make a .jpg or .bmp picture into a widget...?
Hi JimDaniel,
I have tried using your example but why do I get errors like this?:confused:
Code:
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...
Code:
#ifndef __BLINKINGICON_H__
#define __BLINKINGICON_H__
#include <QWidget>
#include <QLabel>
class BlinkingIcon
: public QWidget{
Q_OBJECT
public:
BlinkingIcon
(QWidget * parent
= 0);
virtual ~BlinkingIcon();
private:
}
#endif // __BLINKINGICON_H__
Code:
#include "blinkingicon.h"
BlinkingIcon
::BlinkingIcon(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?
Code:
image: url(:/images/checkbox_indeterminate_pressed.png);
}
Thanks!!
Re: make a .jpg or .bmp picture into a widget...?
The compiler has already suggested what's wrong:
Quote:
src\blinkingicon.cpp:4: note: (perhaps a semicolon is missing after the definition of `BlinkingIcon')
Re: make a .jpg or .bmp picture into a widget...?
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...
Code:
#include <QApplication>
#include "BlinkingIcon.h"
int main(int argc, char *argv[])
{
BlinkingIcon picIcon;
picIcon.show();
return app.exec();
}
next is the blinkingicon.h...
Code:
#ifndef __BLINKINGICON_H__
#define __BLINKINGICON_H__
#include <QWidget>
#include <QLabel>
class BlinkingIcon
: public QWidget{
Q_OBJECT
public:
virtual ~BlinkingIcon();
private:
}
#endif //__BLINKINGICON_H__
following which, is the blinkingicon.cpp...
Code:
#include "BlinkingIcon.h"
BlinkingIcon
::BlinkingIcon(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.png"));
display_image->adjustSize();
}
BlinkingIcon::~BlinkingIcon(){}
the new set of errors i am getting now is...
Code:
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!!!
Re: make a .jpg or .bmp picture into a widget...?
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.
Re: make a .jpg or .bmp picture into a widget...?
Quote:
Originally Posted by
mynahz
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".
Re: make a .jpg or .bmp picture into a widget...?
Quote:
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...
Code:
#ifndef __BLINKINGICON_H__
#define __BLINKINGICON_H__
#include <QWidget>
#include <QLabel>
class BlinkingIcon
: public QWidget{
Q_OBJECT
public:
virtual ~BlinkingIcon();
private:
}
#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,
Code:
#ifndef __BLINKINGICON_H__
#define __BLINKINGICON_H__
#include <QWidget>
#include <QLabel>
class BlinkingIcon
:: public QWidget{
Q_OBJECT
public:
virtual ~BlinkingIcon();
private:
}
#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
Re: make a .jpg or .bmp picture into a widget...?
Quote:
Originally Posted by
mynahz
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.
Re: make a .jpg or .bmp picture into a widget...?
Code:
#ifndef __BLINKINGICON_H__
#define __BLINKINGICON_H__
#include <QWidget>
#include <QLabel>
class BlinkingIcon
: public QWidget{
Q_OBJECT
public:
virtual ~BlinkingIcon();
private:
};
#endif //__BLINKINGICON_H__
Re: make a .jpg or .bmp picture into a widget...?
gosh, silly me! i mixed up colon and semicolon! thanks a lot to all of you! :o
Code:
/***********blinkingicon.h***************/
#ifndef __BLINKINGICON_H__
#define __BLINKINGICON_H__
#include <QWidget>
#include <QLabel>
class BlinkingIcon
: public QWidget{
Q_OBJECT
public:
virtual ~BlinkingIcon();
private:
void startBlinking();
};
#endif //__BLINKINGICON_H__
/***********blinkingicon.cpp**************/
#include "BlinkingIcon.h"
BlinkingIcon
::BlinkingIcon(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
Code:
#include "BlinkingIcon.h"
BlinkingIcon
::BlinkingIcon(QWidget * parent
){
setWindowTitle(tr("My Blinking Icon"));
resize(200,200);
/**********************************************/
connect(timer, SIGNAL(timeout()), this, SLOT(startBlinking()));
timer->start(1000);
startBlinking();
/**********************************************/
}
void BlinkingIcon::startBlinking()
{
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