How can I connect dynamically created actions - c++

Following the answer In Qt 4.7, how can a pop-up menu be added to a QToolbar button? I can create menus on the fly, but how can I connect these menus in a way that I recognize which menu was used?
If I create these menus in a loop, I have to connect all of them to same slot
For example when I have a button and I create menus for it:
button
-menu 1
-menu 2
Using code similar to this
int r=0;
while (r<2)
{
QAction *action = new QAction("menu " + QString::number(r), this);
Menus->addAction(action);
// here I could use connect in order to connect this menu to a certain slot
// but that would make all of them trigger the same function
r++;
}
ui->Button->setMenu(Menus);
How do I recognize if menu I activated was menu 1 or menu 2? Is it possible to create a slot which has pointer to sender object so that I can for example, read the text?

Related

How to press a default button when by pressing an lineEdit in a WIDGET in QT

I am using QT 4.8 and C++ and I need a default push button which will be pressed when some text are input in a lineEdit and ENTER key is pressed.
According to the QT document for the property "default" of pushbutton.
the behavior is only for dialogs.
"The default button behavior is provided only in dialogs. Buttons can always be clicked from the keyboard by pressing Spacebar when the button has focus."
How can I have this behavior for a widget, which is a container for all my GUI components.
Thanks.
You can connect the signal QLineEdit::returnPressed() to the slot QAbstractButton::click().
Method 1
from the Designer you simply can:
Right-click on lineEdit
Select "Go to slot..."
Choose returnPressed():
Press ok
In the code view you will see the slot connected to the chosen signal
You can simply call the click() slot of a button you need:
Method 2
You can manually connect signal of any lineEdit to a slot of any pushButton:
In Qt 4 syntax:
connect(ui->lineEdit, SIGNAL(returnPressed()),
ui->anyButton, SLOT(click()));
In new Qt 5 syntax:
connect(ui->lineEdit, &QLineEdit::returnPressed,
ui->anyButton, &QPushButton::click);

How to set signal to a button of the GtkColorChooserDialog composite widget?

I am using Glade to create GUI. I have the default GtkColorChooserDialog.
How to set signals to the Select and Cancel button clicked event.
These two buttons are grouped in one GtkButtonBox and I can only choose the box, but not the buttons themselves.
If I can't select the button then how to assign action to the clicked signal?

Detect QMenu caller when using same SLOT

I have a QMenu and actions added to it. Works fine. Assume it does have menu called "Paste here" in it.
So now I have 3 buttons, I assign this same menu to three buttons:
btnNum1->setMenu(MyMenu);
btnNum2->setMenu(MyMenu);
btnNum3->setMenu(MyMenu);
Now in the triggered slot, when I do:
qDebug() << QObject::sender();
I get the QAction which is the submenu item, I want to know which button invoked the menu which then user clicked on a menu item and triggered the slot. I need to know is it menu loaded in btnNum1 or btnNum2 or btnNum3.
Is it even possible?

Pop up menu event control in Qt

When I add a pop up menu in Qt as follows:
QMenu menu(widget);
menu.addAction("AAA");
menu.exec(eventPress->globalPos());
How do I control "AAA" action events. e.g. do something when "AAA" is clicked.
You can overloaded addAction.
From Qt assistant
This convenience function creates a new action with the text text and
an optional shortcut shortcut. The action's triggered() signal is
connected to the receiver's member slot. The function adds the newly
created action to the menu's list of actions and returns it.
MyClass::Popup()
{
QMenu menu(widget);
menu.addAction("AAA", this, SLOT(burnCase()));
menu.exec(eventPress->globalPos());
}
// This is your slot
MyClass::burnCase()
{
}

Creating popup menu in Qt for QTableView

I have a QTableView in the main UI of my program. I'd like to show popup menu when user right clicks on the cells of the table and take appropriate action when an option is selected from the menu.
I am using Qt Creator 1 (Qt version 4.5). How can I do that?
Check out the customContextMenuRequested signal to get the event, and use a QMenu for the menu itself. Use QTableView::indexAt to find out what, if any, cell was clicked based on the coordinates given to the signal and take the appropriate action when a menu item is clicked.