## Using QWidgetActions to place arbitrary widgets inside command-menus.

Again, on the subject of finding unusual ways to use the Qt GUI Library, to solve certain atypical problems that can exist in the design of applications. What a programmer might want to do, beyond giving his application command-menus, that fire Signals, which are received by Slots, which in turn evoke the capabilities of his or her program. The programmer may feel that he needs an entry in this command-menu to have an unusual appearance, such as, to consist of blue text, with a background that goes from being neutral to being yellow, when this entry is hovered over with the mouse…

Well, Qt has a special base-class for that sort of thing, which is called ‘QWidgetAction’. The idea behind it is, that it inherits the ‘QAction’ class, that would normally be added to the menus, but in such a way, that it additionally connects with a ‘QLabel’ object, which in turn can be given the most striking appearances, including rich text in the form of HTML.

This is what the result looks like:

The ‘Reset Program’ command is different in appearance, from the other menu entries.

This is the code that was required, to make it happen:

The file ‘menubar.h‘ –



#include "mainwindow.h"

{

public:

};

class HoverWidget : public QLabel
{
public:
HoverWidget(QWidget *parent);

void enterEvent(QEvent * event);
void leaveEvent(QEvent * event);
};




The file ‘menubar.cpp‘ –


#include "mainwindow.h"
#include <QApplication>
#include <QWidgetAction>

: QWidget(parent)
{

QAction *quit = new QAction("&Quit", this);
quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));

//    QAction *reset_window = new QAction(this);
//    reset_window->setText("Re&set Program!");
//    reset_window->setShortcut(Qt::CTRL + Qt::Key_0);

QAction *zoom_in = new QAction("Zoom I&n", this);
QAction *zoom_out = new QAction("Zoom Ou&t", this);

zoom_in->setShortcut(Qt::CTRL + Qt::Key_Plus);
zoom_out->setShortcut(Qt::CTRL + Qt::Key_Minus);

HoverWidget *reset_window_label = new HoverWidget(help);
reset_window_label->setText("Reset Program            Ctrl+0");
reset_window_label->setStyleSheet("QLabel {color: blue; margin: 4px}");
QWidgetAction *reset_window = new QWidgetAction(this);
reset_window->setDefaultWidget(reset_window_label);
reset_window->setShortcut(Qt::CTRL + Qt::Key_0);

//  Qt5 Semantics for Signals sent to Slots...

connect(quit, &QAction::triggered, qApp, &QApplication::quit);

connect(reset_window, &QAction::triggered, parent, &MainWindow::resetQ);
connect(zoom_in, &QAction::triggered, parent, &MainWindow::zoom_in_do);
connect(zoom_out, &QAction::triggered, parent, &MainWindow::zoom_out_do);

}

HoverWidget::HoverWidget(QWidget *parent) :
QLabel(parent)
{
setAttribute(Qt::WA_Hover, true);
}

void HoverWidget::enterEvent(QEvent *event)
{
setStyleSheet("QLabel {color: blue; background-color: yellow; margin: 4px}");
QLabel::enterEvent(event);
}

void HoverWidget::leaveEvent(QEvent *event)
{
setStyleSheet("QLabel {color: blue; margin: 4px}");
QLabel::leaveEvent(event);
}



The exercise in which I tested this feature can be found at the following URL within my own site:

https://dirkmittler.homeip.net/binaries/

And, the relevant compressed archives are named ‘Creator_Test3.tar.gz‘ and ‘Creator_Test3.zip‘.

(This is a link to the previous exercise.)

(Update 8/22/2020, 17h40: )