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.

Revisiting HTML, this time, With CSS.

When I first taught myself HTML, it was in the 1990s, and not only has the technology advanced, but the philosophy behind Web-design has also changed. The original philosophy was, that the Web-page should only contain the information, and that each Web-browser should define in what style that information should be displayed. But of course, when Cascading Style-Sheets were invented – which in today’s laconic vocabulary are just referred to as “Styles” – they represented a full reversal of that philosophy, since by nature, they control the very appearance of the page, from the server.

My own knowledge of HTML has been somewhat limited. I’ve bought cuspy books about ‘CSS’ as well as about ‘JQuery’, but have never made the effort to read each book from beginning to end. I mainly focused on what some key concepts are, in HTML5 and CSS.

Well recently I’ve become interested in HTML5 and CSS again, and have found, that to buy the Basic license of a WYSIWYG-editor named “BlueGriffon“, proved informative. I do have access to some open-source HTML editors, but find that even if they come as a WYSIWIG-editor, they mainly tend to produce static pages, very similar to what Web-masters were already creating in the 1990s. In the open-source domain, maybe a better example would be “SeaMonkey“. Beyond that, ‘KompoZer‘ can no longer be made to run on up-to-date 64-bit systems, and while “BlueFish”, a pronouncedly KDE-centric solution available from the package-manager, does offer advanced capabilities, it only does so in the form of an IDE.

(Updated 03/09/2018, 17h10 : )

Continue reading Revisiting HTML, this time, With CSS.