wxWidgets - modeless wxDialog - c++

I am creating my own subclass of wxDialog and it works well when used as a modeless dialog like so:
AddDialog newAddDialog = new AddDialog(this, wxID_ANY, _T("Dialog Title"), wxDefaultPosition, wxDefaultSize, 0);
if (newAddDialog.ShowModal() == wxID_OK)
{
//do something
}
When using ShowModal(), the flow of the program stops until OK or Cancel is pressed. I need the dialog to show up, but not stop the flow of the GUI, so I tried this:
AddDialog newAddDialog = new AddDialog(this, wxID_ANY, _T("Dialog Title"), wxDefaultPosition, wxDefaultSize, 0);
if (newAddDialog.Showl() == wxID_OK)
{
//do something
}
When using Show(), the dialog briefly shows up and then disappears. I thought it might be a scope issue, so I used a pointer for newAddDialog. Then, the dialog shows up, when when I click OK or Cancel, the dialog closes, but the if statement code does not execute even if OK is clicked.
Does anyone know how to proceed? Thanks.
Further clarification:
I have virtual void function in my Dialog subclass that I overide in another class. I can't seem to get the event working when I overide, however, if I have a void in the actual Dialog subclass, I get the event call. This seems to be an overide problem, but I don't know what the problem would be. This is not the main GUI that I'm calling in the OnInit() call - could that be a problem?

ShowModal blocks execution of your program and returns the outcome (like wxID_OK). On the other hand, Show just shows the dialog and returns immediately, so you can't check the outcome (what the user pressed for buttons) from it's return value. Instead you have to communicate the outcome of that dialog-box by sending an event from withing the dialog or something like that.

AddDialog newAddDialog = new AddDialog(...
Does this compile? Really?
The 'new' operator returns a pointer to void, so the code you have posted looks very odd indeed.
The usual way of doing this is:
AddDialog * newAddDialog = (AddDialog *) new AddDialog( ...
Or
AddDialog newAddDialog( ...

Related

Qt - How to handle memory management for dialogs?

I am running into the following issue:
Users presses "Ctrl+N" which goes into function MainWindow::newAction()
In MainWindow::newAction(), create a QDialog dlg(centralWidget()) and call dlg.exec()
While dlg is open, users pressed "Ctrl+N" again
The result is that dlg never gets deleted (it will only get deleted once centralWidget() gets deleted).
The call stack is something like this:
MainWindow::newAction ()
...
MainWindow::newAction()
I am wondering how to handle this case. I want all of the local dialog variables from the first call to newAction() to be deleted by the time we go into the function newAction() again.
You also can try something like this:
void MainWindow::newAction() {
const auto dialog = new MyDialog(centralWidget());
// When user will close the dialog it will clear itself from memory
connect(dialog, &QDialog::finished, [dialog]() {
dialog->deleteLater();
});
dialog->exec();
}
However, a good move would be to stop user from summoning more QDialogs than a single one, given that this one is a modal dialog(might be a good idea to keep this dialog pointer as a class member and check is it on screen already before calling exec() on it.
If i understood the question right, you want one dialog to be opened and want to delete it before a new dialog request comes in?
If that's the case you can do following:
In MainWindow.h declare QDialog *dlg = nullptr
In your MainWindow.cpp newAction() function you can do following:
void newAction()
{
if(dlg != nullptr)
{
dlg->close();
dlg->deleteLater();
//or
//dlg->destroy(); // this will immediately free memory
}
dlg = new QDialog(centralWidget());
...
//dlg->exec(); // This will automatically make QDialog modal.
dlg->show(); // This will not make a QDialog modal.
}
I hope this will help. Remember QDialogs when displayed with exec() they automatically behave as Modal window. show() will make it non-modal.

Qt: button - going back from "help.cpp" to "mainwindow.cpp"

I'm new at Qt. I've created small application and I created second page help.cpp. On MainWindow.cpp I have a button, that switches to help.cpp page.
Function which switches to "help" page:
void MainWindow::on_box1button_clicked()
{
helpwindow = new help(this);
helpwindow->show();
}
This code works properly.
On the "help" page I've got a QButton, which will switch back to mainwindow.cpp. How Can I code that button to actually make this action?
If your intention by "switching" is hiding one window and showing another one, so you can simply pass a reference of the main window to your help window and there when you want to switch back, you can hide/close itself and show the main window.
MainWindow (this code is fine)
helpwindow = new help(this);
helpwindow->show();
HelpWindow
When you want to switch back to the main window, you can do this:
// Hide the HelpWindow itself
// or this->close()
this->hide()
// Show the MainWindow (i.e. the parent window)
QWidget *parent = this->parentWidget();
parent->show();
Since you are creating a new help(this); on mainwindow it is better to close the help window
Use
this->close();

How to catch information for Qt designer

I have created a Qdialog box using the Qt creator designer as shown below:
When I need to display it, I'm instantiate the class dialogoverwrite (.cpp, .h and .ui)
DialogOverwrite *OverwriteDialog = new DialogOverwrite;
OverwriteDialog->exec();
OverwriteOption = OverwriteDialog->result()
My issue is that I want to get the QDialogButtonBox result but I do not know how. the current code, returning the result of the OverwriteDialog but it's not returning any QDialogButtonBox::Yes, QDialogButtonBox::YesToAll ...
How to catch the QButtonGroup result and not the QDialog result.
In the same way, If I want to change the label value from "File(s) and/or Folder(s)" to another label, how to access to this QLabel ?
Thanks for your help
When you pressed QDialogButton it was emit signal clicked(QAbstractButton*) by catching this signal you can identify which action button pressed.
Please go through following link it would be help you.
Qt: How to implement QDialogButtonBox with QSignalMapper for non-standard button ??
Well the standard way to do this is to handle the result by connecting it. So you could do:
connect(this, SIGNAL(clickedDialogButton(QAbstractButton*)),
SLOT(dialogButton(QAbstractButton* aButton)));
Next you would create a function in your class called dialogButton (for example) and have that handle the result:
void MyUI::dialogButton(QAbstractButton* aButton) {
// Obtain the standard button
StandardButton button = buttonBox−>standardButton(button);
// Switch on the type of button
switch (button) {
case QDialogButtonBox::YesToAll:
// Do the thing you would like to do here
break;
// add some more cases?
}
}
You could also check for the signal given by the QButtonGroup. Something like: void QGroupButton::buttonClicked(QAbstractButton* button) would work in the same way.

How to disable minimizing by taskbar icon click

I've stumbled across very strange behaviour during work on my program.
I've written custom changeEvent class, which allows me to hide program to SysTray on minimizing.
But when i double click on taskbar app icon, the function goes crazy. It creates 2 to 4 systray icons and on requesting window show again, it just shows main window borders without any content inside.
Here's my changeEvent code:
void MainWindow::changeEvent(QEvent *e) {
QMainWindow::changeEvent(e);
if(e->type()==QEvent::WindowStateChange)
if(isMinimized()) {
trayIcon=new QSystemTrayIcon(QIcon(":/icon/itime.ico"));
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(on_show(QSystemTrayIcon::ActivationReason)));
QAction *showAction=new QAction("Pokaż",trayIcon);
connect(showAction,SIGNAL(triggered()),this,SLOT(on_show()));
QMenu *trayIconMenu=new QMenu;
trayIconMenu->addAction(showAction);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->show();
this->hide();
}
}
on_show(QSystemTrayIcon::ActivatioReason) SLOT:
void MainWindow::on_show(QSystemTrayIcon::ActivationReason reason) {
if(reason) {
if(reason!=QSystemTrayIcon::DoubleClick)
return;
}
if(this->isMinimized()) {
this->raise();
this->showNormal();
this->setWindowState(Qt::WindowActive);
trayIcon->hide();
}
}
on_show() SLOT is just the same besides that first if.
Soo, I would like to know whether there is any way to disable minimizing of window by taskbar icon click.
If there's none, then maybe you have any ideas what can go wrong in here when doubleclicking on icon in taskbar?
Thanks for help!
I've managed to work around that problem by overloading closeEvent function and leaving alone changeEvent function.
So, I'm using boolean flag to distinct between closing of program by menu item and by clicking "X" button and the rest stays just the same, as posted in my earlier post with one change.
I've moved this whole block of code to window constructor in order to prevent multiple creation of trayIcon, as pointed out by Nicolas.
trayIcon=new QSystemTrayIcon(QIcon(":/icon/itime.ico"));
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(on_show(QSystemTrayIcon::ActivationReason)));
QAction *showAction=new QAction("Pokaż",trayIcon);
connect(showAction,SIGNAL(triggered()),this,SLOT(on_show()));
QMenu *trayIconMenu=new QMenu;
trayIconMenu->addAction(showAction);
trayIcon->setContextMenu(trayIconMenu);
Thanks for your help!

wxWidgets 2.9 AutoComplete in wxComboBox custom EnterEvent

I have an issue concerning wxWidgets 2.9 and the wxComboBox AutoComplete feature. Below is my event table which takes the ENTER event of my ComboBox, on enter I fire OnComboEnter. If I do that I'm not able to select an item from the AutoComplete list since it directly executes the OnComboEnter method on the text the user typed into the ComboBox.
BEGIN_EVENT_TABLE(LVFilterPanel, wxPanel)
EVT_TEXT_ENTER(wxID_ANY, LVFilterPanel::OnComboEnter)
EVT_CONTEXT_MENU(LVFilterPanel::OnComboContextMenu)
END_EVENT_TABLE()
My ComboBox is declared like this:
mFilterString = new wxComboBox(this, LV_FILTER_STRING, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, 0, wxTE_PROCESS_ENTER);
AutoComplet is done using the default AutoComplete method found in wxWidgets 2.9:
mFilterString->AutoComplete(historyarr);
historyarr is a wxArrayString filled with the strings which were previously typed in by the user.
The OnComboEnter method looks like this:
void LVFilterPanel::OnComboEnter(wxCommandEvent& event) {
wxCommandEvent ce(wxEVT_COMMAND_BUTTON_CLICKED, LV_FILTER);
static_cast<wxButton*>(FindWindow(LV_FILTER))->Command(ce);
}
My question now is, how could I change the event handling in a way that it is able to select the item first and then processes OnComboEnter, so the user is able to select an item first (or may not select an item at all and directly hit enter to launch the event and the OnComboEnter method).
Thanks in advance.
Greets,
Roin
If you need to execute your event handler after the standard handling takes place, the usual trick is to do nothing in your event handler (which means also calling event.Skip(), of course!) except setting some internal flag and check this flag in EVT_IDLE handler. If it is set, then do whatever you need (e.g. button->Command() in your case) and reset the flag.
This approach ensures that the handler is ran "soon after" the event happens without interfering with normal event processing.
I had same problem but wxTextCtrl, this is my solution:
TextCtrl2 = new wxTextCtrl(this, ID_TEXTCTRL2, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));
TextCtrl2->SetHint("Search...");
TextCtrl2->AutoComplete(m_AutoCompleteChoices);
TextCtrl2->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(StartFrame::OnKeyDown),NULL, this);
void StartFrame::OnKeyDown(wxKeyEvent& event)
{
switch (event.GetKeyCode()) {
case WXK_RETURN:
QueryCache(TextCtrl2->GetValue()); // <- This is anything to do!
break;
}
event.Skip();
}
I could use wxSearchCtrl but Autocomplete not working in that control and I don't know why.