QFileDialog problem on Kubuntu
-
Hi All
I have strange problem with QFileDialog.
When I try to remove pointer to QFileDialog application hangs.
I checked my old project that worked before and it looks like the same problem appear.
Also I compiled and run the same application on Windows 7. On Windows application do not hang.
I created simple example with QFileDialog. When I try to remove pointer application hangs.
http://pastebin.com/3VLHZTYY
http://pastebin.com/R9i1HQ0SIt looks like problem appear during the last week. A few updates of Kubuntu appear.
I found one info about updates compared with qfiledialog:
https://www.kde.org/announcements/kde-frameworks-5.10.0.php"Fix native file dialogs from widgets QFileDialog: ** File dialogs opened with exec() and without parent were opened, but any user-interaction was blocked in a way that no file could be selected nor the dialog closed. ** File dialogs opened with open() or show() with parent were not opened at all."
I will be grateful for help!
My version of QT
Qt Creator 3.3.2 (opensource)
Based on Qt 5.4.1 (GCC 4.6.1, 64 bit)
Built on Mar 4 2015 at 00:07:23
With patch 3af3fe7ad3Kubuntu 14.04
KDE 4.13.3
QT version 4.8.6
Kernel 3.3.0-52-generic -
The bug report you linked to is related to having a dialog with no parent and using the exec() command. In your case you have a parent and you are not using exec().
In your version there are two things that seem odd:
- You delete the instance of the dialog when closed. This might be a problem as the parent will delete the dialog when it closes. Maybe something like this would be better:
void MainWindow::on_pushButton_clicked() { if(!wsk) // where wsk is assumed to be initialized to null. { wsk = new QFileDialog(this); } wsk->setFileMode(QFileDialog::ExistingFiles); wsk->setNameFilter(tr("Images (*.jpeg *.jpg)")); wsk->show(); } void MainWindow::on_pushButton_2_clicked() { wsk->hide(); // wsk->close(); // delete wsk; }
- You are using show/hide(or 'close') instead of exec() for a file dialog. Unless this is something you wish to have remain active without blocking the rest of the application it might be better to do something like this:
void MainWindow::on_pushButton_clicked() { if(!wsk) // wsk is set to null in constructor of MainWindow. { wsk = new QFileDialog(this); } wsk->setFileMode(QFileDialog::ExistingFiles); wsk->setNameFilter(tr("Images (*.jpeg *.jpg)")); if(wsk->exec() == QDialog::Accepted) { // dialog was not cancelled. Do something... } }
-
@Rondog
Hi! Thank you for fast respond!"1. You delete the instance of the dialog when closed. This might be a problem as the parent will delete the dialog when it closes. Maybe something like this would be better:"
Unfortunately it is only example. In my project pointer to QFileDialog is inside bigger class and this class is deleted too. QFileDialog pointer is deleted in destructor of this class.
"You are using show/hide(or 'close') instead of exec() for a file dialog. Unless this is something you wish to have remain active without blocking the rest of the application it might be better to do something like this:"
I tried this method.But still I can not remove object. I think this is not correct behaviour.
-
"Unfortunately it is only example. In my project pointer to QFileDialog is inside bigger class and this class is deleted too. QFileDialog pointer is deleted in destructor of this class."
You shouldn't delete the dialog 'wsk' in any part of the parent class. Qt will delete this dialog and all children automatically when the parent is deleted. The only time you need to delete a widget item (QFileDialog* in this case) is if you create it without a parent. I wouldn't recommend this as it adds other problems.
"But still I can not remove object. I think this is not correct behaviour."
Removing this object would be a problem for two reasons; the object will be deleted twice (once when you do it and later when the parent is deleted) and the parent may not know that the child has been deleted so it is still sending messages to it and so on. Likely the second condition is what causes the problems.
Unless there is a reason why you cannot re-use your instance of QFileDialog ('wsk') in some way I don't see why you need to delete the old one and create a new one each time the user clicks on a button.
I have never used a modeless QFileDialog before (using members 'show()' and 'hide()' instead of 'exec()') so I am not sure if this is a problem. I would be concerned that a modeless file dialog would become hidden behind other windows or multiple instances of it created. The underlying operating system might have a problem keeping a file dialog active indefinitely.
-
Actualization of Qt solved the problem. Now everything works fine.