PDA

View Full Version : table in QLabel



Pan Wojtas
5th February 2006, 14:14
QLabel *label = new QLabel(0);
label->setFrameStyle(QFrame::Plain);
label->setBackgroundMode(Qt::FixedColor);
label->setPaletteBackgroundColor(QColor("red"));
label->setText("<table bgcolor=white border=0 cellspacing=0 cellpadding=0><tr><td>aaa<br>ccc<br>ddd</td><td>bbb</td></tr></table>");
label->show();
and effect:
http://www.rejsymorskie.net/smieci/57.png

What can I do to avoid this red border above, under and on the right of the table?
I'd like to have a QLabel that consist ONLY the table - and nothing else (no margins).

(I've made red and white colors only to show you this margins)

jacek
5th February 2006, 16:11
Did you try:
label->setScaledContents( true );and/or
label->setAlignment( Qt::AlignCenter );

Pan Wojtas
5th February 2006, 16:23
setScaledContents doesn't change anything;
setAlignment only moves this table to the center of QLabel (makes another margin on the left side).
:(

jacek
5th February 2006, 17:24
Then try:
label->setText("<table width=\"100%\" bgcolor=white border=0 cellspacing=0 cellpadding=0><tr><td>aaa<br>ccc<br>ddd</td><td>bbb</td></tr></table>");

Pan Wojtas
5th February 2006, 17:39
It looks like this:
http://www.rejsymorskie.net/smieci/58.png

So it removes left and right margins, but QLabel is a way too wide.
For me more serious issue are margins above and below table. "height=100%" doesn't work.

jacek
5th February 2006, 17:45
For me more serious issue are margins above and below table. "height=100%" doesn't work.
Then make the QLabel smaller --- there isn't enough content to fill it.

Pan Wojtas
5th February 2006, 17:57
Then make the QLabel smaller --- there isn't enough content to fill it.
I don't understand...
I've made larger QLabel:
http://www.rejsymorskie.net/smieci/59.png
but there is still the same margin :confused:

I've noticed, that it is always 16px at the bottom and 2px at the top...

wysota
5th February 2006, 17:59
Maybe you should just find some other way to place your table? :)

piotrpsz
5th February 2006, 18:01
Can you try:

label->layout()->setSpacing( ... ),
label->layout()->setMargin( ... ),

:)

Pan Wojtas
5th February 2006, 20:22
label->layout()->setSpacing( ... ),
label->layout()->setMargin( ... ),
But it makes "segmentation fault"...

wysota
5th February 2006, 20:51
QLabel is not a container, thus it doesn't have a layout (unless you provide one, but I don't know what will happen then... QLabel is not supposed to have a layout).

Everall
6th February 2006, 11:40
What can I do to avoid this red border above, below

i did some testing on QT4. I know you're on QT3, but i wanted to see the behaviour here also.

QT4 doesn't provide the left and right margins, but you get top and bottom margins. So this is better than QT3.

But the margins on top and bottom arestill there.
I noticed I could drag the dialogbox to a minimum size = sizehint of the textlabel. I couldn't drag it smaller. (because of QT's wonderfull sizehint functionality)

So here is my dirty trick :
label->resize(1,1);//resize it to 1 by 1 pixel, which QT won't allow.

and in QT4 the dialogbox is the size you want without the margins.

I hope it will work for QT3 also.

Cheers

Pan Wojtas
6th February 2006, 16:29
So here is my dirty trick :
label->resize(1,1);//resize it to 1 by 1 pixel, which QT won't allow.
It resize this QLabel ALWAYS to minimal size, so the table is without margins, but Qt doesn't show the whole table...

QLabel *label = new QLabel(0);
label->resize(1,1);
label->setFrameStyle(QFrame::Plain);
label->setBackgroundMode(Qt::FixedColor);
label->setPaletteBackgroundColor(QColor("red"));
label->setText("<table width=\"100%\" bgcolor=white border=0 cellspacing=0 cellpadding=0><tr><td>aaa<br>ccc<br>ddd</td><td>bbb</td></tr></table>");
label->show();
gives:
http://www.rejsymorskie.net/smieci/60.png
How does it look on Qt4?

I've seen in some sources this trick with resize(1,1), but I don't remember exacly where it was... I'll look for it.

Everall
6th February 2006, 17:03
How does it look on Qt4?
it looks like :

Cheers

piotrpsz
6th February 2006, 18:39
QLabel is not a container, thus it doesn't have a layout (unless you provide one, but I don't know what will happen then... QLabel is not supposed to have a layout).

QLabel is a QWidget.
Every QWidget have a layout (QWidget have a function member QLayout* layout()).
Maybe is not used, but have layout.

Piotr

piotrpsz
6th February 2006, 18:49
But it makes "segmentation fault"...

That's right.
For label function layout() returns 0 (NULL).

But try this code:



QLabel* lbl = new QLabel( "cos_niecos", this );
QHBoxLayout* lbl_layout = new QHBoxLayout( lbl );
QLayout* lay = lbl->layout();

if( 0 == lay ) qWarning( "lay is 0" );
else qWarning( "OK" );

lbl->layout()->setMargin( 0 );
lbl->layout()->setSpacing( 0 );


This code functioning.
Piotr

wysota
6th February 2006, 19:02
QLabel is a QWidget.
Every QWidget have a layout (QWidget have a function member QLayout* layout()).
Maybe is not used, but have layout.


Eeem.... no.


#include <qwidget.h>
#include <qlayout.h>
#include <qapplication.h>

int main(int argc, char **argv){
QApplication app(argc, argv);
QWidget *w = new QWidget();
w->layout()->setMargin(5);
delete w;
return 0;
}

On my system it crashes on "w->layout()->setMargin()" call, meaning there is no layout in the widget. A class may have methods to get or set a layout, but it doesn't mean it uses them. If you take a look at QWidget sources, you'll notice, that it only references "layout" while dealing with "height for width".

piotrpsz
6th February 2006, 19:38
Eeem.... no.


#include <qwidget.h>
#include <qlayout.h>
#include <qapplication.h>

int main(int argc, char **argv){
QApplication app(argc, argv);
QWidget *w = new QWidget();
w->layout()->setMargin(5);
delete w;
return 0;
}

On my system it crashes on "w->layout()->setMargin()" call, meaning there is no layout in the widget. A class may have methods to get or set a layout, but it doesn't mean it uses them. If you take a look at QWidget sources, you'll notice, that it only references "layout" while dealing with "height for width".

OK :)
Sorry, I should tell, that every widget CAN have a layout.
Look a previous my post.

:) Best regards
Piotr

wysota
7th February 2006, 23:33
So set a layout for QWidget and try to do anything meaningfull with it...

Pan Wojtas
13th February 2006, 12:37
I've noticed, that this extra margin on the bottom is always equal
to "fontMetrics().height()", so it seems that Qt adds one empty line below
each table...