How to add a scrollbar to parent QWidget - qt

I understand how to add a scrollArea to a particular widget. However in my case Qwidget has multiple child widgets and these are all set using QVBoxLayout. Now how can I add a scroll bar in this case? Here QWidget is not the center widget, its one of the pages of the TabWidget. My code looks like:
QTabWIdget *center = new QTabWidget; setCentralWIdget(center);
xTab = new QWidget;
formLayout = new QFormLayout; formLayout->addWidget(...); formLayout->addWidget(...); formLayout->addWidget(...); formLayout->addWidget(...);
xTab->setLayout(formLayout);
Now how can I set the scrollBar to xTab? I tried using
scrollArea = new QScrollArea;
scrollArea->setWidget(xTab);
however, this isn't working.
Any idea/suggestions are helpful and appreciated.

Have you tried using QScrollArea as the tab page?
QTabWIdget *center = new QTabWidget; setCentralWIdget(center);
xTab = new QScrollArea;
formLayout = new QFormLayout; formLay....
xTab->setLayout(formLayout);
center->addTab(xTab, "XXX Tab");

I had success using the following:
scroll=new QScrollArea(mainWindow->centralWidget);
scroll->setGeometry(mainWindow->tabWidget->geometry());
scroll->setWidget(mainWindow->tabWidget);
scroll->show();
The QScrollArea defines where the scrollable widget will appear. If parent is 0, it's a non-modal window. setGeometry sets the QScrollArea instance to the desired dimensions (that of the tab). setWidget defines what widget the QScrollArea will actually be scrolling.

Related

Can we shift widget between the cells of a table widget

I've created a a widget in which I've placed two buttons using a layout and placed it inside a table widget's cell. The thing is that I'm changing the size of the table and I want to shift the placement of the widget without deleting them and re-initializing them from the beginning because I already assigned them actions on click (I think that the application would crash in this situation)
Code:
btn = new QPushButton[horzHeaders.size()];
btn[j].setParent(ui->tableWidget);
btn[j].setIcon(QIcon("./save.png"));
btn[j].setVisible(true);
btn_Load = new QPushButton[horzHeaders.size()];
btn_Load[j].setParent(ui->tableWidget);
btn_Load[j].setIcon(QIcon("./upload.png"));
btn_Load[j].setVisible(true);
lay = new QHBoxLayout[horzHeaders.size()];
lay[j].addWidget(&btn[j]);
lay[j].addWidget(&btn_Load[j]);
QWidget *w = new QWidget[horzHeaders.size()];
w[j].setLayout(&lay[j]);
ui->tableWidget->setCellWidget(j,vertHeaders.size() - 1, &w[j]);
You can add and remove widgets from a layout anytime you want
QWidget *widget = new QWidget();
QPushButton *button = new QPushButton();
QHBoxLayout *Hbox = new QHBoxLayout();
QVBoxLayout *Vbox= new QVBoxLayout();
Hbox->addWidget(button);
// use it till the window is resized
//and then check with an if-statement if the window is resized or not
Hbox->removeWidget(button); // remove from the button from layout
layout()->removeAt(widget); //remove the widget's current layout
Vbox->addWidget(button); // add button widget to vertical layout
widget->setLayout(Vbox); // Give it a new layout
widget->setLayout(Vbox);
If this didn't answer your question then ask in the comments.

How do you place a groupbox in a QMenuBar?

I need the groupbox that contains the ID picture and a button on the right side of the QMenuBar, and I don't know how to do that. Here's what I have so far:
QGridLayout *menulayout = new QGridLayout();
QWidget *menuwidget = new QWidget (dynamic_cast<QMainWindow*>(this->parent())->menuBar());
menulayout->setContentsMargins(18,0,18,0);
menulayout->addWidget(menuwidget);
menulayout->addWidget(_ui->groupBox_UserMenu, 0 ,0);
dynamic_cast<QMainWindow*>(this->parent())->menuBar()->setLayout(menulayout);
Fixed this by placing the groupbox on the desired position on the Form and then set it's parent to the QMenu Bar. The first lines of code I posted were unnecessary.
_ui->groupBox_UserMenu->setParent(_ui->menuBar);

How to autofit widget size and position to a cell in QTreeWidget?

I pragrammaticaly create a QTreeWidget.
Then I pragrammaticaly add some items.
Then I add two QLabel widgets to two items (QTreeWidgetItem) by
myTree->setItemWidget(item1, 0, myLabel1);
myTree->setItemWidget(item2, 0, myLabel2);
And then I try to resize the row of the item pragrammaticaly.
If I use an
item1->setSizeHint(0, QSize(myWidth, myHeight) );
the row chaged. But myLabel1 is not.
If I use an
item1->setSizeHint(0, QSize(myWidth, myHeight) );
myLabel->resize(myWidth, myHeight);
everething is ok but the row of myLabel2 mis adjusting to label by position.
Can I do something to auto-adjusting a widget (by size and position) to a cell of QTreeWidget?
P.S. After any resizing of tree (resize by width or expand/collapse node) widgets updates correctly.
In view of the fact that autofit start after resizing QTreeWidget, there is some method inside that resize widgets in cells.
So I opened QTreeWidget description (https://doc.qt.io/qt-5/qtreewidget.html) and red all of Public Functions and Public Slots. When I did not find any useful function I looked at a parent class (QTreeView). And found
myTree->resizeColumnToContent(0);
Call resizeColumnToContent after resizing any row in a QTreeWidget and widgets will be always fit to cells.
P.S. I am the OP.
you can try adding/updating a widget as an item, for example:
QWidget* wdg = new QWidget;
QPushButton* btIcon = new QPushButton();
QLabel* lb = new QLabel();
QHBoxLayout* layout = new QHBoxLayout(wdg);
layout->addWidget(btIcon);
layout->addWidget(lb);
layout->setAlignment( Qt::AlignCenter );
wdg->setLayout(layout);
myTree->setItemWidget(item1, 0, wdg);
Or just fill the tree with widgets

Show QSplitter child widgets titlebar

When I add QWidget to QSplitter, header/title bar of the child widget gets hidden automatically. is there a way to show it ??
Ex:
QSplitter* sp = new QSplitter();
QWidget* wid = new QWidget();
wid->setWindowTitle("Mytitle);
sp->addWidget(wid);
In this case wid title bar gets hidden automatically, I want to show it.

Qt- How to position UI elements like this?

I am creating a UI in Qt that has a QDockWidget containing a QPushButton and QLineEdit. Please refer to the attached mock-up. I have created the widget components and successfully got them up and running. However they are not positioned the way I want them to. Both the elements should float to the left making the extra space to the right stretch when the window is resized.
The code-
this->searchField = new QLineEdit; //"this" is a QDockWidget subclassed Object
searchField->setFixedWidth(200);
mainMenu = new Menu();
QHBoxLayout *layout= new QHBoxLayout;
QSpacerItem *filler = new QSpacerItem(1000, 10);
layout->addWidget(mainMenu->getMenuBar());
layout->addWidget(this->searchField);
layout->addSpacerItem(filler);
Any suggestion or help would be awesome!
Thanks for your time :)
http://qt-project.org/doc/qt-4.8/layout.html
http://qt-project.org/doc/qt-4.8/qboxlayout.html#addStretch
void QBoxLayout::addStretch ( int stretch = 0 )
Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end of this box layout.
So this is what your new code would look like:
this->searchField = new QLineEdit;
searchField->setFixedWidth(200);
mainMenu = new Menu();
QHBoxLayout *layout= new QHBoxLayout;
layout->addWidget(mainMenu->getMenuBar());
layout->addWidget(this->searchField);
layout->addStretch(); // Added this
Hope that helps.

Resources