PDA

View Full Version : setContentsMargins in QAbstractItemView



Promethee
18th January 2006, 12:19
Hi everybody,

I would like to make a view of my own, containing some widgets layouted in a QGridLayout. So I sub classed QAbstractItemView. But I have a problem with the function setContentsMargins() : this function does nothing. What is strange is that with a QFrame (parent of QAbstractItemView), it is possible to set the margins. Here is my code:



QTextEdit* textEdit1 = new QTextEdit;
QTextEdit* textEdit2 = new QTextEdit;

MyOwnView* myView = new MyOwnView(this);
myView->setContentsMargins(50,50,50,50);
QGridLayout* gridLayout1 = new QGridLayout(myView);
gridLayout1->addWidget(textEdit1, 0, 0);
myView->setLayout(gridLayout1);

QFrame* frame = new QFrame(this);
frame->setContentsMargins(50,50,50,50);
QGridLayout* gridLayout2 = new QGridLayout(frame);
gridLayout2->addWidget(textEdit2, 0, 0);
frame->setLayout(gridLayout2);

QStackedWidget* stackedWidget = new QStackedWidget(this);
stackedWidget->addWidget(myView);
stackedWidget->addWidget(frame);

setCentralWidget(stackedWidget);


On the first attached image the QTextEdit is inside a QFrame and the second one it is inside a QAbstractItemView

Did somebody experiment the same issue? Is it a Qt bug or one of my own?

wysota
4th February 2006, 12:50
I think you should first set the layout for the widget and then change the margins and not the other way round.

Xagen
4th February 2006, 15:40
QGridLayout has two functions that you shoulf use:
setMargin( int ) // you should use 0
setSpacing( int ) // 0

This is the solution of your problem.:D