how to add an widget into the Form In QtDesigner - qt

in qdesigner_workbench.cpp, how can I add a widget (say QLabel) into a FormWindow by code?
Since methods like createWidget()...etc are all abstract, how do I properly use the internal mechanics to add QLabel into the active FormWindow?
EDIT:
In qdesigner_workbench.cpp, this is currently what I have:
QDesignerFormWindowManagerInterface* fwm = core()->formWindowManager();
QDesignerFormWindowInterface* fw = fwm->activeFormWindow();
QWidget* mw = fw->mainContainer();
QLabel* label = new QLabel(mw); //can be added correctly but not in the right hierarchy
label->setText("I am a good girl.");
The mw (obtained from fw->mainContainer()) is actually a MainWindow, however the real data I need is in:
mw -> children[2] (which is a QDesignerWidget) -> children
There are 9 widgets in the designer, and you can see there's 9 arrays in children mentioned above; see this link (an image) for illustration.
http://img24.imagevenue.com/img.php?image=98871_a_122_476lo.jpg
So... how can I correctly add the QLabel widget?
Tried both
QLabel* label = new QLabel(fw); // will be a sibling of MainContainer, which is the QMainWindow (mw) in this case
QLabel* label = new QLabel(mw); // will be a sibling of QDesignerWidget
and apprarently either of the works.

If you want just to display a widget on a form, you can set your QMainWindow or QDialog to be the widget parent:
QLabel *l = new QLabel(this);
l->setText("My long string");
Where this is a pointer pointing to your current QDialog or QMainWindow.
Otherwise as ufukgun pointed out, you can use setCentralWidget if you need your widget to occupy the center of the QMainWindow.

You should add any QWidget to the QLayout of the form.This will put it into the display strategy of the form when resizing it.
form->ui->layout->add(yourQWidget);
Depending of the QLayout you are using, parameters of the add function will not be the same.

create a widget and add it to your main window as it is your central widget
mainWindow->setCentralWidget(centralWidget);
if you want to add a label, you can add it to this central widget

Related

QDockWidget with QStatusBar possible?

For a QMainWindow I can easily set a status bar. When I have a floating QDockWidget, it behaves like a normal window (from user's perspective).
What I want to archive is to add a QStatusBarto the floating QDockWidget. With the signal topLevelChanged I could hide the status bar when the window is docked.
But can I add a status bar to a QDockWidget? At least in the Qt Creator form builder I can not (I have a context menu "Create Status Bar" for QMainWindow, but not for QDockWidget).
Any way to do it a runtime?
Just do this:
QStatusBar *bar = new QStatusBar;//in constructor for example
bar->showMessage(tr("Ready"));
ui->dockWidget->setWidget(bar);
In this case QStatusBar will be as widget, but you can also use some QWidget as container and add layout where your QStatusBar will be always bottom.
With QProgressBar:
QStatusBar *bar = new QStatusBar;
QProgressBar *pr = new QProgressBar;
bar->showMessage(tr("Ready"));
pr->setValue(50);
bar->addPermanentWidget(pr);
ui->dockWidget->setWidget(bar);
Result(there is "Ready" too, but you can't see it because of size of my window) :
Little example with another part of your question:
QStatusBar *bar = new QStatusBar;
QProgressBar *pr = new QProgressBar;
bar->showMessage(tr("Ready"));
pr->setValue(50);
bar->addPermanentWidget(pr);
ui->dockWidget->setWidget(bar);
connect( ui->dockWidget,&QDockWidget::topLevelChanged,[=](bool visible)
{
if(visible)
bar->hide();
else
bar->show();
});
I used here C++11 (CONFIG += c++11 to .pro file) and new syntax of signals and slots, but of course you can use old syntax if you want.

Reversing the layout of a QToolButton

I would like to make a QToolButton where the text is the left of its icon. I've tried to find related information, and tried things with:
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
but this sets the text to the right side of the button. I've looked into trying stylesheets, but text-alignment is only for push buttons. Is there any way to do this?
My highest-level goal is to make an interface bar which looks like this:
with a text label and image right beside it. Currently I'm using a toolbar with checkable toolbuttons because of the style (no border unless moused-over, still has the indent with checkable, contains text and an icon...). It's entirely possible that I'm using the wrong type widgets, so if I can't change the layout of this, is there any way to emulate this style?
This is what I currently have:
.
You can use QWidget::setLayoutDirection:
toolbar->setLayoutDirection(Qt::RightToLeft);
Your best bet is probably to create this functionality yourself in a QWidget. It should have a QLabel and a QToolButton (or QPushButton) arranged in a QHBoxLayout. Then you can add this custom widget to the toolbar using QToolBar::addWidget() or QToolBar::insertWidget().
Here's an example of a widget you could use:
#include <QtGui>
class CoolButton : public QWidget
{
public:
CoolButton(QWidget *parent = 0) : QWidget(parent)
{
QLabel *label = new QLabel("Hi!", this);
QToolButton *button = new QToolButton(this);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(label);
layout->addWidget(button);
setLayout(layout);
}
};

How to Add QListView/QListWidget to QGraphicsScene in Qt?

How to Add QListView/QListWidget to QGraphicsScene and add Widgets to ListView
When i Try to add QLisView to QGraphicsScene mouse scroll affects goes from Scene.
I want to add QPushButtons as ListView Items in QgraphicsScene with mouse scroll affect.
Thanks.
What about QGraphicsProxyWidget?
QListView *listView = new QListView;
QGraphicsProxyWidget *proxy = scene.addWidget(listView);
Then (or before that) you can populate the list with anything you want. QPushButton can be added to the list using setIndexWidget(). Also you might rethink the whole idea of having a QListView, and give it a try with QScrollArea and a linear layout containing buttons. That would require a bit more logic to organize items within the scroll area, but it should be more lightweight that QListView with custom widgets.
I second the answer above: ProxyWidget is the answer.
Here is my working code,
Header:
class CenterScreen{
private:
QListWidget* nameListWidget;
QGraphicsProxyWidget* nameProxyWidget;
...
C++ source:
void CenterScreen::addListView()
{
QGraphicsScene* scene = ui.centerGraphicsView->scene();
nameListWidget = new QListWidget();
nameProxyWidget = scene->addWidget(nameListWidget);
...
nameProxyWidget->hide(); // you can control your widget as you like

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)

How to add a scrollbar to parent QWidget

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.

Resources