Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, September 2007
QGridLayout - addWidget problem


Message 1 in thread

Hi There,

The problem is very simple. Widgets are added the the QGridLayout object
like this:
pQGridLayout->addWidget(pSomeWidget, n, m, 1, x,
Qt::AlignLeft|Qt:AlignVCenter);

The problem is that it does not matter what is put to x, the size is always
one. No matter if it is -1 (should be full size) or any other number, always
1 seems to be interpreted what is not as per documentation. Has anyone seen
this behavior before? I did a search on the list and found
http://lists.trolltech.com/qt-interest/2004-02/thread00573-0.html. My
problem is similar, I would like to control the width (not in pixels, in
grid units) of the added widgets but it does not seem to be working.

Is that line above correct and Qt (4.2.3 in my case) is buggy or do I do
something wrong?

Bye,
Sandor

--
 [ signature omitted ] 

Message 2 in thread

Hi

I have attached an example. Notice the difference when alignment is passed.

-- 
 [ signature omitted ] 
#include <QtGui>

static QLabel* createLabel(const QString& text,
                           const QString& color,
                           QWidget* parent = 0)
{
    QLabel* label = new QLabel(text, parent);
    label->setStyleSheet(QString("background: %1").arg(color));
    return label;
}

int main(int argc, char* argv[])
{
    QApplication a(argc, argv);
    QWidget window;
    QGridLayout* grid = new QGridLayout(&window);
    grid->addWidget(createLabel("1 row, 2 cols", "red",    &window), 0, 0, 1, 2, Qt::AlignCenter);
    grid->addWidget(createLabel("1 row, 1 col",  "green",  &window), 1, 0);
    grid->addWidget(createLabel("1 row, 1 col",  "blue",   &window), 1, 1);
    grid->addWidget(createLabel("2 rows, 1 col", "yellow", &window), 0, 2, 2, 1);
    window.show();
    return a.exec();
}

Message 3 in thread

Hi There,

Hmmm, then the problem might be a bit different. Basically what I do is:
There is a QMainWindow. I set a QWidget based class as the central window,
which has elements on QScrollArea. So the code looks something like this and
the sizing is still incorrect on those:

	QHBoxLayout *layout = new QHBoxLayout;

	QScrollArea *scrollArea = new QScrollArea;
	QWidget *contentWidget = new QWidget;
	pMainLayout = new QGridLayout;

	QLabel *pLabel1 = new QLabel("label1", this);
	pMainLayout->addWidget(pLabel1, 0, 0, 1, 1,
Qt::AlignLeft|Qt::AlignVCenter);
	QLabel *pLabel2 = new QLabel("label2", this);
	pMainLayout->addWidget(pLabel2, 0, 1, 1, 1,
Qt::AlignLeft|Qt::AlignVCenter);
	QLabel *pLabel3 = new QLabel("label3", this);
	pMainLayout->addWidget(pLabel3, 0, 2, 1, 1,
Qt::AlignLeft|Qt::AlignVCenter);
	QLabel *pLabel4 = new QLabel("label4", this);
	pMainLayout->addWidget(pLabel4, 1, 0, 1, -1,
Qt::AlignLeft|Qt::AlignVCenter);
	QLabel *pLabel5 = new QLabel("label5", this);
	pMainLayout->addWidget(pLabel5, 2, 0, 1, 2,
Qt::AlignLeft|Qt::AlignVCenter);

	contentWidget->setLayout(pMainLayout);
	scrollArea->setWidget(contentWidget);

	layout->addWidget(scrollArea);
	setLayout(layout);

Still, pLabel4 and pLabel5 are 1 unit wide.

I could not figure out yet why, do you have a clue?

Thanks,
Sandor

-----Original Message-----
From: qt-interest-request@xxxxxxxxxxxxx
[mailto:qt-interest-request@xxxxxxxxxxxxx]On Behalf Of J-P Nurmi
Sent: 10/09/2007 23:54
To: Qt-interest
Subject: Re: QGridLayout - addWidget problem


Hi

I have attached an example. Notice the difference when alignment is passed.

-- 
 [ signature omitted ] 

Message 4 in thread

> Still, pLabel4 and pLabel5 are 1 unit wide.
>
> I could not figure out yet why, do you have a clue?

A non-zero alignment in grid layout indicates that the label should
not grow to fill the available space but should be sized according to
sizeHint(). I suggest you drop those alignments and set some
background colors so you can see what's going on. :)

PS. Notice that label has an alignment property too.

-- 
 [ signature omitted ] 

Message 5 in thread

Nice, it worked out! Thank you very much :)

-----Original Message-----
From: qt-interest-request@xxxxxxxxxxxxx
[mailto:qt-interest-request@xxxxxxxxxxxxx]On Behalf Of J-P Nurmi
Sent: 11/09/2007 10:01
To: Qt-interest
Subject: Re: QGridLayout - addWidget problem


> Still, pLabel4 and pLabel5 are 1 unit wide.
>
> I could not figure out yet why, do you have a clue?

A non-zero alignment in grid layout indicates that the label should
not grow to fill the available space but should be sized according to
sizeHint(). I suggest you drop those alignments and set some
background colors so you can see what's going on. :)

PS. Notice that label has an alignment property too.

-- 
 [ signature omitted ]