Detect QMenu caller when using same SLOT - c++

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?

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?

How can I connect dynamically created actions

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?

QRadioButton not Resetting to unchecked

I have an QDialog class say 'OptionsClass' to display a dialog for options for my Application.
I have designed it in Qt Designer & the object of that class is created in the constructor of my QMainWindow inherited class by new & it is deleted in the destructor (I think this helps in quickly loading the Dialog when button is clicked). Whenever the options button is clicked I am calling a function in OptionsClass which basically edits some text in QLabel & after that calling show(). There are 3 QRadioButton's also in the QDialog class.
When I open the dialog for the 1st time in my application's startup the radio button's are unchecked. Now say i check any button & close the dialog. Now if I again open the Dialog then still that radio button is checked. I want that everytime I open the Dialog all the radio button's should be unchecked.
Here's the SLOT for the button which is clicked to open the Dialog:
void MyMainWindow::on_actionCut_triggered()
{
optionsObj->init(n, 'x');
optioobjn->show();
}
Here is a snippet of the function init:
void OptionsClass::init(int n, char c)
{
//some settings to edit the QLabel
ui->radio1->setChecked(false);
ui->radio2->setChecked(false);
ui->radio3->setChecked(false);
}
I have tried with other properties like SetDown(), SetChecked(), etc but still it doesnt work. What am I doing wrong?
In order to prevent your button from resetting, you need to do
radio-> setAutoExclusive(false);
Then you can uncheck. Don't forget to turn autoExclusive on again.
AutoExclusive is normally off for other abstract buttons, but on for Radio buttons.

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.