How to lock all toolbars via menu in Qt? - c++

I am experimenting with Qt Creator and the Application Example.
I would like to add a checkable menu entry to a toolbar menu that reads "Lock toolbars" and, when checked, locks the positions of all tool bars. I guess that it is a quite common feature.
I have managed to find a command that locks single bars via :
toolBar->setMovable(false);
But I cannot figure out how to lock all toolbars.
Edit
This question used to contain an inquiry concerning the toolbar context menu rather than the standard menu. Since I got an answer concerning the context menu elsewhere I removed it from this question.
How to add an entry to toolbar context menu in qt?

Here is an example of how you can achieve it. First, add a QAction and a QMenu; also, declare all your toolbars private :
private:
QMenu* m_pLockMenu;
QToolBar* m_pFileToolBar;
QToolBar* m_pEditToolBar;
QToolBar* m_pHelpToolBar;
QAction* m_pLockAction;
Also, declare a slot where you will manage the locking of your toolbars when the action will be triggered :
public slots :
void lockActionTriggered();
Implement your slot. You just need to lock all the toolbars :
void lockActionTriggered()
{
m_pFileToolBar->setMovable(false);
m_pEditToolbar->setMovable(false);
m_pHelpToolBar->setMovable(false);
}
Now, you just have to declare your main window in your .cpp, and add the menu, the toolbars and the action in it :
QMainWindow* mainWindow = new QMainWindow();
m_pLockMenu = mainWindow->menuBar()->addMenu("Lock Toolbars");
m_pFileToolBar = mainWindow->addToolBar("File");
m_pEditToolBar = mainWindow->addToolBar("Edit");
m_pHelpToolBar = mainWindow->addToolBar("Help");
m_pLockAction = new QAction("Lock", this);
Now, add the action to the menu :
m_pLockMenu->addAction(m_pLockAction);
And connect the QAction's signal triggered() to your slot :
connect(m_pLockAction, SIGNAL(triggered()), this, SLOT(lockActionTriggered()));
Don't forget to show() your main window :
mainWindow->show();
And it should be working now!
EDIT
Your code must look like this :
In the mainwindow.h :
class MainWindow : public QMainWindow
{
...
private:
QMenu* m_pLockMenu;
QToolBar* m_pFileToolBar;
QToolBar* m_pEditToolBar;
QToolBar* m_pHelpToolBar;
QAction* m_pLockAction;
public slots :
void lockActionTriggered();
};
In the main.cpp:
int main(int argc, char *argv[])
{
...
QApplication app(argc, argv);
MainWindow window;
window.show();
app.exec();
}
In the mainwindow.cpp:
void MainWindow::createActions()
{
m_pLockMenu = menuBar()->addMenu("Lock Toolbars");
m_pFileToolBar = addToolBar("File");
m_pEditToolBar = addToolBar("Edit");
m_pHelpToolBar = addToolBar("Help");
m_pLockAction = new QAction("Lock", this);
m_pLockMenu->addAction(m_pLockAction);
connect(m_pLockAction, SIGNAL(triggered()), this, SLOT(lockActionTriggered()));
...
}
void MainWindow::lockActionTriggered()
{
m_pFileToolBar->setMovable(false);
m_pEditToolbar->setMovable(false);
m_pHelpToolBar->setMovable(false);
}

If you have a pointer to your mainwindow, you can simply find all the toolbars and iterate over them:
// in slot
for (auto *t: mainWindow->findChildren<QToolBar*>())
t->setMovable(false);
Alternatively, you might want to connect all toolbars to a single toggle action, when you initialise the UI:
// in constructor
for (auto *t: this->findChildren<QToolBar*>())
connect(action, &QAction::toggled, t, &QToolBar::setMovable);

One common example with flexible lock options
# Created by BaiJiFeiLong#gmail.com at 2022/2/15 22:56
import typing
from PySide2 import QtWidgets, QtGui, QtCore
class Window(QtWidgets.QMainWindow):
def createPopupMenu(self) -> QtWidgets.QMenu:
menu = super().createPopupMenu()
menu.addSeparator()
toolBars: typing.Sequence[QtWidgets.QToolBar] = self.findChildren(QtWidgets.QToolBar)
for toolbar in toolBars:
if toolbar.rect().contains(toolbar.mapFromGlobal(QtGui.QCursor.pos())):
title = "%s %s" % ("Lock" if toolbar.isMovable() else "Unlock", toolbar.windowTitle())
menu.addAction(title, lambda toolbar=toolbar: toolbar.setMovable(not toolbar.isMovable()))
menu.addSeparator()
if any(x.isMovable() for x in toolBars):
menu.addAction("Lock All", lambda: list(x.setMovable(False) for x in toolBars))
if any(not x.isMovable() for x in toolBars):
menu.addAction("Unlock All", lambda: list(x.setMovable(True) for x in toolBars))
return menu
app = QtWidgets.QApplication()
window = Window()
window.setWindowTitle("Toolbar Example")
for i in range(1, 6):
toolBar = window.addToolBar(f"ToolBar{i}")
toolBar.setMovable(i % 2 == 0)
toolBar.addWidget(QtWidgets.QPushButton(f"ToolBar {i}"))
label = QtWidgets.QLabel("Ready")
label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
window.setCentralWidget(label)
window.resize(800, 600)
window.show()
app.exec_()

Related

Qt: Show popup menu, close if I click anywhere outside of it

I want to show a menu in Qt with the following functionality: clicking anywhere outside of the menu will close it, but the click will be processed by the receiving widget. As far as I know, this rules out using setWindowFlag(Qt::Popup) because the click that closed the popup will be 'eaten' and not processed any further. I'd love to know if there is a way around this.
For the time being, I'm overriding focusOutEvent like so:
class Menu : public QWidget
{
Q_OBJECT
public:
Menu(QWidget* parent = nullptr)
: QWidget(parent){
setWindowFlags(Qt::FramelessWindowHint);
resize(200, 100);
}
public slots:
void showMenu(QPoint point)
{
move(point);
show();
setFocus(); // so that focusOutEvent will be trigerred
}
protected:
void focusOutEvent(QFocusEvent *event) override
{
hide();
}
};
And I'm using it like this:
class Window : public QMainWindow
{
Q_OBJECT
public:
Window(): m_menu()
{
auto* list = new QListWidget();
list->addItems(QStringList() << "item1" << "item2" << "item3");
connect(list, &QListWidget::itemClicked, [this](){
m_menu.showMenu(QCursor::pos());
});
// Layout stuff
auto central_widget = new QWidget();
auto layout = new QVBoxLayout(central_widget);
layout->addWidget(list);
layout->addItem(new QSpacerItem(20,200, QSizePolicy::Minimum, QSizePolicy::Expanding));
setCentralWidget(central_widget);
}
public:
~Window(){}
private:
Menu m_menu;
};
Single clicking on any of the items in the list will show the menu and clicking anywhere on the grey background will close the menu.
The issue I'm facing is that when the menu is shown, focus is lost from the main window (photo on the right, the titlebar buttons and selected item are greyed out). Focus is lost because of calling setFocus on the menu but if I don't use it then the QFocusOutEvents are not trigerred and the menu doesn't close.
The same happens if I use setAttribute(Qt::WA_ShowWithoutActivating) on the menu.
So here are my questions:
Is there a way to use Qt::Popup without the 'eaten' clicks issue?
If not, is there a way to show the menu without losing the focus from the main window but still trigger the focusOutEvents as necessary?
Using Qt 5.15.1 on MacOS 11.2.3. Thanks for reading through my long post!

When QDockWidget with QWebEngineView is undocked, other docked widgets don't respond to user action

I have multiple dockwidgets in my main window and everything works fine until I add a dockedwidget with QwebEngineView as the widget.
When a dockwidget with QwebEngineView as a child is undocked, user inputs such as scroll bars in the other docked widgets are ignored. When the dockwidget with QWebEngineView is docked, it works fine.
The code I am testing with is the example project in
..\Qt 5.6\widgets\mainwindows\dockwidgets
with minor modification to add one more dockedwidget with QWebEngineView (see below).
///// added this in void MainWindow::createDockWindows()
WebView* w_view = new WebView(nullptr);
dock = new QDockWidget(tr("WebView"), this);
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
dock->setWidget(w_view);
addDockWidget(Qt::LeftDockWidgetArea, dock);
viewMenu->addAction(dock->toggleViewAction());
/// the widget with QWebEngineView
class WebView : public QWidget {
Q_OBJECT
public:
WebView(QWidget* parent = 0)
:QWidget(parent)
{
setObjectName("WebView");
m_webEngineView = new QWebEngineView(this);
m_webEngineView->load(QUrl("http://qt-project.org/"));
}
~WebView() {}
private:
QWebEngineView* m_webEngineView;
};
///////////////////////////
Any help?

With QToolBox, which setting to have page be only its content size?

I'm trying to find the settings or size policy so that each page in my QToolBox instance only takes up the space needed by its content. I've tried everything I could see in the properties for both the instance and for each of the individual pages.
Am I misconstruing the functionality of QToolBox widget or just missing the right setting?
What I am going for is something similar to the accordion fold type widget in Qt Creator:
I can't seem to get this "Sort" page to take only the size needed to display the button and field.
Unfortunately you can't do that directly because it will span all the available space that the title widgets don't occupy. You can emulate what you want by setting a fixed height on the QToolBox if you know the exact height your page(s). But you do not want to do that in practise.
If you want the behavior you ask for then you need to write your own custom control. It doesn't have to be hard. Use a QVBoxLayout and fill into it items of a custom class, let's call it ToolItem, which is a QWidget with a title (perhaps a button to show/hide) and another QWidget for showing the contents that is either visible or not.
The following very simple example will toggle the visibility of the ToolItem when it is clicked. And only when visible will it occupy any space.
class ToolItem : public QWidget {
public:
ToolItem(const QString &title, QWidget *item) : item(item) {
QVBoxLayout *layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(new QLabel(title));
layout->addWidget(item);
setLayout(layout);
item->setVisible(false);
}
protected:
void mousePressEvent(QMouseEvent *event) {
item->setVisible(!item->isVisible());
}
private:
QWidget *item;
};
class ToolBox : public QWidget {
public:
ToolBox() : layout(new QVBoxLayout) {
setLayout(layout);
}
void addItem(ToolItem *item) {
// Remove last spacer item if present.
int count = layout->count();
if (count > 1) {
layout->removeItem(layout->itemAt(count - 1));
}
// Add item and make sure it stretches the remaining space.
layout->addWidget(item);
layout->addStretch();
}
private:
QVBoxLayout *layout;
};
And simple usage of it:
QWidget *window = new QWidget;
window->setWindowTitle("QToolBox Example");
QListWidget *list = new QListWidget;
list->addItem("One");
list->addItem("Two");
list->addItem("Three");
ToolBox *toolBox = new ToolBox;
toolBox->addItem(new ToolItem("Title 1", new QLabel("Some text here")));
toolBox->addItem(new ToolItem("Title 2", list));
toolBox->addItem(new ToolItem("Title 3", new QLabel("Lorem Ipsum..")));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(toolBox);
window->setLayout(layout);
window->resize(500, 500);
window->show();
You can now tweak it to look like the QToolBox if needed.
Please don't hesitate to ask follow-up questions.
The example shown from Qt Designer may not be using a QToolBox, which behaves more like a stacked tab widget only displaying a single page at a time. The example in Qt Designer appears to be a QTreeWidget with custom drawing or styling.
This is not the complete answer.
I traced down the actual component, it can be included outside designer (kind of). Here is a minimal example showing how to do that (modified from https://github.com/zdenekzc/qtdesigner-integration).
form.h
#ifndef FORM_H
#define FORM_H
#include <QMainWindow>
class FormWindow : public QMainWindow {
Q_OBJECT
public:
explicit FormWindow (QWidget * parent = 0);
};
#endif // FORM_H
form.cc
#include "form.h"
#include <QApplication>
#include <QtDesigner/QtDesigner>
#include <QtDesigner/QDesignerComponents>
FormWindow::FormWindow (QWidget* parent) : QMainWindow (parent) {
QDesignerFormEditorInterface* core = QDesignerComponents::createFormEditor (this);
core->setWidgetBox (QDesignerComponents::createWidgetBox (core, 0));
this->setCentralWidget (core->widgetBox());
}
extern "C" int main (int argc, char * * argv) {
QApplication app (argc, argv);
FormWindow * win = new FormWindow ();
win->show ();
return app.exec();
}
qt-designer.pro
QT += designer
HEADERS = form.h
SOURCES = form.cc
LIBS += -lQt5DesignerComponents
Build it:
mkdir -p build
cd build
qmake-qt5 ../qt5-design.pro
make
./qt5-design
This is obviously not useful by itself unless you want to build a designer but another step towards isolating the actual component.

How to change QStackedWidget index from Qdialog

My application have an "actionhelp" in the menu bar which when clicked opens up a QDialog that contains an ok button at the other side in the mainwindow i have a QStackedWidget
So my question is how to change the index of the stackedwidget when i press that ok button in the QDialog??
Signals and slots. Connect the signal from the ok button (or emit one of your own when checking QDialog::Accepted after it closes) to a slot that will change the index in the QStackedWidget.
Example code:
Create and connect QAction in main method:
QAction *displayDialog = new QAction("Display Dialog", this);
connect(popup, SIGNAL(triggered()), this, SLOT(showDialog()));
Display Dialog:
void showDialog()
{
YourDialog *dialog = new YourDialog(this);
int return_code = dialog.exec();
if (return_code == QDialog::Accepted)
{
int index = someValue;
qStackedWidget.setCurrentIndex(index);
}
}
Assuming you have a line edit on your dialog and you want to change the index of stacked widget based on the line edit value ( or a spin box ):
//your dialog
//the constructor
YourDialog::YourDialog(QWidget*parent)
:QDialog(parent)
{
connect(ur_ok_btn, SIGNAL(clicked()), SLOT(accept ()));
}
//access to line edit value
QString YourDialog::getUserEnteredValue(){return ur_line_edit->text();}
Where you create an instance of YourDialog class :
//your main window
YourDialog dlg;
if( dlg.exec() == QDialog::Accepted ){
int i = dlg.getUserEnteredValue().toInt();
ur_stacked_widget->setCurrentIndex(i);
}

Automatically populate "Edit" menu in menubar with current focus widget context menu

I've been looking for ways to implement the "Edit" menu of Qt application. The "Edit" menu contains items such as "Copy", "Cut", "Paste", etc. and which need to forward to the currently active widget.
I can't seem to find a standard or elegant way to do this. According to this question, it's not possible:
How to implement the "Edit" menu with "Undo", "Cut", "Paste" and "Copy"?
I recently had the idea to trigger a context menu event on the current active widget when the "Edit" menu is shown, via:
// create menus in MainWindow constructor
...
edit_menu = menuBar()->addMenu(tr("&Edit"));
connect(edit_menu, SIGNAL(aboutToShow()), this, SLOT(showEditMenu()));
...
// custom slot to handle the edit menu
void MainWindow::showEditMenu()
{
QWidget* w = QApplication::focusWidget();
// show the context menu of current focus widget in the menubar spot
QPoint global_pos = edit_menu->mapToGlobal(edit_menu->rect().bottomLeft());
QPoint pos = w->mapFromGlobal(global_pos);
QApplication::sendEvent(w, new QContextMenuEvent(QContextMenuEvent::Keyboard, pos, global_pos));
}
This shows the a context menu for the current widget great, but has some problems. For example, it takes focus away from the menubar, or if you click a different menubar item first, the menubar has focus, etc.
One partial solution would be to grab the context menu from the widget and copy it's items into the edit menu dynamically. Is there a way to do this?
Is there a better way build the edit menu in Qt?
Thanks for your help.
well, if you just need to create menu, you can always take actions from actions of a widget. To create edit actions for widgets, you can do something like this:
void MainWindow::addActions (QWidget* widget)
{
QAction * copyAction = new QAction("copy",widget);
if(connect(copyAction,SIGNAL(triggered()),widget,SLOT(copy())))
{
widget->addAction(copyAction);
qDebug()<<"success connection";
}
}
and
foreach (QObject * obj, centralWidget()->children())
{
QWidget * w = dynamic_cast<QWidget*>(obj);
if (w)
addActions(w);
}
then you always can update actions of edit menu with focused widget's actions
This may be not elegant, but it better, than imporssible. The main bad assumption in example is that copy slot is named copy
An elegant solution I guess would be to have a base class for the widgets you need to have copy/paste/... functionality, and have them register themselves with some parent class upon becoming active and unregistering when being deactivated. The actions can then just be connected to slots in the main window, which forwards them to the registered widget. You could even gray out the menu items if no widget is currently registered (e.g. because the active widget does not have the required functionality).
An example for registering/unregistering (untested):
class ActionWidget;
class ActionWidgetManager
{
public:
ActionWidgetManager() : actionWidget_(0){}
void registerWidget(ActionWidget* widget){ actionWidget_ = widget; }
void unregisterWidget(ActionWidget* widget)
{ if (actionWidget_ == widget) actionWidget_ = 0; }
bool hasActiveWidget() const{ return actionWidget_ != 0; }
ActionWidget* getActiveWidget(){ return actionWidget_; }
private:
ActionWidget* actionWidget_;
};
class ActionWidget : public QWidget
{
public:
ActionWidget(ActionWidgetManager* manager, QWidget* parent=0)
: manager_(manager), QWidget(parent) {}
~ActionWidget(){ manager_->unregisterWidget(this); }
void Widget::changeEvent(QEvent *event)
{
QWidget::changeEvent(event);
if(event->type() == QEvent::ActivationChange){
if(isActiveWindow()) {
manager_->registerWidget(this);
}
else {
manager_->unregisterWidget(this);
}
}
}
virtual void doCopy() = 0;
virtual void doPaste() = 0;
virtual void doUndo() = 0;
virtual void doCut() = 0;
private:
ActionWidgetManager* manager_;
};
Or something equivalent using signals and slots.