Menu without action
-
Hello,
I'm a newbie in Qt and I've a simple question.When I click on the Menu 'Clear', I want to clear a QTextEdit widget.
In the same application, I have a Menu 'File' which contains several choices as New, Save and Quit.
With these items, I have associated a slot for each action and it works fine.So my question, is to know if it's possible to click on the Menu 'Clear' and the QTextEdit will clear.
Thanks by advance for your help.
Yogis[0_1549345723904_mainwindow.cpp](Uploading 100%)
My code for the MainWindow.cpp file:
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //connect(this->menuWidget()->,this->Clear(),ui->textEdit,Clear); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_actionNew_triggered() { ui->textEdit->append("New option selected"); } void MainWindow::on_actionSave_triggered() { ui->textEdit->append("Save option selected"); } void MainWindow::on_actionExit_triggered() { ui->textEdit->append("Exit option selected"); } void MainWindow::Clear() { ui->textEdit->clear(); }
-
Hi
try
connect(ui->menuTest, &QMenu::triggered, ui->textEdit, &QTextEdit::clear); -
@yogis40 said in Menu without action:
Unfortunately it's not working
What exactly does not work? The connect? ...?
-
Hi,
As written in the triggered signal documentation:
This signal is emitted when an action in this menu is triggered.
-
@yogis40
Sorry, i’ve Forgotten to ask my question.
How do we catch when the user is clicking on ‘Clear’ menu ? Is it possible to specify the ‘Clear’ in the QMenu::triggered(&Clear)
?
Thanks for your help and sorry to ask but I’m a newbie and struggle with Qt. -
It's not exactly a Qt problem. It's what you are trying to do. You're going against the UI guidelines of all desktop environments and against the normal usage of a menu.
The content of the menu bar is menus. Clicking on a menu should not trigger anythings else that showing its content.
Your clear action should be in for example an edit menu. You can put accelerator on it and even a keyboard shortcut.
-
Hi
Just had a test.
connect(ui->menuClear,&QMenu::triggered, ui->textEdit, &QTextEdit::clear);
does not work as a top menu (with no sub items) added in Designer does not call triggered.
(it has no QAction to be triggered)
That is why its not working - as its expected to have sub items.That said, its possible from code, using an QAction instead.
QAction *a = new QAction("Clear", this); ui->menuBar->addAction(a); connect(a, &QAction::triggered, ui->textEdit, &QTextEdit::clear);
That works however as SGist says, its not really a normal way for menus to work seen
from a users perspective. a clear command would normally be in Edit sub menu. -
-
@yogis40 said in Menu without action:
Regarding the UI guidelines, I’m interested to found it. As mentioned previuously, I’m newbie in Qt and the last time I’ve programmed a UI it was in Visual Basic in 2004. Then, 15-years ago...
There's per platform: