QDockWidget Draggable Tabs - c++

I am using QDockWidgets and placing two of them on the left side of my application so that tabs can be used to select between them. However, Qt's default behavior for this looks horrible and is unintuitive. Instead of being able to drag the tabs to move the widgets, it places another bar below the selected tab (with the same name) that must be dragged instead. As a user, it would be hard to figure this out.
(My QDockWidgets are "Attributes" and "Library")
Is there a way to get rid of this second bar and make it so I can move my QDockWidgets by dragging the tabs themselves?

If you are adding QTabWidgets to a main window derived from QMainWindow, you can try tabifyDockWidget.
It tabifies two QDockWidgets just like you wanted and of course you are able to drag them.
dockWidget1 = new QDockWidget("Tab1") ;
dockWidget2 = new QDockWidget("Tab2") ;
this->addDockWidget(Qt::LeftDockWidgetArea , dockWidget1 );
this->addDockWidget(Qt::LeftDockWidgetArea , dockWidget2 );
this->tabifyDockWidget(dockWidget1,dockWidget2);

I think, Tom was not too far away from a solution:
You can set your own Widget as title bar:
myDockingWidget->setTitleBarWidget(myTitleBar)
If you design this widget to not show the dock window title, you have it. Via the signal QDockWidget::topLevelChanged your docking widget can even become informed, when it gets floating, so you could then enable the title in myTitleBar again.

As far as I can see from QDockWidget::mousePressEvent implementation in src/gui/widgets/qdockwidget.cpp dragging the dockwidgets using tabs is NOT possible:
QDockWidgetLayout *dwLayout
= qobject_cast<QDockWidgetLayout*>(layout);
if (!dwLayout->nativeWindowDeco()) {
QRect titleArea = dwLayout->titleArea();
if (event->button() != Qt::LeftButton ||
!titleArea.contains(event->pos()) ||
// check if the tool window is movable... do nothing if it
// is not (but allow moving if the window is floating)
(!hasFeature(this, QDockWidget::DockWidgetMovable) && !q->isFloating()) ||
qobject_cast<QMainWindow*>(parent) == 0 ||
isAnimating() || state != 0) {
return false;
}
initDrag(event->pos(), false);
....
As you can see from the implementation one of the things that the QDockWidget checks before allowing undocking is whether the mouse press event has come from title bar or not.

have you tried:
myDockingWidget->setTitleBarWidget(0)
edit:
QWidget* titleWidget = new QWidget(this);
mUi.dockWidget->setTitleBarWidget(titleWidget);
where 'this' is a QMainWindow
this will remove the title bar, though im not sure how to make the QDockWidget draggable from the tabs

Edited:
Please do not use this method. It introduces problems rather than soloves them.
Maybe you can try this wierd way, that is move the QWidget in the dock widget area to the title bar.
I modify the demo in folder
C:\Qt\Qt5.12.9\Examples\Qt-5.12.9\widgets\mainwindows\dockwidgets
to show how it works:
In "void MainWindow::createDockWindows()"
QDockWidget *dock = new QDockWidget(tr("Customers"), this);
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
//make a panel to hold your widgets
QWidget *p = new QWidget(dock);
QVBoxLayout *l = new QVBoxLayout(p);
p->setLayout(l);
customerList = new QListWidget(p);
l->addWidget(customerList);
customerList->addItems(QStringList()
<< "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton"
<< "Jane Doe, Memorabilia, 23 Watersedge, Beaton"
<< "Tammy Shea, Tiblanka, 38 Sea Views, Carlton"
<< "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal"
<< "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston"
<< "Sally Hobart, Tiroli Tea, 67 Long River, Fedula");
dock->setWidget(new QWidget());//hide the real dock area
dock->setTitleBarWidget(p); //use the titlebar erea to show the content
The demo:
Drag the edge of the panel to move, actually you can drag the empty area (no child widget area). The widget on this panel still functional properly.

I also think that setTitleBarWidget() really does the trick. I remember seeing it being used for a similar purpose in the source code of the Amarok music player. Amarok has a QMainWindow which only contains dock widgets. You might want to have a look at the source code there.

It looks like you've set your dock tab position to be on the top. The default is for it to be on the bottom. Then it's not as visually jarring to have the tab text right next to the title bar text.
I don't think there's any way to do what you're proposing in Qt (eliminate the QDockWidget title bar and drag from the tab), at least not with the standard widgets. You could probably write a lot of custom code to make it happen, but that's probably not worth it.
Instead, I'd suggest moving the tabs to the bottom (see QMainWindow::setTabPosition) or possibly one of the sides.

Related

Qt Desktop on Mac: QFormLayout sizing with subpanels

TL;DR: I'm having a grow/shrink probably using embedded forms inside a MainWindow. I'm unsure what to try next.
Okay, I have another sizing problem.
This is a sample app of what I'm trying to do:
When I click on the various toolbar options, I intend to change the central widget contents accordingly. Maybe I should just use a tab widget, but I wanted to do it this way.
In the simplest form, with a widget layout like this:
I set the central widget's layout to Horizontal, and the Inner Widget to FormLayout then set the inner widget's expand rules to expand any expandable fields. As I resize the window, the simple line edit expands and contracts as desired.
When I click the bus icon in the toolbar, I swap out the contents of the central widget with a separate panel. That panel has a widget with a form layout, and is also set to expand and collapse. Here are the layout rules for the second panel:
My trigger code does this:
currentCenter = ui->innerWidget; // In the constructor
currentCenter->hide();
if (v1Form == nullptr) {
v1Form = new V1Form(ui->centralWidget);
}
v1Form->show();
currentCenter = v1Form;
I have tried various orders to this, and I tried using setCentralWidget(). In all cases, the new central area remains a fixed size, even though the original one expands and collapses.
What is working: I can readily change the inner contains for different forms. That's working great. (It took a while to figure it out.)
-or- I can make simple popup forms that grow and shrink properly.
What is not working is grow/shrink when I embed my form inside my central widget or if I use setCentralWidget.
I'm not sure what else to try.
Maybe I should just use a tab widget, but I wanted to do it this way.
You should definitely use a QTabWidget as your central widget. It is designed specifically for your use case, and it will greatly simplify your code.
My trigger code does this:
currentCenter = ui->innerWidget; // In the constructor
currentCenter->hide();
if (v1Form == nullptr) {
v1Form = new V1Form(ui->centralWidget);
}
v1Form->show();
currentCenter = v1Form;
With a QTabWidget, your trigger code can be simplified to:
ui->innerTabWidget->setIndex(1).
You don't need to dynamically construct a V1Form. Simply use Qt Designer to create multiple pages in your QTabWidget and implement all your subpanel widgets within your MainWindow.ui.
(Nonetheless, if you want to implement each subpanel in its own separate *.ui file, you can still promote each page in your QTabWidget to your custom widget.)
What is not working is grow/shrink when I embed my form inside my central widget or if I use setCentralWidget.
To address your original symptoms: Your widgets don't grow/shrink because you didn't put them inside a layout that is part of your main window.
I found a solution doing it the way I started. I had to add one line of code:
void MainWindow::switchForm(QWidget *widget) {
if (centralForm != widget) {
if (centralForm != nullptr) {
centralForm->hide();
centralForm = nullptr;
}
if (widget != nullptr) {
centralForm = widget;
centralForm->show();
ui->centralwidget->layout()->addWidget(centralForm);
}
}
}
void MainWindow::on_actionSetup_triggered()
{
if (setupForm == nullptr) {
setupForm = new SetupForm(ui->centralwidget);
}
switchForm(setupForm);
}
The missing line -- adding my new form to the layout:
ui->centralwidget->layout()->addWidget(centralForm);

QGraphicsItem loses focus after using QComboBox

I am doing a small game in Qt and try to use WASD to move my protagonist(QGraphicsPixmapItem). The map is quite big so I use a QComboBox to change the scale of the scene.
The game looks like this:
The simple game
How I built the protagonist:
protagonist = new MyProtagonist();
protagonist->setFlag(QGraphicsItem::ItemIsFocusable);
protagonist->setFocus();
scene->addItem(protagonist);
How I built the combox:
sceneScaleCombo = new QComboBox;
QStringList scales;
scales << tr("1%")<<tr("10%") << tr("20%") <<tr("50%") << tr("100%") <<tr("200%");
sceneScaleCombo->addItems(scales);
sceneScaleCombo->setCurrentIndex(4);
connect(sceneScaleCombo,SIGNAL(currentIndexChanged(QString)),this,SLOT(sceneScaleChanged(QString)));
void MainWindow::sceneScaleChanged(const QString &scale)
{
double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100;
QMatrix oldMatrix = view->matrix();
view->resetMatrix();
view->translate(oldMatrix.dx(),oldMatrix.dy());
view->scale(newScale,newScale);
protagonist->setFocus();
}
Everything worked well at the beginning. However, after I clicking the combobox, my protagonist cannot be controlled by keyboard any more. I need to click my protagonist to make it focused again.
Is there any way to set it focused automatically?
Thanks a lot.
By clicking QComboBox the QGraphicsView looses the focus. You attempt to return it back by calling setFocus(); on the QGraphicsPixmapItem. This approach doesn't work as you expect, because it sets the focus item inside the view, but the view itself still does not have focus. To fix that in MainWindow::sceneScaleChanged instead of protagonist->setFocus(); write view->setFocus();.

Qt QStatusBar wider separator

I am using qt to develop an embedded gui application. I am using 2 QStatusBars to make a menu-like buttons one can see on an osciloscope for example:
My problem is I dont know a proper way of separating the buttons from eachother with a certain width. In the picture you can see I have added couple separators to achieve that, but it doesnt look that way when run on the target.
Is there a better way to separate buttons on QStatusBar with certain width?
I'd prefer you use a blank widget to do the seperation as suggested by Martin, like so;
//the 2 widgets in the status bar
button1 = new QPushButton("Button1");
button2 = new QPushButton("Button2");
//the blank widget. You can set your width with 'setFixedWidth(int)'
widget = new QWidget;
widget->setFixedWidth(50);
widget->setHidden(1);
widget->setVisible(1);
//placing them in the status bar
statusBar = new QStatusBar;
statusBar->addWidget(button1);
statusBar->addWidget(widget);
statusBar->addWidget(button2);

How to logically group widgets in QT for easy show/hide?

I'm grouping a set of widgets in a parent and then I control the visibility/flow of these widgets by hiding/showing the parent. Is this a good way to achieve what I'm trying to do? Here is the code:
QVBoxLayout* l = new QVBoxLayout(this);
// .....
QWidget* toolset_frame = new QWidget(this);
{
QVBoxLayout* l = new QVBoxLayout(toolset_frame);
l->addWidget(new QLabel(tr("Stuff")));
this->Toolset = new QLineEdit(toolset_frame);
l->addWidget(this->Toolset);
}
l->addWidget(toolset_frame);
// Call toolset_frame->hide() and this hides everything inside the parent
The problem with this solution is that the children shrink in size slightly, I think this is due to some padding or border in the parent. Ideally the children should appear as if they are not contained in an intermediate object, but rather flow with the parent. In this case the horizontal size of the children should not be affected.
http://doc.qt.io/qt-5/qtwidgets-dialogs-extension-example.html
This example shows that your approach is correct. Using a widget to contain the elements you want to hide, and so on.
If you want the margins/content margins/padding to be less, then change it.
// in finddialog.cpp
extensionLayout->setMargin(0);
To quickly prototype what properties to change to get it to look right, try laying it out in the Qt Designer, and modify the property editor to get the look and feel you want.
Hope that helps.

How to set QListWidget in particular position

How do i set QListWidget in particular position say i have window size of(1000,1000) and i want to set QListWidget at position (200,200).
widget = new QWidget();
setCentralWidget(widget);
list1->setFixedSize(200,150);
list1->addItem("Surya TV");
list1->addItem("Sony TV");
list1->addItem("Zee TV");
vertical->addWidget(list1);
widget->setLayout(vertical);
You can use :
void move ( int x, int y )
Something like :
p = new QListWidget(this);
p->move(200,200);
In my case "this" is the QMainWindow.
Does it helps?
Please use spacers with predefined sizes to pad your list if you are going for layouts. It is better to go for layouts if you have more widgets along with the listWidget in your main form.
If you are unsure, please use Designer to build your forms. This will allow you to have the kind of control you wish achieve in terms of fine grained positioning.
Personally, I do not prefer designer though.