PDA

View Full Version : Qlabel



dragon
5th August 2007, 18:50
Hello anyone,

I have a question about QLabel in a RichTextDocument.

I have a label called freesLabel and put a pixmaps on it like this.
QPixmap eca = (QPixmap(":/images/eca.png");
QPixmap ecp = (QPixmap(":/images/ecp.png");
QPixmap erf = (QPixmap(":/images/erf.png");
freeslabel->setPixmap(eca);
freeslabel->setPixmap(ecp);
freeslabel->setPixmap(erf);

Now i want to put this pixmaps into richtextdocument like this.
Normally you must type this line
cursor.insertImage(":/images/eca.png"); or (":/images/ecp.png");
This works ok. but this i don't want.

I want to make a choice what at that moment is on the freesLabel.
I have try
if ( freesLabel == eca) {
cursor.insertImage( eca); }
But this give's a error cannot convert from QPixmap to QLabel.
How make the right code for this.

Thanks in advance.

marcel
5th August 2007, 19:02
You can try:


if((*freesLabel->pixmap()) == eca )
...do something


This may not work, so you will have to compare the two pixmaps by looking at relevant member data, such as size, depth, etc.

Regards

dragon
5th August 2007, 20:18
if((*freesLabel->pixmap()) == eca )

This did not work.
Its give's this error:

error C2678 binary == no operator found wich takes a left-hand operand of type
çonst QPixmap' (or there is no acceptable conversion).

maby other suggestions.

marcel
5th August 2007, 20:33
Well, this is because QPixmap doesn't have an operator==. I assumed that but I wasn't sure.

I now looked closer at your code and noticed what you are trying to do, and it can be done easier.


cursor.insertImage( freesLabel->pixmap());


And that is it.

Regards

dragon
5th August 2007, 20:51
if((*freesLabel->pixmap()) == eca )
cursor.insertImage(freesLabel->pixmap());
I have try this piece of code but i have still the same error.

marcel
5th August 2007, 21:03
But why do you do it like that?
Use only this:


cursor.insertImage(freesLabel->pixmap());

That line will add the pixmap currently contained by the QLabel( whatever it is, eca, ecb .. ) to the document.

There is no need for any additional testing.

Regards

dragon
6th August 2007, 16:55
I have try this piece of code
cursor.insertImage(freesLabel->pixmap());

This gives a error like this:
error C2664 void QTextCursor::insertImage( const TextImageFormat &) cannot convert
parameter 1 from const QPixmap to const QTextImageFormat &

marcel
6th August 2007, 17:16
Well, my bad after all :).
Looks like you have to build a QTextImageFormat.
You will have to set the name, width and height for the image format.
The name must be the name of the pixmap the label currently displays.
So you're back at square one. This can be solved as follows:
every time you set a different pixmap to the label, also add a dynamic property to it. The dynamic property will hold the name of the image:


QPixmap erf = (QPixmap(":/images/erf.png");
freeslabel->setPixmap(erf);
freesLabel->setProperty("imagePath", ":/images/erf.png");


Now, when you wish to insert the image:


QTextImageFormat imgfmt;
imgfmt->setName(freesLabel->property("imagePath").toString());
imgfmt->setWidth(freesLabel->pixmap()->width());
imgfmt->setHeight(freesLabel->pixmap()->height());
cursor.insertImage(imgfmt);



Hopefully this should work.

Regards

dragon
6th August 2007, 18:37
Iám newbe,

Maby a stuppid question where put i the width and height of the pixmap.



QPixmap erf = (QPixmap(":/images/erf.png");
freeslabel->setPixmap(erf); // here
freesLabel->setProperty("imagePath", ":/images/erf.png");

QTextImageFormat imgfmt;
imgfmt->setName(freesLabel->property(":/images/erf.png").toString());
imgfmt->setWidth(freesLabel->pixmap()->width()); //or here
imgfmt->setHeight(freesLabel->pixmap()->height()); //or here

marcel
6th August 2007, 18:43
Well, I have read the docs more carefully, and you might not need that.
Only do this when you wish to insert a pixmap:


QImage img = freesLabel->pixamp()->toImage();
textDoc->addResource(QTextDocument::ImageResource, QUrl("labelImage"), img);
cursor.insertImage("labelImage");


This will work just fine if you implement it correctly.
textDocument is your QTextDocument.

Regards

dragon
7th August 2007, 16:46
Marcel thanks for your time and answers it works fine now.