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:

Screenshot_20200821_021223_e

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‘ –

 


#ifndef MENUBAR_H
#define MENUBAR_H

#include "mainwindow.h"

class SimpleMenu : public QWidget
{

public:
    SimpleMenu(MainWindow *parent = 0);

};

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

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

#endif // MENUBAR_H


 

The file ‘menubar.cpp‘ –

 


#include "menubar.h"
#include "mainwindow.h"
#include <QApplication>
#include <QMenu>
#include <QMenuBar>
#include <QWidgetAction>

SimpleMenu::SimpleMenu(MainWindow *parent)
    : 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);

    QMenuBar *toplev = new QMenuBar(parent);

    QMenu *file;
    file = toplev->addMenu("&File");
    file->addAction(zoom_in);
    file->addAction(zoom_out);
    file->addSeparator();
    file->addAction(quit);

    QMenu *help;
    help = toplev->addMenu("&Help");

    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);

    help->addAction(reset_window);

    //  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: )

Continue reading Using QWidgetActions to place arbitrary widgets inside command-menus.

Yet another 3rd-Party, as in KDE-Store, Widget, that can help us Personalize the Linux Experience.

One of the subjects which I’ve been writing about is, that in principle, it’s possible to create a Linux, Plasma 5.8 (+) workspace, that resembles an OS/X computer somewhat, in that at least it has a dock. Well if one sets about to do that seriously, then one is also looking for ways to keep the total number of applets on that dock to a minimum, while giving the fastest possible access to all the features which we have a computer for.

Well one tool which can help users accomplish that, is The Places Widget, from the KDE Store. Don’t worry, the widgets available at this ‘store’ don’t cost any money, and if the reader doesn’t have a Linux computer, which happens to be running ‘KDE 4′ or ‘Plasma 5′ as its desktop manager, then there is no point to read any further, because nothing in the KDE Store could ever be of any interest otherwise.

But this one widget has a surprisingly simple premise: The user can define ‘Places’, a set of URIs or locations, within the Dolphin File Browser, that have always existed unobtrusively within Plasma 5. They include a collection of local and remote Folders, a collection of ‘timeline:/’ URIs which must first be Added to Places, a collection of Searches (…), and the current set of Connected Devices:

Screenshot_20190326_184606

Then, once the user has done that, the widget in question can be added to a Plasma 5.6 (+) Panel, so that when clicked on once, a fly-out opens, that gives access to all the defined ‘Places’ with one more click:

Screenshot_20190326_184726

Why does this interest me? Because, while the developer first intended for this widget to be added to the Panel of a regular Plasma 5 desktop, it can be added just as easily to the ‘Latte-Dock’, that I wrote about in the earlier posting linked to above. In fact, I’ve made it part of my recent test-project, to set up an OS/X -like desktop on the computer I name ‘Phosphene’…

Continue reading Yet another 3rd-Party, as in KDE-Store, Widget, that can help us Personalize the Linux Experience.