PDA

View Full Version : QFileDialog and QMessageBox not working. How to fix it?



robgeek
3rd April 2015, 04:20
Good evening!

So, i made a class that makes part of my program. I called her "Historic". She does not inherit any other class. I tried to create a QFileDialog and a QMessageBox inside of one of my methods of this class, but i'm having the following error message.


#include <QFileDialog>
#include <QMessageBox>
//...code here
filePath = QFileDialog::getOpenFileName(this, QObject::tr("Open File"), "D:/Texts", QObject::tr("Text Files (*.txt)"));
//...code here
QMessageBox::critical(this, QObject::tr("Error"), QObject::tr("Blablablabla."));


D:\Programming\C-C++\Qt\Windows\historic.cpp:10: error: no matching function for call to 'QFileDialog::getOpenFileName(Historic*, QString, const char [18], QString)'
filePath = QFileDialog::getOpenFileName(this, QObject::tr("Open File"), "D:/Texts", QObject::tr("Text Files (*.txt)"));

D:\Programming\C-C++\Qt\Windows\historic.cpp:33: error: no matching function for call to 'QMessageBox::critical(Historic*, QString, QString)'
QMessageBox::critical(this,QObject::tr("Error."),QObject::tr("Blablablabla."));

I would like to know if why this is happening and if i really have to make my class inherits some other and which one is it.

Thanks!

jefftee
3rd April 2015, 06:10
I would like to know if why this is happening and if i really have to make my class inherits some other and which one is it.
Look at the documentation for QFileDialog::getOpenFileName and see what type of argument is expected for the first argument.

Why do you think you can create your own class that doesn't inherit from QWidget and expect the QFileDialog::getOpenFileName to be able to work with your completely unknown object? This has nothing to do with Qt of course. Inheritance is a core feature of the C++ language.

Please don't take offence, but if you don't have an understanding of C++ fundamentals, you'll likely struggle with Qt. You should brush up on your C++ before you get too frustrated.

Hope that helps.

Edit: Same thing for QMessageBox::critical

robgeek
3rd April 2015, 16:59
Look at the documentation for QFileDialog::getOpenFileName and see what type of argument is expected for the first argument.

Why do you think you can create your own class that doesn't inherit from QWidget and expect the QFileDialog::getOpenFileName to be able to work with your completely unknown object? This has nothing to do with Qt of course. Inheritance is a core feature of the C++ language.

Please don't take offence, but if you don't have an understanding of C++ fundamentals, you'll likely struggle with Qt. You should brush up on your C++ before you get too frustrated.

Hope that helps.

Edit: Same thing for QMessageBox::critical

I'm not offended and thank you for your help. I don't think i said in this post, but i'm a newbie. So, i thought i could work with QFileDialog because i included it first. I thought this would be enough to work with QFileDialog. I didn't know i have to inherit QWdget.

d_stranz
3rd April 2015, 18:54
I didn't know i have to inherit QWdget

You don't have to inherit QWidget to use QFileDialog. The first argument in QFileDialog::getOpenFileName() is a pointer to a class derived from QWidget that will serve as the dialog's parent. When the dialog appears, it will be centered on this parent on the screen. If you supply a null pointer (i.e. 0), then the dialog will be centered on the screen instead.

However, the more common use of QDialog-based widgets is to create them from within a class that is derived from QWidget, like the QMainWidow-based class that serves as your main application window. In that case, passing "this" as the QWidget parent works just fine. However you do it, if you create a QDialog, whatever you pass as parent must either be null or a pointer to a QWidget, not any old plain C++ class.