I am very new to Qt and OpenCV and am creating a project integrating both. The problem I am running into is that I have a button to load a file, which uses QFileDialog. The whole thing runs smoothly and my file gets loaded. However, it crashes if I click the load button a second time. It seems like the problem occurs at the call to QFileDialog::getOpenFileName, but I need an expert opinion.
This is the function for the button click.
void MainWindow::on_pushButton_clicked()
{
QFileDialog dialog(this);
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
dialog.setViewMode(QFileDialog::Detail);
// dialog.setAttribute(Qt::WA_DeleteOnClose);
// dialog.DontUseNativeDialog;
filename = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home",
tr("Images (*.png *.xpm *.jpg)"));
imageObject = new QImage();
imageObject->load(filename);
image = QPixmap::fromImage(*imageObject);
scene = new QGraphicsScene(this);
scene->addPixmap(image);
scene->setSceneRect(image.rect());
ui->graphicsView->setScene(scene);
ui->graphicsView->fitInView(scene->sceneRect(),Qt::KeepAspectRatio);
cvHandler = new OpenCVHandler(filename.toStdString());
}
I have already tried both the lines that are commented out. My search also turned up nothing that I could understand easily:
Crash when calling getOpenFileName from QItemDelegate's custom editor
QFileDialog opens a second (possibly parent) unwanted window
Qt File Dialog Rendered Incorrectly and Crashes
If at all relevant, I am on an Ubuntu 16.04 LTS system.
Thank you
The problem was in the commented lines. I didn't use dialog.DontUseNativeDialog properly. Using it inside the getOpenFileName function did the trick:
filename = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home",
tr("Images (*.png *.xpm *.jpg)"),0,QFileDialog::DontUseNativeDialog);
Thank you all.
Related
When I open an open file dialog the first time everything works as it should, meaning the dialog opens and I can select a certain file. The problem I have is that each time I am trying to open an open file dialog a second time my whole program freezes. I have no idea why this is happening. I have debugged my program and I've seen that each time the program freezes at the dialog.exec() method. This issue started after I have upgraded my qt creator to version 5.8. I have also tried deleting the dialog instance each time I press a button and creating a new one, the result was the same. I include the header #include < QFileDialog >. Bellow you have my code posted.
QFileDialog *dialog;
dialog = new QFileDialog(this);
dialog->setFileMode(QFileDialog::AnyFile);
dialog->setNameFilter(tr("Images (*.png *.xpm *.jpg *.bmp)"));
QStringList l;
QString file;
if (dialog->exec())
{
l = dialog->selectedFiles();
file = l.at(0);
//do things with the file
QMessageBox::information(this,"Image Loader","Image loaded successful");
}
If anyone knows how to solve this I would be grateful.
Firstly, for context, I am connecting a signal from triggered from QAction to a slot called fileOpen in this, and other similar connections are done in a method in my main window class like the following:
void MainWindow::createActions()
{
m_fileNew = new QAction("&New", this);
m_fileOpen = new QAction("&Open", this);
m_fileExit = new QAction("E&xit", this);
connect(m_fileNew, SIGNAL(triggered(bool)), this, SLOT(fileNew()));
connect(m_fileOpen, SIGNAL(triggered(bool)), this, SLOT(fileOpen()));
connect(m_fileExit, SIGNAL(triggered(bool)), this, SLOT(fileExit()));
}
To show the file dialog, the static method QFileDialog::getOpenFileName is used in MainWindow::fileOpen:
void MainWindow::fileOpen()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open Audio File"),
"", tr("WAVE Files (*.wav);;All Files (*.*)"));
if (filename != QString::null) {
m_fileName = filename;
}
}
The signal-slot connection between m_fileOpen and fileOpen works and displays the file dialog, but after closing the dialog the window takes longer to redraw while resizing.
Why is that happening, and how can I fix it?
All I had to do was build my Qt application in release mode, which means removing the "CONFIG+=debug" argument from the qmake call.
In release mode the performance degradation is gone, which is great, although I don't understand what differences between the debug and release versions of the Qt libraries allow this to occur.
I'm trying to debug a c++/Qt5.5 code in MSVS2010 Professional. A function has following lines of code,
/* Static method approach */
QString filters("Music files (*.mp3);;Text files (*.txt);;All files (*.*)");
QString defaultFilter("Text files (*.txt)");
QFileDialog::getSaveFileName(0, "Save file", QDir::currentPath(), filters, &defaultFilter);
The dialog is simply doesn't open and the application freezes.
I tried the alternative was as below.
/* Direct object construction approach */
QFileDialog fileDialog(0, "Save file", QDir::currentPath(), filters);
fileDialog.selectNameFilter(defaultFilter);
fileDialog.exec();
But again, the code freezes at 'fileDialog.exec()'.
So, I created a different new simple project with these statements only and the it worked as expected.
Is this a issue of my environment configuration. I tried to debug but on stepping into the above line simply freezes the code without any error.
This looks like a know issue in Qt.
https://forum.qt.io/topic/49209/qfiledialog-getopenfilename-hangs-in-windows-when-using-the-native-dialog/8
The workaround is to use QFileDialog::DontUseNativeDialog flag as below.
m_imageFile = QFileDialog::getOpenFileName(this, tr("Open Image"), QDir::homePath(), tr("Image Files (*.png *.jpg *.bmp)"), 0, QFileDialog::DontUseNativeDialog); //works
Thanks for the help though!
I had the same problem and discovered this could be because of a bad COM initialization in your UI thread. If you have somewhere:
HRESULT hres = CoInitializeEx( 0, COINIT_MULTITHREADED );
It MUST be replaced by:
HRESULT hres = CoInitializeEx( 0, COINIT_APARTMENTTHREADED );
I think the native window is maybe using COM calls, and just sits here because of a deadlock.
I've just started to deploy my first application for Ubuntu using QtCreator 3.1.1 based on Qt5.2.1 on Ubuntu 14.10.. I need to open some video files, so I'm going to put a Button to choose file obviously. Also I know I can use this function to open files:
FileDialog {
id: fileDialog
title: "Please choose a file"
onAccepted: {
console.log("You chose: " + fileDialog.fileUrls)
Qt.quit()
}
onRejected: {
console.log("Canceled")
Qt.quit()
}
Component.onCompleted: visible = true
}
Here's my question: How can I connect them to each other? I want the FielDialog to be opened when I click on the button. And is that the only way to do this? I mean couldn't I do the same process in C++ code?
Of course you can do in C++.
There's a Qt class called QFileDialog: http://qt-project.org/doc/qt-5/QFileDialog.html
You can simply connect a button clicked signal to a slot that creates a QFileDialog, you can use some of the static functions like in the example:
fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));
and here how to use the QPushButton: http://qt-project.org/wiki/How_to_Use_QPushButton
I am trying to get a file path at my Qt project. When I was using KDE4 and Qt 4.8 everything worked fine but now I updated to Qt 5 and Gnome 3. And the QFileDialog now looks like this:
Or even like this:
But more often it looks like this:
I noticed the behaviour changes depending on the path being passed as the initial path for the dialog. E. g., if I call
QString path = QFileDialog::getOpenFileName(qobject_cast<QWidget*>(this->parent()), tr("Choose model"), QCoreApplication::applicationDirPath(), QLatin1String("*.obj"));
then I get the second case screenshot.
Yet, if I call
QString path = QFileDialog::getOpenFileName(qobject_cast<QWidget*>(this->parent()), tr("Choose model"), QString(), QLatin1String("*.obj"));
then I get any folder empty while it is not (the first case screenshot).
Also, using this hint, I managed that commenting one line at my ~/.gtkrc-2.0 file and changing the theme to ambiance instead of adwaita at gnome-tweak-tool makes my application show the second-case screenshot.
I use this code to show up the dialog:
QString path = QFileDialog::getOpenFileName(qobject_cast<QWidget*>(this->parent()), tr("Choose model"), QCoreApplication::applicationDirPath(), QLatin1String("*.obj"));
Here, this points to the QGraphicsScene ancestor, OpenGLScene. Here is the code where I create one:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
GraphicsView view;
view.setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
view.setScene(new OpenGLScene);
view.show();
view.resize(1024, 768);
return app.exec();
}