QRadioButton not Resetting to unchecked - c++

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.

Related

C++ MFC Button on a second Dialog does nothing

I created a C++ MFC Program with the Visual Studio Wizard. There I set the application type to "Dialog Based".
I have a button on the first dialog, which opens another dialog. I created this second dialog by right clicking on the project -> Add -> Resource -> Dialog -> New.
Then I added a MFC class to the new dialog by double clicking it in resource view.
On the second Dialog I also created a button. I double clicked the button and added some code which should be executed.
When I run the program and click the button on the second dialog, nothing happens and the code is not executed. I did everything like with the button on the first dialog. That one works fine. Why is the second button not working and what do I need to do?
First Dialog
void CMFCApplication1Dlg::OnBnClickedButton1()
{
CDialogEx secondDialog(IDD_DIALOG1);
secondDialog.DoModal();
}
Second Dialog
void SettingsDlg::OnBnClickedButton1()
{
MessageBox(L"Button clicked", L"Button clicked", MB_OK);
}
#andrew-truckle, your side node was the answer! I changed it to:
void CMFCApplication1Dlg::OnBnClickedButton1()
{
SettingsDlg settingsDialog;
settingsDialog.DoModal();
}
Now the button works just as expected. Thank you very much!
Further Info (from #andrew-truckle)
For the benefit of others the issue here was that the original code declared the dialog like this:
CDialogEx secondDialog(IDD_DIALOG1);
That was wrong because the dialog was actually associated with the class SettingsDlg. This is the class that had the message map and event handlers etc. CDialogEx was the base class.
I added this update to the answer to save the reader from locating my comment to the question.

Is there a way to stop a qmenu from being hidden when you click off of the menu

I have a QMenu. I would like to keep the menu up when you click outside menu. Is this possible. I have tried overriding the eventmethod but that does not work. I know how to keep the menu open when you click on an action (there are a few questions here about that) but I want to keep it up when you click outside the menu, clicking on an action should still close the menu. This should work when bringing up the menu via popup or exec. Is this possible using QMenu?
EDIT: showTearOffMenu is not what I want, I would like to keep these pop-up frameless
EDIT #2: Overriding closeEvent and ignoring it will keep the menu up but now it steals all clicks
void MenuSubclass::closeEvent(QCloseEvent* e){
e->ignore();
}

QT Modal Window and Disabling parents toolbar

I am developing a project using Qt4 and I have run into a slight issue.
I am creating a modal window from the main window. I want this to disable the toolbar at the top.
I have a menu item that spawns the modal from the menu. What I want is that when the modal is spawned that menus is disabled. I have tried using the setEnabled(false) function but that doesn't reset it.
Here is the code:
void Main_Screen::Create_ViPro()
{
std::auto_ptr<ViPro_Dialog> modal(new ViPro_Dialog(this));
modal->show();
modal->exec();
}
So It is just a simple class that is triggered when a menu item is selected. I feel that the problem stems from the fact that i'm setting the parent to the main screen, however I don't know how to create a modal without a parent(it doesn't make sense to do that). Is there a way to disable the parents toolbar from the child? The only thing I have seen so far is _fileMenu->setEnabled(false);
That works as long as I don't create the modal, but as soon as that is spawned, the menu works again. I'm totally lost.
Thanks in advance
Edit as Patrice
Here is the constructor
Main_Screen::Main_Screen(QWidget* parent /*= NULL*/)
: QMainWindow(parent),
_newProj(new QAction(tr("New &ViPro"), this)),
_fileMenu(menuBar()->addMenu(tr("&File")))
{
//create slot for creating a new project
connect(_newProj.get(), SIGNAL(triggered()), this, SLOT(Create_ViPro()));
_fileMenu->addAction(_newProj.get());
//if i do this then setEnabled(false) works perfectly and i can't access the menu
Create_ViPro()
}
So the function is signaled by triggering the newproject action. If i call the function directly from within the constructor it disables it as you stated patrice, however, if I have the function called via the trigger, it doesn't disable it. Am I handling the signal / slot mechanism wrong? Thanks again.
Another example, if I make the function create_vipro() as below
void Main_Screen::Create_ViPro()
{
_fileMenu->setEnabled(false);
}
The file menu isn't disabled when I trigger the event, so it must have nothing to do with the modal itself, but instead how signals are handled.
Since child is a modal dialog main screen can't react to event. But you can disable the toolbar (or menubar) before creating the modal dialog and enable it as soon as you leave the exec function:
void Main_Screen::Create_ViPro()
{
_fileMenu->setEnabled(false);
std::auto_ptr<ViPro_Dialog> modal(new ViPro_Dialog(this));
modal->show();
modal->exec(); // Will stay here until you close the modal dialog
_fileMenu->setEnabled(true);
}
if ViPro_Dialog is really a modal dialog it will works.
Another thing, since ViPro_Dialog is modal you can declare it locally without using auto_ptr:
void Main_Screen::Create_ViPro()
{
_fileMenu->setEnabled(false);
ViPro_Dialog modal(this);
modal.show();
modal.exec(); // Will stay here until you close the modal dialog
_fileMenu->setEnabled(true);
}
EDIT:
I guess (I can't test at work) that you can't enable/disable menu when it is executing a QAction. Signal is calling slots sequentially so QAction is busy when you try to disable the menu.
Try this:
In Main Screen, create a slot with one boolean parameter that enable/disable menubar. Just call the setEnabled function
In ViPro_Dialog, emit a signal with a boolean parameter (false at startup, true at validation)
In Create_ViPro, once dialog created, connect the new signal with the slot, exec dialog and don't forget to disconnect slot from signal:
void Main_Screen::Create_ViPro()
{
ViPro_Dialog modal(this);
// Connect signal/slot
modal.show();
modal.exec(); // Will stay here until you close the modal dialog
// Disconnect signal/slot
}
This can achieve what you want
EDIT2
You are doing a mistake when using modal dialog. There's a conflict between show() and exec(). When you are displaying modal dialog you don't need to disable other windows: it's automatically done by modal status of the dialog. There are many modal depth: http://qt-project.org/doc/qt-4.8/qt.html#WindowModality-enum. So your code should be:
void Main_Screen::Create_ViPro()
{
ViPro_Dialog modal(this);
// modal.setWindowModality(Qt::WindowModal); // Uncomment this line if you want to only disable parent
modal.exec();
}
Read this for more detail: http://qt-project.org/doc/qt-4.8/QDialog.html#details.
Using exec() doesn't just create a modal dialog, it also suspends most of the regular event handling, and only handles events in the dialog that is in exec(). This may include some UI updates (like the transitions from enabled to disabled), but I'm not positive. A better way to handle this might be to explicitly set the dialog modal, but allow the regular event loop to continue, something like this:
void Main_Screen::Create_ViPro()
{
ViPro_Dialog* modal = new ViPro_Dialog(this);
modal->setModal(true);
modal->show();
}
That code will still not visually disable the toolbar or menubar, but they should be unresponsive. To disable them, you could try combining this with Patrice Bernassola's answer.

QWidgetAction : how to make the menu disappear after the user completes his input

In my QMenuBar, I have several menus.
One of those menus has a QWidgetAction in it.
It shows up fine, but the problem is that once the user completes his input, I want the menu to disappear (as is the normal behavior for a classical QAction).
However, I am not sure on how to do that. In my QWidgetAction, there is a button the user presses when he is done; I can therefore bind to this button's clicked() signal.
In the slot, I tried to setFocus() an element outside the menu but the menu still doesn't disappear.
How to tell the menu to close itself when my users finish interacting with the QWidgetAction?
Thanks
QMenu inherits QWidget, so calling yourMenu->hide() should do the work.
Hope this helps.

Not able to Show a Dialog Box in its class using SW_SHOW in MFC?

I am trying to create a wizard like structure using dialog boxes...So I replaced the code in CDialog1App as below
CDialog1Dlg* dlg = new CDialog1Dlg;
m_pMainWnd = dlg;
dlg->Create(IDD_DIALOG1);
dlg->ShowWindow(SW_SHOW);
the above worked fine...its displying the dialog box.but I have added another dialog box...
So in the first dialog box if the user clicks Next it has to hide the first dialog box and display the second dialog..
//CDialog1 class
void CDialog1Dlg::OnBnClickedNext()
{
// TODO: Add your control notification handler code here
CDialog2* dialog2 = new CDialog2();
dialog2->Create(IDD_DIALOG2);
dialog2->ShowWindow(SW_SHOW);
this->ShowWindow(SW_HIDE);
}
in the above code am creating an object for the Dialog2 class and trying to show that....
Now the problem is,when I click next its hiding both the windows..What can I do..I tried several types but its still its not workin..Please dont suggest me to do with PropertySheet..It will work with that, i know ...but I want this using Dialog Box for some reason
You're creating the dialog2 with the default parent window (NULL):
dialog2->Create(IDD_DIALOG2);
But the default parent seems to be dialog1 in your case. And since you hide dialog1 which is the parent of dialog2, dialog2 is also hidden.
Find the window (CWnd) of either your main app dialog (if you have one visible apart from your wizard), or use the desktop window as the parent.
For example:
dialog2->Create(IDD_DIALOG2, GetDesktopWindow());