June 20, 2011

Adi Adi
Lab Rat
206 posts

Regarding Layout Management

 

Hello All

I am trying to place below mentioned widgets on my main Window but they are not aligning properly PleaseHELP

Widgets
1.GroupBox
2.In that GroupBox 2 PushButtons & One TableWidget

Here is my code

  1. QGroupBox *TabDialog::createFirstExclusiveGroup()
  2. {
  3.  QGroupBox *groupBox = new QGroupBox(tr(""));
  4.  
  5.  QGridLayout *mainLayout = new QGridLayout;
  6.    
  7.  btnImport = new QPushButton(tr("&Import"));
  8.  btnImport->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
  9.  connect(btnImport, SIGNAL(clicked()), this, SLOT(ImportCSVFile()));
  10.  
  11.  btnExport = new QPushButton(tr("&Export"));
  12.  btnExport->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
  13.  connect(btnExport, SIGNAL(clicked()), this, SLOT(ExportCSVFile()));
  14.  
  15.  table = new QTableWidget(3,3);
  16.  
  17.  
  18.  mainLayout->addWidget(btnImport, 0, 0 , 1, 2);
  19.  mainLayout->addWidget(btnExport, 0, 1, 1, 2);
  20.  mainLayout->addWidget(table, 0, 1, 1, 2);
  21.  
  22.  mainLayout->setColumnStretch(0,300);
  23.  mainLayout->setColumnStretch(1, 500);
  24.  
  25.  groupBox->setLayout(mainLayout);
  26.  
  27.  return groupBox;
  28. }

2 replies

June 20, 2011

Gerolf Gerolf
Robot Herder
3286 posts

The order you use the calls does not set parents properly.
Usually, I suggest to give the parent in the constructors:

  1.  QGridLayout *mainLayout = new QGridLayout(groupBox);
  2.  btnImport = new QPushButton(tr("&Import"), groupBox);
  3.  btnExport = new QPushButton(tr("&Export"), groupBox);
  4.  table = new QTableWidget(3,3, groupBox);

The other possibility is to use the auto reparent feature of the layouts, but then, the parent of the layout must fit, which means the layout must have it’s parent before addWidget is called.

  1.     groupBox->setLayout(mainLayout);
  2.     mainLayout->addWidget(btnImport, 0, 0 , 1, 2);
  3.     mainLayout->addWidget(btnExport, 0, 1, 1, 2);
  4.     mainLayout->addWidget(table, 0, 1, 1, 2);

 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)

June 20, 2011

ludde ludde
Lab Rat
325 posts

I don’t think that’s the problem. I always construct layouts that way, and have never had any problems. With Qt 3.x you could run into all kinds of problems if you didn’t do things in the right order, but with Qt 4.x it tends to just work, regardless of the order in which you create things and put them in the layouts.

I think the problem is with the arguments to addWidget. Looks like you are adding btnExport and table in exactly the same place, for example. And btnImport and btnExport are overlapping.

 
  ‹‹ QGraphicsView performance with lots of items      How to get registry entries if there are multiple versions installed on Windows system using Qt API? ››

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