1

I have a horizontal layout inside which i have added a group box. The group box is split using a QSplitter and contain a QListWidget and a QTextBrowser. I want to add two push buttons at the bottom right corner of the window to navigate inside the QTextBrowser. I'm adding two buttons inside the layout but unable to add them at the bottom right corner. Here is my code:

   _groupBox = new QGroupBox();
   _groupBox->setTitle("Config Help System");
   _verticalLayout = new QVBoxLayout(_groupBox);
   this->setLayout(_verticalLayout);
   _splitter = new QSplitter(_groupBox);
   _splitter->setOrientation(Qt::Horizontal);

   _list = new QListWidget(_splitter);
   QListWidgetItem *item1 = new QListWidgetItem;
   item1->setText("About Config Tool");
   _list->insertItem(0, item1);
   QListWidgetItem *item2 = new QListWidgetItem;
   item2->setText("Configuration Help");
   _list->insertItem(1, item2);
   _list->setSelectionMode(QAbstractItemView::SingleSelection);
   _splitter->addWidget(_list);

   _helpBrowser = new QTextBrowser(_splitter);
   _homeButton = new QPushButton(tr("&Home"), this);
   _backButton = new QPushButton(tr("&Back"), this);
   _closeButton = new QPushButton(tr("&Close"), this);

   QHBoxLayout *buttonLayout = new QHBoxLayout(this);
   buttonLayout->addWidget(_homeButton);
   buttonLayout->addWidget(_backButton);
   buttonLayout->addStretch(1);
   buttonLayout->addWidget(_closeButton)

   _splitter->addWidget(_helpBrowser);
   _splitter->setStretchFactor(1,1);

   _verticalLayout->addWidget(_splitter);
   this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
   this->resize(1000,600);

I have tried the stretch factor to adjust the buttons to appear at the bottom right corner to the splitter. Can anyone please help me with it?

Jablonski
  • 18,083
  • 2
  • 46
  • 47
Beginner
  • 43
  • 4

1 Answers1

1

Try this(code is slightly different because I tried to run it on my computer):

QWidget *www = new QWidget;
QGroupBox*    _groupBox = new QGroupBox();
_groupBox->setTitle("Config Help System");
QVBoxLayout *_verticalLayout = new QVBoxLayout(_groupBox);
www->setLayout(_verticalLayout);
QSplitter *_splitter = new QSplitter(_groupBox);
_splitter->setOrientation(Qt::Horizontal);

QListWidget *_list = new QListWidget(_splitter);
QListWidgetItem *item1 = new QListWidgetItem;
item1->setText("About Config Tool");
_list->insertItem(0, item1);
QListWidgetItem *item2 = new QListWidgetItem;
item2->setText("Configuration Help");
_list->insertItem(1, item2);
_list->setSelectionMode(QAbstractItemView::SingleSelection);
_splitter->addWidget(_list);

QTextBrowser *_helpBrowser = new QTextBrowser(_splitter);
QPushButton* _homeButton = new QPushButton("home");
QPushButton* _backButton = new QPushButton("back");
QPushButton *_closeButton = new QPushButton("close");

QHBoxLayout *buttonLayout = new QHBoxLayout(this);
buttonLayout->addWidget(_closeButton);//new
buttonLayout->addStretch(1);
buttonLayout->addWidget(_homeButton);//new
buttonLayout->addWidget(_backButton);//new


_splitter->addWidget(_helpBrowser);
_splitter->setStretchFactor(1,1);

_verticalLayout->addWidget(_splitter);

_verticalLayout->addLayout(buttonLayout);//new

www->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
www->resize(1000,600);
www->show();

Result:

enter image description here

Edit:

#include <QGroupBox>
#include <QListWidget>
#include <QTextBrowser>
#include <QListWidgetItem>//do different includes to use my code inside main

//...

QHBoxLayout *buttonLayout = new QHBoxLayout;//just remove the parent
Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • Adding the HBoxLayout to the VBoxLayout doesnt work well for me. I get a error that says QLayout::AddChildLayout: layout already has a parent – Beginner Oct 21 '14 at 04:23
  • @user3035979 Unfortunately can't said where is problem without your code, so I think that you use my example wrong. Try copy and paste my example inside your main function and do connection and remove parent. And after that said me is all good or you still get this error. – Jablonski Oct 22 '14 at 19:16
  • I used a grid layout instead of vboxlayout and hboxlayout. The code works fine for me. Thanks for your help anyway :) – Beginner Oct 23 '14 at 04:57