August 25, 2011

Leon Leon
Ant Farmer
501 posts

[SOLVED] Help me with creating a Groupbox with listwidget and buttons!

 

Hello. I want to create a groupbox like this:
Screen1

With this code:

  1. optionsGroupBox = new QGroupBox(tr("Items"));
  2.     optionsGroupBoxLayout = new QGridLayout;
  3.     optionsGroupBoxLayout->addWidget(ui->listWidget, 1, 0);
  4.     optionsGroupBoxLayout->addWidget(ui->addButton, 1, 1);
  5.     optionsGroupBoxLayout->addWidget(ui->editButton, 1, 1);
  6.     optionsGroupBoxLayout->addWidget(ui->removeButton, 1, 1);
  7.     optionsGroupBoxLayout->addWidget(ui->clearButton, 1, 1);
  8.     optionsGroupBoxLayout->addWidget(ui->moveupButton, 1, 1);
  9.     optionsGroupBoxLayout->addWidget(ui->movedownButton, 1, 1);
  10.     optionsGroupBox->setLayout(optionsGroupBoxLayout);

I have this:
Screen2

All the buttons are in one place now.. I know why.. cause i have set all to 1,1.. but what else could i do? It is something like 1,1,another number here,another one here … Don’t know!

P.S Another solution would be to make a vertical layout of the buttons and then add the layout to the groupbox .. But how? optionsGroupBoxLayout->addLayout(ui->verticalLayout, 1, 1);
Will give me an error:QLayout::addChildLayout: layout “verticalLayout” already has a parent
The program has unexpectedly finished.

Thanks for any answer, Leon :)

2 replies

August 25, 2011

Andre Andre
Robot Herder
6399 posts

Is there a reason you add all the widgets to the same row and column (that is: the same cell in the grid layout)?

I would probably not use a QGridLayout, but use a QVBoxLayout for the buttons, and a QHBoxLayout to put the listbox and the layout for the buttons in.

August 25, 2011

Leon Leon
Ant Farmer
501 posts

Hello andre! Thanks for the tip! It worked :)

  1. mainLayout2 = new QHBoxLayout;
  2.     mainLayout2->addWidget(ui->listWidget);
  3. mainLayout3 = new QVBoxLayout;
  4.     mainLayout3->addWidget(ui->addButton);
  5.     mainLayout3->addWidget(ui->editButton);
  6.     mainLayout3->addWidget(ui->removeButton);
  7.     mainLayout3->addWidget(ui->clearButton);
  8.     mainLayout3->addWidget(ui->moveupButton);
  9.     mainLayout3->addWidget(ui->movedownButton);
  10. optionsGroupBox = new QGroupBox(tr("Items"));
  11.     optionsGroupBoxLayout = new QGridLayout;
  12.     optionsGroupBoxLayout->addLayout(mainLayout2, 1,0);
  13.     optionsGroupBoxLayout->addLayout(mainLayout3, 1,1);
  14.     optionsGroupBox->setLayout(optionsGroupBoxLayout);

 
  ‹‹ [solved] How to catch mouse click event on titlebar area of QMainWindow?      [SOLVED]Making QListWidget Item as Editable from QListWidget Context menu.. ››

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