QT dialog close and delete - c++

I have a MainWindow and Type class.
A button in the MainWindow sends a signal to a slot with this code:
dialog = new QDialog(this);
Ui_type typeui;
typeui.setupUi(dialog);
dialog->show();
The dialog then shows. When a button is clicked on the dialog, I want to close the dialog and delete it.
I don't understand how to refer to the dialog from the dialog itself.
Any help would be appreciated. Thanks.

You can set Qt::WA_DeleteOnClose attribute on your dialog. This will ensure that the dialog gets deleted whenever it is closed.
Then call close() method in the dialog when your button is clicked.
dialog = new QDialog(this);
Ui_type typeui;
typeui.setupUi(dialog);
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->show();
Refer to the documentation for details :
QWidget::setAttribute ( Qt::WidgetAttribute attribute, bool on = true )
Qt::WidgetAttribute

First the close button is at the dialog window right, then most easy way to do it, is create a button, and connect the close() function to response the click() signal.
like:
Dialog::Dialog(){
// other code
QPushButton *closeButton = new QPushButton(tr("Close"));
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
// other code
}
Under the Qt/examples/dialog projects are good reference for your question. check it out.

The simple way to get input from a modal dialog is QDialog::exec(). This may handle everything you need.

Related

How to close a QDialog that uses a different ui file in Qt?

I've been trying to close a QDialog that uses a separate ui file. The dialog is used in a slot of a different class (TaskManager). Sorry for the question but I couldn't find a workaround anywhere.
I'm creating a ToDo App as a University project (first year, first time using C++ and Qt): as the user clicks on the "Add Task" button in the TaskManager, the dialog is shown. The user inserts the tasks attributes in the dialog and then I take that data to create a Task object (different class) that is shown in the TaskManager (works fine if I close the dialog manually).
This is a code snippet of the creation of the dialog:
void TaskManager::on_pushButton_addTask_clicked()
{
Ui::AddTask addTaskDialog; // addTaskDialog takes the Ui of the file AddTask.ui
QDialog dialog;
addTaskDialog.setupUi(&dialog);
dialog.exec(); // shows the dialog
[More Code]
}
I wanted to close the QDialog when the user clicks on QDialogButtonBox:
ok -> closes the dialog
cancel -> closes and deletes the dialog.
I have tried something like this, but it doesn't work (I get this build issue: "No matching member function for call to 'connect'):
QPushButton* ok = addTaskDialog.buttonBox->button(QDialogButtonBox::Ok);
connect(ok, &QPushButton::clicked, this, dialog.close());
Any help would be much appreciated!
connect(ok, &QPushButton::clicked, this, dialog.close()); - your connect is wrong, you don't pass a pointer to a member function as forth parameter but the return value of dialog.close(). Also the context is wrong -> connect(ok, &QPushButton::clicked, &dialog, QQDialog::close).
btw: Your whole approach on how to create this dialog looks fishy - better create a new class derived from QDialog and do the stuff in there.

How to set focus after execution QAction

Main widget steals focus after execution QAction.
I need the focus to be set to popup widget.
QAction *action = new QAction(tr("show popup"), this);
connect(action, &QAction::triggered, this, &MyWidget::showPopup);
addAction(action);
void MyWidget::showPopup()
{
QMessageBox* popup = new QMessageBox(this);
popup->setModal(true);
popup->show();
popup->setFocus();
}
MyWidget inherits from QWidget.
Because you just created popup, it's not 'there' yet in the GUI. Even the show() doesn't instantly show it. After you leave the scope of MyWidget::showPopup(), the GUI event loop will continue looping and be able to process your new popup. Thus the setFocus() call comes too early.
But there is help underway:
QWidget::setFocus() is a slot, so you can invoke it.
If you use a timer (QTimer::singleShot(0, popup, SLOT(setFocus()));), it should work.
Maybe you'll need to use 10ms instead of 0ms.

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.

How to set input focus to a shown dialog in Qt?

In a button click slot, I create and exec() a dialog with a NULL parent. Inside the dialog's constructor, I have:
this->activateWindow();
this->raise();
this->setFocus();
The dialog is application modal and has strong focus. However, it does NOT respond to keyboard events until I click on it. How do I make the dialog get focus without having to click it?
The problem was that I was setting the Qt:Tool window flag. Using Qt::Popup or Qt::Window instead will cause input focus is automatically set when the dialog is shown.
I used Qt::Window myself. Some of the other flags will probably work as well, but the main thing is that a QDialog with the Qt::Tool flag will not automatically set input focus when the dialog is shown.
Install the Event filter for the dialog.
classObject->installEventFilter(this);
void className::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Space)
{
focusNextChild();
}
else
{
QLineEdit::keyPressEvent(event);
}
}
for more info refer here.
http://doc.trolltech.com/4.6/eventsandfilters.html
In my case even settings Qt::Window didn't do the trick. I had to
QMetaObject::invokeMethod(widgetToFocus, "setFocus", Qt::QueuedConnection);
before show() or exec().

Problem with event handling on QToolButton in Linux

I am developing an application, I have added a QToolBar object in that, and have added the QToolButton object on that, I have also connect the clicked() event with that but the problem is that the mouse click event don't work on QToolButton but when I bring focus on that using Tab, then space button works fine, but I want it with mouse click.. any idea? here is the code.
pToolBar = new QToolBar(this);
pToolBar->setAllowedAreas(Qt::NoToolBarArea);//NoToolBarAreaAllToolBarAreas
pToolBar->setFloatable(false);
pToolBar->setGeometry(300,0,160,30);
QToolButton *playButton=new QToolButton(pToolBar);
playButton->setIcon(QIcon("/images/play.png"));
playButton->setGeometry(10,0,40,30);
playButton->setToolTip("Play/Pause");
connect(playButton, SIGNAL(clicked()),SLOT(playButtonClicked()));
The Tool buttons are normally created when new QAction instances are created with QToolBar::addAction() or existing actions are added to a toolbar with QToolBar::addAction().
Example:
QAction *newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
newAct->setShortcut(tr("Ctrl+N"));
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAct);
You can use triggered signal, This signal is emitted when the given action is triggered.
Your example:
QToolButton *playButton=new QToolButton(pToolBar);
connect(playButton, SIGNAL(triggered()),SLOT(playButtonClicked()));
Try explicitly adding the toolbutton to the toolbar. The following code works perfectly for me:
QToolBar *pToolBar = new QToolBar(this);
QToolButton *playButton=new QToolButton(pToolBar);
playButton->setIcon(QIcon("/images/play.png"));
playButton->setText("Play");
playButton->setToolTip("Play/Pause");
playButton->setGeometry(10,0,40,30);
QAction *a = pToolBar->addWidget(playButton);
a->setVisible(true);
connect(playButton, SIGNAL(clicked()),SLOT(playButtonClicked()));
You should probably save the QAction pointer somewhere, since it's the easiest way to assign keyboard shortcuts, enable / disable the button etc. Let me know if this works for you. If it doesn't, perhaps posting a complete compilable example here will help us help you. You should be able to get a small demo program that shows your problem within one or two files.
Cheers,
As jordenysp indirectly explains, the API is QAction centric