PDA

View Full Version : Qt4 widget and nested layout issue.



bunjee
18th January 2007, 13:06
Hey all,

I've got a problem with a nested layout.
First off I'm creating the main widget items.
Then I'm passsing it to my first view to create its Qt components :



ZeLoginView::ZeLoginView(QWidget *parent, ZeClientController& clientController) :
QWidget(parent)


At the end of the process I'm deleting this view and free the layout of my main widget :


void ZeGlobalController::SetView(ViewMode viewMode)
{
delete this->layout(); [...]
}


Finally I'm passing my main widget to another view with a "Nested layout" :


ZeUserView::ZeUserView(QWidget *parent, ZeUserController& userController) :
QWidget(parent)
{
ZeLog::Get()->AddLine("--- Creating the user view ---\n");

mUserLayout = new QVBoxLayout(parent);

// User Infos

mUserInfoLayout = new QGridLayout;

// User picture
mUserPictureLabel = new QLabel;
mUserPictureLabel->setPixmap(QPixmap("zeData/profil_no_pic.jpg"));
mUserInfoLayout->addWidget(mUserPictureLabel, 1, 1, Qt::AlignCenter);

// Login label
mLoginLabel = new QLabel;
mLoginLabel->setText(tr("Login"));
mUserInfoLayout->addWidget(mLoginLabel, 2, 1, Qt::AlignCenter);

// Message combo box
mMessageComboBox = new QLineEdit;
mMessageComboBox->setFixedWidth(100);
mUserInfoLayout->addWidget(mMessageComboBox, 3, 1, Qt::AlignCenter);

// Status combo box
mStatusComboBox = new QComboBox;
mStatusComboBox->setEditable(true);
mStatusComboBox->setFixedWidth(100);
mUserInfoLayout->addWidget(mStatusComboBox, 4, 1, Qt::AlignCenter);

// Layout stretch
mUserInfoLayout->setColumnStretch(0, 10);
mUserInfoLayout->setColumnStretch(2, 10);

mUserLayout->addLayout(mUserInfoLayout);

}


For some reason the nested layout : mUserInfoLayout, never appears : blank window.

And even more strange, it's working in two cases:
1. If I don't use nested layout to show my components.
2. Working with nested stuff If I'm creating the second view at first.

Looks like something is messed up between the first view and the second creation.

There must be something wrong with the main widget, is there anything I could have forgot beside the delete layout() ?

In advance thanks.

Ben.

wysota
18th January 2007, 13:34
What is the base class of ZeUserController?

jacek
18th January 2007, 13:39
There must be something wrong with the main widget, is there anything I could have forgot beside the delete layout() ?
Why do you delete that layout and where do you call QWidget::setLayout()?

bunjee
18th January 2007, 13:56
Here is the base class of the ZeUserController.



class ZeUserController : public QWidget
{
Q_OBJECT


I have one Widget for the main window of the application and many views.

As I read in Qt doc, a Widget cannot have several layouts,
I'm deleting the previous one in order to assign my next view.



void ZeGlobalController::SetView(ViewMode viewMode)
{
ClearControllers();

delete this->layout();

switch (viewMode)
{
case LOGIN_VIEW:
mClientController = new ZeClientController(*(ZeClient::Get()));
mClientController->GenerateView();
break;

case CONTACT_VIEW:
mUserController = new ZeUserController();
mUserController->GenerateView();

default:
break;
}
}

jacek
18th January 2007, 13:59
I'm deleting the previous one in order to assign my next view.
OK and where do you call setLayout()?

bunjee
18th January 2007, 14:11
ZeLoginView::ZeLoginView(QWidget *parent, ZeClientController& clientController) :
QWidget(parent)
{
// Login layout
mLoginLayout = new QGridLayout(parent);


In the constructor of mLoginLayout in ZeLoginView.



ZeUserView::ZeUserView(QWidget *parent, ZeUserController& userController) :
QWidget(parent)
{
mUserLayout = new QVBoxLayout(parent);


In the constructor of mUserLayout in ZeUserView.

wysota
18th January 2007, 14:20
That's an awful way to do that (tightly couples the two widgets). Why not reuse the layout instead of creating a new one?

bunjee
18th January 2007, 14:23
In one case I need a QGrid in another a QHBox as main layout.

wysota
18th January 2007, 14:28
But in both cases they can be children of the parent widget's layout. Your "view" should be a complete widget (having its own layout), not a complete layout. Then you can simply add the widget to the parent's layout.

jacek
18th January 2007, 14:35
mLoginLayout = new QGridLayout(parent);
...
mUserLayout = new QVBoxLayout(parent);
I see, but in this case you end with a widget that has no layout and no child widgets.

bunjee
18th January 2007, 16:07
Thanks a lot guys,

I've changed my approach :
" One widget has one layout and sticks with it ".

Even though I don't understand why this option:


mLoginLayout = new QGridLayout(parent);
...
mUserLayout = new QVBoxLayout(parent);

... doesn't work.

jacek
18th January 2007, 18:56
Even though I don't understand why this option: [...]
... doesn't work.
Maybe you don't see any other widgets because they are hidden under those empty ZexxxView widgets?

wysota
18th January 2007, 21:29
Or you just didn't call show() on them...