October 4, 2011

fluca1978 fluca1978
Lab Rat
529 posts

[SOLVED] help with layout

 

Hi,
in a widget I’ve got the following layout setup:

  1. QHBoxLayout* hLayout = new QHBoxLayout( this );
  2.     panelMaster->setLayout( hLayout );
  3.     tableViewMaster->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
  4.     hLayout->addWidget( tableViewMaster );
  5.  
  6.  
  7.  
  8.     QWidget* buttons = new QWidget( this );
  9.     QVBoxLayout* vLayout = new QVBoxLayout( buttons );
  10.     buttons->setLayout( vLayout );
  11.     vLayout->addWidget( buttonAddMasterElement );
  12.     vLayout->addWidget( buttonEditMasterElement );
  13.     vLayout->addWidget( buttonDeleteMasterElement );
  14.     vLayout->addWidget( buttonRefreshMasterElement );
  15.     hLayout->addWidget( buttons );

The idea is to have the tableview to occupy the most and central space of the widget, with a vertical set of buttons on the right. However, when I set up this widget as the central widget of my mainwindow the widget itself occupies less than the whole window space and I cannot resize the table. What am I doing wrong?

5 replies

October 4, 2011

Gerolf Gerolf
Robot Herder
3254 posts

It’s a bit difficult to say, what is wrong, as I have no idea, how the widgets belong to each other (parent child). What is the panelMaster pointer? Where is tableViewMaster created on?

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

October 4, 2011

fluca1978 fluca1978
Lab Rat
529 posts

Well the method that performs the setup of the gui from the constructor of the widget (and the constructor does nothing else) is:

  1. void UserView::setUpPanelMaster()
  2. {
  3.     panelMaster = new QWidget( this );
  4.     ...
  5.     tableViewMaster = new QTableView( panelMaster );
  6.  
  7.     ...
  8.     QHBoxLayout* hLayout = new QHBoxLayout( panelMaster );
  9.     panelMaster->setLayout( hLayout );
  10.     tableViewMaster->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding);
  11.     hLayout->addWidget( tableViewMaster );
  12.  
  13.  
  14.  
  15.     QWidget* buttons = new QWidget( panelMaster );
  16.     QVBoxLayout* vLayout = new QVBoxLayout( buttons );
  17.     buttons->setLayout( vLayout );
  18.     vLayout->addWidget( buttonAddMasterElement );
  19.     vLayout->addWidget( buttonEditMasterElement );
  20.     vLayout->addWidget( buttonDeleteMasterElement );
  21.     vLayout->addWidget( buttonRefreshMasterTable );
  22.     hLayout->addWidget( buttons );

I think I fixed all the paret-child relationships.
In the mainwindow I’ve got:

  1. setCentralWidget( uview );

with uview as my widget and no layout set up for the main window.

October 4, 2011

Gerolf Gerolf
Robot Herder
3254 posts

And where is the layout of uview?
What is panelMaster used for? In which layout is it used?
Or is userView the main window class?

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

October 5, 2011

fluca1978 fluca1978
Lab Rat
529 posts

To make it clear:

  • uview is a subclass of QWidget that in its constructor calls setUpPanelMaster:

  1. UserView::UserView(QWidget *parent) :
  2.     QWidget(parent)
  3. {
  4.  
  5.         setUpPanelMaster();
  6. }

  • panel master is simply used as a panel within uview, since I will reach the condition when I have two or more panels with different widgets inside the uview (but at the moment I’m using only one).
  • panel master is set up with a horizontal layout, on the left there is the tableview, and on the right there is vertical layout with the buttons.
  • in the main window class I create a new uiview and place it as central widget without any further layout setup.

October 6, 2011

fluca1978 fluca1978
Lab Rat
529 posts

I managed it in the end!
The problem was in the constructor of the UserView, that while it was setting up a layout, it was not adding widgets to it. The final code that works is therefore:

  1. // set up a vertical layout
  2.         QVBoxLayout* vLayoutMain = new QVBoxLayout( this );
  3.         setLayout( vLayoutMain );
  4.  
  5.         setUpPanelMaster();
  6.  
  7.  
  8.         vLayoutMain->addWidget( panelMaster );

and now it is displayed correctly.

 
  ‹‹ Cross platform SSE/MMX capabilities detection      On Focus Event alternative? && returning a value from dialog ››

You must log in to post a reply. Not a member yet? Register here!