PDA

View Full Version : Problem with QFileDialog



mobucl
20th April 2011, 17:18
Hi, I am having a strange problem with QFileDialog. Basically i have created a very simple piece of code for test purposes:



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QStringList fileNames;
QFileDialog dialog(this);
// dialog.setDirectory("C:\\Qt\\2010.05\\test1\\");
// dialog.setFileMode(QFileDialog::ExistingFiles);
//dialog.setNameFilter(trUtf8("CIFs (*.cif)"));
if (dialog.exec())
fileNames = dialog.selectedFiles();
}

MainWindow::~MainWindow()
{
delete ui;
}


This creates the dialog box when executed but it is very slow to open and there seems to be severe problems with the operation of the dialogbox itself.

Here is a screendump: http://img192.imageshack.us/i/picture1rz.png

So first the 'main window'with the icons is not correct as it is not showing any of the files in the directory indicated and it doesnt update when i change the directory to be 'looked in'using the navigation controls. I also cannot double click on the icons in this window. Note that the lines that are commented out dont seem to cause the problem....

Can someone please help me as i can create my application without being able to select and open multiple files.

Thanks

Matt

Added after 11 minutes:

UPDATE: I have just tried using the static call to QFileDialog:

QString fileName;
fileName = QFileDialog::getOpenFileName(this);


And this produces and entirely different selection window (which looks more like the 'usual' windows dialog and this works correctly, however i cannot with this select and load multiple files????

mcosta
21st April 2011, 08:21
I think taht opening a QFileDialog in QMainWindow constructor could be a problem because the MainWindow object is not yet completely constructed


QString fileName;
fileName = QFileDialog::getOpenFileName(this);
And this produces and entirely different selection window (which looks more like the 'usual' windows dialog and this works correctly, however i cannot with this select and load multiple files????


In Windows the static methods use NativeDialogs as default

mobucl
21st April 2011, 09:47
Hi mcosta,

Thanks for the suggestion, I created a push button and a clicked slot and added the code into this:



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_load_clicked()
{
QStringList fileNames;
QFileDialog dialog(this);
if (dialog.exec())
fileNames = dialog.selectedFiles();
}


So now i get the dialog box when i click the button BUT its behaviour is exactly the same.....

I was thinking that maybe there is a problem with QT not being able to find the standard windows dialogs unless I use static functions? Maybe i need to 'point'QT to these somewhere??

Thanks

Matt

mcosta
21st April 2011, 10:03
I tried your code and the result is

6270

so it works fine!

mobucl
21st April 2011, 10:12
Well that really is strange as i it does not work on my version of QT. I will reinstall and see if that fixes the problem. Iw ill also try the code before and after building QWT which i also have installed incase this causing an issue. Will let you know of the results when i get the chance to reinstall!

Thanks

mobucl
2nd May 2011, 14:20
So coming back to this problem.....I have just reinstalled QT from scratch using the 2010.05 SDK. I have NO other plugins installed and yet i still have the same problems as described above i.e:


QString fileName;
fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "C:", tr("Image Files (*.png *.jpg *.bmp)"));



works and gives me a standard XP dialog box but:


QStringList fileNames;
QFileDialog dialog(this);
if (dialog.exec())
fileNames = dialog.selectedFiles();


Gives me a completetly different dialog in which the 'main window'icons are broken, files do not appear and navigation in the main window doesnt work.

Can someone please help as this is starting to get very problematic as i really need to be able to open multiple files!

I am thinking it could be something to do with enviromental variables??? Maybe QT doesnt know where to look to open a standard window??? HELP!

laszlo.gosztola
2nd May 2011, 14:44
try to use QFileDialog::getOpenFileNames instead of QFileDialog::getOpenFileName

mobucl
2nd May 2011, 15:46
Hi laszlo.gosztola - thanks for the message. Yes that does work around the problem of opening more than one file!

But I really would like to get to the bottom of why i can't use the other method for opening files - anyone have the answer to this?

Hi laszlo.gosztola - thanks for the message. Yes that does work around the problem of opening more than one file!

But I really would like to get to the bottom of why i can't use the other method for opening files - anyone have the answer to this?

Added after 52 minutes:

Ok so following on I have now tried a couple of things and get some interesting results. it seems the prolem is something to do with using if (dialog.exec()) So this:


QFileDialog dialog;
QStringList filename;
if (dialog.exec())
filename = dialog.selectedFiles();


Gives me a broken dialog (even though this is exactly the code described in the online documentation:

http://img862.imageshack.us/i/broken.png/

But using these lines:


QFileDialog dialog;
QStringList filename;
filename = dialog.getOpenFileNames();


Works ok and gives me this dialog box which acts as expected:

http://img703.imageshack.us/i/workingr.png/

BUT i cant use this code variation:


QFileDialog dialog;
QStringList filename;
filename = dialog.selectedFiles();


As this doesnt open up a selection window for me. So it appears that whilst i have a work around i cant use the code which was actually described in the QT documentation. Has anyonw any ide why this is occuring from the code i show??