QDialogButtonBox button Ok not woking after connecting - signals-slots

I have a mainWindow class that calls a function mainWIndow::ShowDialogBox() when double clicked on the QTabBar. The dialog box shows up, but it isn't connecting the buttons. I have the connect calls in ShowDialogBox. It gives me a red underline on connect saying
no instance of overloaded function "MainWindow::connect" matches the argument list"
This is my code
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if (object == mTabWidget->getTabBar() && event->type() == QEvent::MouseButtonDblClick)
{
qDebug()<<"dblclk";
ShowDialogBox();
}
return QObject::eventFilter(object, event);
}
//Show dialog box when double clicked on QTabBar
void MainWindow::ShowDialogBox(){
QDialog dialog;
QVBoxLayout layout(&dialog);
QLineEdit editLine;
layout.addWidget(&editLine);
QDialogButtonBox *dialogButton = new QDialogButtonBox(QDialogButtonBox::Ok );
connect(dialogButton, SIGNAL(accepted()), dialog, SLOT(accept())); //this 'connect' is underlined
layout.addWidget(dialogButton);
dialog.setLayout(&layout);
if(dialog.exec() == QDialog::Accepted)
{
mTabWidget->setTabText(0, editLine.text());
}
}
I have added the signals and slot in mainWindow.h as
private slots:
void accept();
signals:
void accepted();
I have spend hours on this but no luck. I am new to Qt.

Line:
connect(dialogButton, SIGNAL(accepted()), dialog, SLOT(accept()));
should be:
connect(dialogButton, SIGNAL(accepted()), &dialog, SLOT(accept()));
Because the third parameter has to be a memory address(pointer).

Related

Soft keyboard not closing when dialog is cancelled

In my app i have a dialog where the user should enter some text in an edittext. But when I tap outside of the dialog to close the dialog, the dialog closes but the soft keyboard which popped up because i clicked on the edittext stays. It's very weird: when I set windowsoftinputmode to stateAlwaysHidden,the keyboard gets a bit transparent but it doesn't close. I only have this problem in portrait, In landscape it doesn't happen but that could be because the softkeyboard fills the whole screen. I also cant click on the keys of the keyboard it doesn't react. I already tried to set the windowsoftinputmode to different values and I set a oncancellistener on my dialo g which should close the softkeyboard but it doesn't. It's seems to me as a bug.
The code of my dialog
public void create(View view) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_SWIPE_TO_DISMISS);
dialog.setContentView(R.layout.dialoglayout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
dialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
#Override
public void onCancel(DialogInterface dialog) {
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
View view = getCurrentFocus();
if (view == null) {
view = new View(getBaseContext());
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
});
editText = dialog.findViewById(R.id.levelname);
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
name = editText.getText().toString();
callables.totaloverwriteFile(name,getApplicationContext(),"newlevelname");
getApplicationContext().startActivity(new Intent(getApplicationContext(), CreatorActivity.class));
return true;
}
return true;
}
});
}```
Try building InputMethodManager from context, something like this (kotlin)
val inputMethodManager = context?.getSystemService<InputMethodManager>()
inputMethodManager?.hideSoftInputFromWindow(view.windowToken,0)

How to 'properly close MFC Modeless Dialogs and fixing resource leak'

I've created an MFC Application, with a Base Dialog (derived from CDialog class), a Setting Dialog (derived from CBaseDlg and an App Dialog (also derived from CBaseDlg). Then I created a class called CScrMng (aka Screen Manager) that hold the ShowDialog() function (to Create and Show these dialogs).
The basic idea is ScrMng will manage all of my Modeless Dialogs, and anytime I want to open a dialog, I just need to CScrMng::ShowDialog() in the BaseDlg.cpp, and the dialog will display.
This approach has caused resources to leak here and there. I've done a bit of research about overriding the PostNcDestroy(), but I don't have a clear idea of where to call it.
What function should I use to properly close these modeless dialogs?
I want to open the Setting Dialog from Base Dialog, then when I click on the Cancel button, it should return me to the Base Dialog screen so that I can open another Dialog.
Right now I'm using EndDialog(). I know it's wrong, but calling DestroyWindow() will immediately exit the program, which is not what I want.
Source code
MFCApplication.cpp
#include...
BEGIN_MESSAGE_MAP(CMFCApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
CMFCApp::CMFCApp()
{
// support Restart Manager
m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
}
CMFCApp theApp;
BOOL CMFCApp::InitInstance()
{
...
CWinApp::InitInstance();
CShellManager *pShellManager = new CShellManager;
CScrMng::GetInstance()->ShowDialog(IDD_MAINDLG);
SetRegistryKey(_T("Local Applications"));
if (pShellManager != NULL)
{
delete pShellManager;
}
return TRUE;
}
CScrMng.cpp
#include...
CScrMng* CScrMng::m_pInstance = NULL;
CScrMng* CScrMng::GetInstance(){
if (m_pInstance == NULL)
m_pInstance = new CScrMng();
return m_pInstance;
}
CScrMng::CScrMng(){}
void CScrMng::ShowDialog(int ID)
{
CMainDlg* m_pDlg = NULL;
switch (ID)
{
case IDD_MAINDLG:
m_pDlg = new CMainDlg();
theApp.m_pMainWnd = m_pDlg;
m_pDlg->Create(IDD_MAINDLG);
m_pDlg->ShowWindow(SW_SHOW);
m_pDlg->UpdateWindow();
break;
case ...
break;
case IDD_SETTINGDLG:
m_pDlg = new CSettingDlg();
m_pDlg->Create(ID,NULL);
m_pDlg->ShowWindow(SW_SHOW);
m_pDlg->UpdateWindow();
break;
}
CMainDlg.cpp
#include...
CMainDlg::CMainDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMainDlg::IDD, pParent) {m_hIcon = AfxGetApp()-> LoadIcon(IDR_MAINFRAME);}
void CMainDlg::DoDataExchange(CDataExchange* pDX) {...}
void CMainDlg::PostNcDestroy() //Added these
{
CDialog::PostNcDestroy();
delete this;
}
BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
...
END_MESSAGE_MAP()
BOOL CMainDlg::OnInitDialog() {
CDialog::OnInitDialog();
SetIcon(m_hIcon, FALSE);
return TRUE;
}
void CMainDlg::OnPaint() {...}
void CMainDlg::OnBnClickedOpenAppdlg()
{
CScrMng::GetInstance()->ShowDialog(IDD_APPDLG);
}
void CMainDlg::OnBnClickedOpenSettingdlg()
{
CScrMng::GetInstance()->ShowDialog(IDD_SETTINGDLG);
}
void CMainDlg::OnBnClickedExit()
{
DestroyWindow(); //replaced CDialog::OnCancel() with this.
}
Update: After changing the code in the SettingDlg.cpp, i encountered a Debug Assertion Failed! problem :
void CWnd::MoveWindow(int x, int y, int nWidth, int nHeight, BOOL bRepaint)
{
ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL)); //Breakpoint triggered
if (m_pCtrlSite == NULL)
::MoveWindow(m_hWnd, x, y, nWidth, nHeight, bRepaint);
else
m_pCtrlSite->MoveWindow(x, y, nWidth, nHeight);
}
Here are what i changed in the .cpp file:
SettingDlg.cpp
void CSettingDlg::PostNcDestroy()
{
CMainDlg::PostNcDestroy();
}
void CSettingDlg::OnBnClickedSettingcancel()
{
DestroyWindow(); //Using destroyWindow rather than EndDialog();
}
Close & Delete a modeless Dialogs.
A proper Way is: Override PostNcDestroy, OnOk() and OnCancel() for the Modeless Dialogs
void CBaseDlg::PostNcDestroy()
{
CDialog::PostNcDestroy();
delete this;
}
.
void CBaseDlg::OnOk()
{
if(!UpdateData(TRUE))
return;
DestroyWindow();
}
.
void CBaseDlg::OnCancel()
{
DestroyWindow();
}

Android - Back button behavior

I have a project with 2 activities, the first one is the "SplashActivity" - where I load some network data - the second one, the MainActivity.
Inside of my MainActivity I have a fragment and inside of this fragment a webview. My first point is, when the user clicks on back button, the SplashScreen is open again.
The back button should behave like:
When the user doesn't navigate inside of my webview, close the app.
When the user navigates in webview, use the back history of the browswer.
I read about back stack here: http://developer.android.com/training/implementing-navigation/temporal.html#back-webviews
I didn't understand at all how it should work, because I have all cases "mixed". Anyone knows what should I do to fix this problem?
Any idea or sample code will be appreciate!
Define Webview wb as a global variable. Then try this;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(wb.canGoBack() == true){
wb.goBack();
}else{
new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Application will be closed")
.setMessage("Close app?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
System.exit(0);
}
}).setNegativeButton("No", null).show();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}

Crashing while executing GetParent(). Closing of a modeless dialog box

I am creating a modeless dialog box. The dialog box is called from the menu item of main frame window.
MainFrm.h
CModeless* modeless;
bool modelessDlgOpen;
MainFrm.cpp
void CMainFrame::OnDatabaseMLdlg()
{
// TODO: Add your command handler code here
if (modelessDlgOpen == TRUE)
return;
modelessDlgOpen = TRUE;
modeless = new CModeless(this);
//modeless->Create(IDD_MLDLG, GetDesktopWindow());
modeless->Create(IDD_MLDLG, this);
mbPoll->ShowWindow(SW_SHOW);
}
When menu item is clicked, OnDatabaseMLdlg() function is called and a modeless dialog box with resource ID IDD_MLDLG appears.
The issue is while closing the modeless dialog box.
I am not able to find out the correct method to have a clean closure / destroy of this modeless dialog box. Upon clicking the cross button in right-top corner, which message gets
generated?
My current code which I have tried is as follows. (producing code related only to the closure of the dialog box)
MLDLG.h
#pragma once
#define WM_MLDLG_CLOSED (WM_USER + 555)
// CModeless dialog
class CModeless : public CDialog
{
DECLARE_DYNAMIC(CModeless)
public:
CModeless(CWnd* pParent = NULL); // standard constructor
virtual ~CModeless();
// Dialog Data
enum { IDD = IDD_MLDLG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
virtual BOOL Create(UINT nIDTemplate, CWnd* pParentWnd = NULL);
afx_msg void OnNcDestroy();
virtual void PostNcDestroy();
CWnd* mParent;
afx_msg void OnBnClickedCancel();
};
MLDLG.cpp
void CModeless::OnNcDestroy()
{
CDialog::OnNcDestroy();
// TODO: Add your message handler code here
}
void CModeless::PostNcDestroy()
{
CDialog::PostNcDestroy();
GetParent()->PostMessage(WM_MLDLG_CLOSED,0,0); // **CRASHES HERE**
delete this;
}
void CModeless::OnBnClickedCancel()
{
// TODO: Add your control notification handler code here
//CDialog::OnCancel();
DestroyWindow();
}
Not able to understand what am I doing wrong or what am I missing?
I can provide additional details in case required.
Thanks in advance.
EDIT-20130612: Additional information:
My constructor is as follows:
CModeless::CModeless(CWnd* pParent /*=NULL*/)
: CDialog(CModeless::IDD, pParent)
{
mParent = pParent;
if (mParent == NULL)
{
MessageBox(L"mParent is NULL");
}
else
{
MessageBox(L"mParent is not NULL");
}
}
Here, I have verified that mParent is not NULL.
PostNCDestroy() is called VERY late and most of the useful state of the MFC window is not valid at that point. GetParent() is probably returning NULL, which will cause a crash the way you are using it.
Try moving the PostMessage call to OnDestroy() before calling the base class implementation there.
Another option is to cache the parent's hWnd and call ::PostMessage() on that hWnd;

In J2ME, is that possible to operate an Alert dialog box with Yes & NO command?

I have created an Alert dialog box in my J2ME app to alert user when user press exit button to terminate an app and ask user confirmation to exit from app with yes and no command.
When user press Yes button app will terminate and when user press No button app will return to its Main form. To do this I developed a code from scratch which are as follows:
public class CustomAlert extends MIDlet implements CommandListener
{
Alert ExitAlrt;
Display d;
Command MainListSelect, Exit, YesCmdAlrt, NoCmdAlrt;
List MainList;
public CustomAlert()
{
d = Display.getDisplay(this);
//Initialization of commands
MainListSelect = new Command("Select", Command.SCREEN, 1);
Exit = new Command("Exit", Command.STOP, 2);
//Initialization of lists
MainList = new List("Menu", List.IMPLICIT);
//Adding command to lists
MainList.addCommand(MainListSelect);
MainList.addCommand(Exit);
MainList.setCommandListener(this);
//Appending the content of lists
MainList.append("Settings",null);
}
protected void startApp()
{
MainList.setSelectedIndex(0, true);
d.setCurrent(MainList);
}
protected void pauseApp() { }
protected void destroyApp(boolean unconditional){}
//This method handle commands which operate list that is Select & Exit
public void commandAction(Command cmd,Displayable dispable)
{
if(cmd == MainListSelect)
{
int slctindx = MainList.getSelectedIndex();
if(slctindx == 0)
{}
else if(slctindx == 1)
{}
}
if(cmd == Exit)
{
ExitAlrt = new Alert("Application Alert","Are you sure you want to exit?",null, AlertType.WARNING);
YesCmdAlrt = new Command("Yes", Command.EXIT,1);
ExitAlrt.addCommand(YesCmdAlrt);
NoCmdAlrt = new Command("No", Command.SCREEN,2);
ExitAlrt.addCommand(NoCmdAlrt);
d.setCurrent(ExitAlrt);
}
}
//This Code handle Commands present on Alert dialog box.
public void commandAction(Command cmd) /
{
ExitAlrt.setCommandListener(this);
if(cmd == NoCmdAlrt)
{
d.setCurrent(MainList);
}
else if(cmd == YesCmdAlrt)
{
destroyApp(true);
notifyDestroyed();
}
}
}
In above code problem is when I click on Exit button, Alert box appears and when I press Yes button to terminate an app it again redirect to me on Main List of an app. I did lot of placements in code but problem remain constant.
What is solution for this?
ExitAlert in the posted code snippet lacks a command listener because you didn't invoke setcommandListener for it. As a result, instead of expected exit, default command action happens which is to simply dismiss the alert, as explained in API javadocs:
If the user invokes a Command and the default listener is present, the default listener ignores the Command and implements the automatic-advance behavior.
Note you might think that ExitAlrt.setCommandListener(this) inside commandAction(Command cmd) method does the trick for you but this is not so, because this method is not invoked in between creation of the ExitAlrt instance and displaying it.
To get the desired behavior, implement and set an appropriate command listener for ExitAlrt prior to invoking setCurrent.
// ...
if(cmd == Exit)
{
System.out.println("Exit command invoked"); // log message for debugging
Alert ExitAlrt = new Alert("Application Alert",
"Are you sure you want to exit?", null, AlertType.WARNING);
ExitAlrt.addCommand(new Command("Yes", Command.EXIT, 1));
ExitAlrt.addCommand(new Command("No", Command.SCREEN, 2));
// --> set command listener for ExitAlrt prior to invoking setCurrent
ExitAlrt.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
System.out.println("command: [" + c.getCommandLabel()
+ "] at screen: [" + d.getTitle() + "]"); // for debugging
if (c.getCommandType() != Command.EXIT) {
System.out.println("Exit cancelled"); // for debugging
d.setCurrent(MainList);
return;
}
System.out.println("Exit confirmed"); // for debugging
destroyApp(true);
notifyDestroyed();
}
});
d.setCurrent(ExitAlrt);
}
// ...
For simplicity, above code snippet uses System.out.println for logging. If needed, refer to another SO question for an explanation and example of how this could be done in a more practical way.

Resources