fixing child layout/widget position in QT - qt

I wanted to know whether is there any way of fixing child layouts within a parent layout. For example...
QVBoxLayout *vbox = new QVBoxLayout;
// adding pushbuttons/layouts...
vbox->addWidget(one);
vbox->addWidget(two);
vbox->addWidget(three);
vbox->addWidget(four);
Now this ends up as four buttons/layouts in a vertical layout in the sequence that they are added. But if I remove buttons/layouts "one", "two" and "three"...
vbox->removeWidget(one);
vbox->removeWidget(two);
vbox->removeWidget(three);
After doing this, the pushbutton "four" will move up the layout as you remove widgets on top of "four". I don't want this to happen.
Is there any way that even if I remove the widget/layout on top, I need that last widget/layout to stay where it is currently.
How do I achieve this ?
UPDATE: Well I was experimenting and I was kind of able to achieve what I wanted using QGridLayout. Here is the code, but I am using QGridLayout instead of QVBoxLayout.
connect(one,SIGNAL(clicked()),this,SLOT(remove_btns()));
g = new QGridLayout(this);
g->addWidget(one,0,0,1,2);
g->addWidget(two,1,0,1,2);
g->addWidget(three,2,0,1,2);
g->addWidget(four,3,0,1,2,Qt::AlignBottom);
setLayout(g);
If I delete the above three buttons, the fourth one stays where it is, because of QT::AlignBottom , it does not work without that thing.
Here is the SLOT remove_btns()
void test::remove_btns()
{
g->removeWidget(one);
g->removeWidget(two);
g->removeWidget(three);
delete one;
delete two;
delete three;
}
When I click "one", top three buttons vanish, and the fourth one stays where it is. But it does not work if I don't give the QT::AlignBottom . Also, these alignment things are a mystery to me, I am unable to find how exactly they work.
This is definitely NOT an answer..., because I don't understand how it worked :P

If you are immediately replacing the widgets you removed, you can always insert your new widgets by index.
void insertWidget ( int index, QWidget * widget, int stretch = 0, Qt::Alignment alignment = 0 )

Yes, just hide the widgets instead of removing them:
one->hide();
two->hide();
three->hide();
If you really have to remove the widgets, perhaps you can replace them with some lightweight widget like a QLabel with no text.

Related

QTreeWidget - width of column with widget

I have QTreeWidget with two columns. The first column is the text, and the second is QPushButton. I can not specify the size of buttons and the size of the second column. When you try to set the size of the column of content, the second column disappears. How to change the width of the second column?
tree_widget_->setColumnCount(2);
tree_widget_->header()->resizeSection(1, 10);
tree_widget_->header()->setStretchLastSection(false);
tree_widget_->header()->setResizeMode(0,QHeaderView::Stretch);
tree_widget_->topLevelItem(4)->addChild( wiop = new QTreeWidgetItem(QStringList() << QString( "Расстояние: %1 км" ).arg( range ) ) );
tree_widget_->setItemWidget(tree_widget_->topLevelItem(4)->child(0),1,range_plot_button_ = new QPushButton("График",tree_widget_));
range_plot_button_->resize(10,10);
tree_widget_->setColumnWidth(1, 10) should do what you wanted.
When you assign QPushButton to QTreeWidget as a top level item the QTreeWidget object usualy assigned as a parent of this button, so all geometry of that button is handled by it's parent.
Take a look at docs.
If you want your push button to have it's own geometry with layouts (within column) you can make blank widget, set it to TreeWidget as an item and make your push button to be a child of that blank widget with all conveniences of layouts in Qt.
I try setColumnWidth, but it is not work.
Later I try tree_widget_->header()->setResizeMode(1,QHeaderView::ResizeToContents);
and it is work!

How to display fix banner between QMenubar and QToolBar in QMainWindow

How can I show fixed banner (with some widget like label and button ) in between
QMenuBar and QToolBar ?
Similarly like QStatusBar but in between QMenuBar and QToolBar.
I tried to implement using QToolBar.
// toolbar Banner with lable inside it.
QLabel * bannerLabel = new QLabel(" bannerToobar with label banner.");
bannerLabel->setAlignment( Qt::AlignVCenter );
ui.bannerToobar->addWidget( bannerLabel );
ui.bannerToobar->setAllowedAreas(Qt::ToolBarArea::TopToolBarArea);
ui.bannerToobar->setMovable( false );
QSize banner_sz = ui.bannerToobar->size();
ui.bannerToobar->setFixedHeight( banner_sz.height() * 2 );
QSizePolicy banner_szPolicy( QSizePolicy::Policy::Maximum, QSizePolicy::Policy::Fixed );
banner_szPolicy.setHorizontalStretch(255);
ui.bannerToobar->setSizePolicy( banner_szPolicy );
but i can't prevent user from draging mainToolbar and droping in the same row as my
bannerToolbar
You can force it to wrap initially using QMainWindow::addToolBarBreak, but I don't know a way to prevent it from being put back there later by the user (except for making the toolbars non-moveable).
If there was a QToolbar::dockLocationChanged signal (which seems to have been requsted and resolved in https://bugreports.qt-project.org/browse/QTBUG-1274, but I still don't see the signal anywhere), I suppose you could use insertToolBarBreak to fix it up whenever things have changed. Maybe there's some hackish way you could get notified when the toolbars move.
Or you could use QMainWindow::setMenuWidget to place a widget containing both your QMenuBar and something else into the menu area of the QMainWindow. This could get tricky if you want to support styles (mac/gnome/etc) where the menubar gets lifted out of your window to the top-of-screen and the toolbar gets unified with the window title decorations. But the idea of a banner between menubar and toolbar just naturally has problems in such cases :-)

Need to display the widgets inside my QListWidget with an offset, basically shifted a bit to the right

I have a panel.ui file done using QTDesigner. It's a QFrame class, rectangular shape with few labels on it. And I have a QListWidget class where I insert 3 instances of the panel.ui.
I create a QListWidgetItem and then use List->SetItemWidget(..) to populate my list.
The Result is a list filled with three panels. I was also able to move the panels inside the list using dragDropMode internalMove.
I also tested the ability to shift the panels a bit to the right when I click on them and that worked:
in procedure List::mousePressEvent(QMouseEvent *event)
Panel *child = static_cast<Panel*>(childAt(event->pos()))
...
int y= child->pos().y();
int x = child->pos().x();
child->move (x +10, y); `
Problem: When I run the app and display the list, I want all the panels to be displayed with that 10 offset to the right. So in the List constructor and inside the loop after this->setItemWidget(myPanelItem, myPanel); I try using myPanel->move() like above but it doesn't seem to work.
I run the app, the panels are displayed without my offset ( not sure why?) but when I click on one, it shifts.
move() won't work reliably since the widgets are in a layout. (Well, not a layout as in a QLayout, but the effect is comparable: When any metric in your application changes, e.g. you resize or scroll the list, the widgets are repositioned by the list widget.)
What you can do is wrap your actual widget in a container widget with a layout margin:
QWidget* wrapIntoContainerForOffset(QWidget* widget, int offset /*in pixels*/) {
QWidget* container = new QWidget;
QHBoxLayout* layout = new QLayout(container);
layout->setContentsMargins(/*left=*/ offset, /*others=*/ 0, 0, 0);
layout->addWidget(widget);
return container;
}
Then you add these containers to the listwidget instead.
Have You tried StyleSheets. The QListWidget supports Box model( http://doc.qt.digia.com/qt/stylesheet-customizing.html#box-model ). So You may want to try playing around with margins in the stylesheets.
Style sheet reference: http://doc.qt.digia.com/qt/stylesheet-reference.html

How to create a scrollable QVBoxLayout?

I'm trying to put a QVBoxLayout inside a QScrollArea in order for it to be scrollable vertically. However items don't seem to be added to it.
I saw a suggestion that I ought to create an inner widget that the ScrollArea uses and to place the layout inside that, although it doesn't seem to have worked. My structure is supposed to look like this:
+-------------------------------
| QScrollArea(realmScroll)
| +----------------------------
| | QWidget(realmScrollInner)
| | +-------------------------
| | | QVBoxLayout(realmLayout)
And the code to do this:
# Irrelevant, added for context (this works)
centralWidget = QWidget(self)
self.container = QVBoxLayout(centralWidget)
centralWidget.setLayout(self.container)
self.setCentralWidget(centralWidget)
# Where trouble starts
self.realmScroll = QScrollArea(self.container.widget())
self.realmScroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.realmLayout = QVBoxLayout(self.container.widget())
self.realmScrollInner = QWidget(self.realmScroll)
self.realmScrollInner.setLayout(self.realmLayout)
self.realmScroll.setWidget(self.realmScrollInner)
self.container.addWidget(self.realmScroll)
# Doesn't add to realmLayout
self.realmLayout.addWidget(QLabel("test"))
I'm still learning Qt (2 days in), so in-depth answers to where I'm going wrong would be appreciated.
Update:
It seems that the addWidget(QLabel()) works right up until the realmScrollInner has been set as realmScroll's widget. Since I'd like to add elements after the UI has been displayed I have to do this, which I'm not sure is really correct:
self.realmLayout.addWidget(QLabel("test"))
# realmScrollInner bound to realmScroll
realmScroll.setWidget(realmScrollInner)
self.container.addWidget(realmScroll)
# Access realmScroll's widget and then layout to add
realmScroll.widget().layout().addWidget(QLabel("test"))
But if you remove that first call to addWidget before the widget has been bound (so the layout has no widgets), then bind to the ScrollArea widgets added afterwards are not displayed. Perhaps the ScrollArea needs repainting (although I don't see a method for that)?
Update 2: Calling repaint() on realmScroll or its contained widget does nothing, as does calling activate/update() on the layout.
It turned out that I was lead down a wrong path by putting the layout as the layout of a widget. The actual way to do this is as simple as:
scrollarea = QScrollArea(parent.widget())
layout = QVBoxLayout(scrollarea)
realmScroll.setWidget(layout.widget())
layout.addWidget(QLabel("Test"))
Which I'm pretty sure I tried originally, but hey it's working.
However this adds an issue that the layout's items are shrunk vertically instead of causing the scrollarea to add a scrollbar.
OK, I just got done fighting with this. Here's a widget that can go into a scroll area (scrollarea->setWidget) and work correctly. It contains a QVBoxLayout and a list of label/listwidget pairs, each in their own little horizontal layout, and it does pretty much what you'd want.
The important thing was reading the QScrollArea docs section on Size Hints and Layouts, and finding the bit where having the sizeContraint QLayout::SetMinAndMaxSize on the layout would be necessary.
class MappingDisplayWidget : public QWidget
{
Q_OBJECT
public:
explicit MappingDisplayWidget(QWidget *parent = 0);
void addFile(QString name);
private:
QVBoxLayout *m_layout;
QMap<QString, QListWidget *> m_mappings;
};
MappingDisplayWidget::MappingDisplayWidget(QWidget *parent) :
QWidget(parent)
{
m_layout = new QVBoxLayout;
m_layout->setSizeConstraint(QLayout::SetMinAndMaxSize);
setLayout(m_layout);
}
void MappingDisplayWidget::addFile(QString name) {
if (m_mappings.find(name) == m_mappings.end()) {
QWidget *widg = new QWidget;
QHBoxLayout *lay = new QHBoxLayout;
widg->setLayout(lay);
QLabel *nlab = new QLabel(name);
lay->addWidget(nlab);
QListWidget *list = new QListWidget;
lay->addWidget(list);
m_layout->addWidget(widg);
m_mappings[name] = list;
}
}
I keep pointers to the list widgets so that I can add stuff to them later, and that works fine.
Try calling
self.realmScroll.setWidgetResizable(True)

Qt4 expanding tabs in QTabBar

I am subclassing QTabWidget to add a QTabBar, who's tabs stretch over the whole width of the tabBar. Therefore I am setting the expanding property to true. This doesn't seem to change anything about the behavior of the tabs.
Did anyone encounter the same problem? I use Qt 4.6 in combination with
TabWidget::TabWidget(QWidget *parent)
{
tabBar = new QTabBar(this);
tabBar->setIconSize(QSize(160,160));
tabBar->setExpanding(true);
setTabBar(tabBar);
}
EDIT: has been solved, here is how I implemented it, in case anyone is interested:
tabBar = new QTabBar(this);
tabBar->setExpanding(true);
layout = new QVBoxLayout(this);
setLayout(layout);
stackedLayout = new QStackedLayout();
layout->addWidget(tabBar);
layout->addLayout(stackedLayout);
connect(tabBar, SIGNAL(currentChanged(int)), stackedLayout, SLOT(setCurrentIndex(int)));
void MainWindow::addTab(QWidget *widget, const QIcon &icon, const QString &label) {
tabBar->addTab(icon, label);
stackedLayout->addWidget(widget);
}
From the QTabBar source code:
// ... Since we don't set
// a maximum size, tabs will EXPAND to fill up the empty space.
// Since tab widget is rather *ahem* strict about keeping the geometry of the
// tab bar to its absolute minimum, this won't bleed through, but will show up
// if you use tab bar on its own (a.k.a. not a bug, but a feature).
To get around this "feature", you can create your own tab widget using a QTabBar above a widget with a QStackedLayout.
5.2.0 onwards
QTabWidget::tab-bar {
min-width: 999999px;
}
It will work. No need to use any combination. You can use QTabWidget. Daniel ans is correct.
Building on #baysmith's answer, an easier way to force the QTabWidget to let the QTabBar expand, is to set a stylesheet on your QTabWidget that looks something like this:
QTabWidget::tab-bar {
width: 999999px;
}
Or another ridiculously large number. If your QTabWidget has the tabs going vertically instead of horizontally, use 'height' instead:
QTabWidget::tab-bar {
height: 999999px;
}
This seems to work fine for me, with Qt 5.0.1. The tabs expand to fill the space, each getting an equal portion. However, it seems like they intentionally leave enough empty space for one more tab, regardless. But the rest of the space is filled as desired. The empty space might be reserved for the tear/scroll buttons, incase too many tabs get added - but I'm not sure.

Resources