Results 1 to 4 of 4

Thread: QFileDialog and QMessageBox not working. How to fix it?

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default QFileDialog and QMessageBox not working. How to fix it?

    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.

    Qt Code:
    1. #include <QFileDialog>
    2. #include <QMessageBox>
    3. //...code here
    4. filePath = QFileDialog::getOpenFileName(this, QObject::tr("Open File"), "D:/Texts", QObject::tr("Text Files (*.txt)"));
    5. //...code here
    6. QMessageBox::critical(this, QObject::tr("Error"), QObject::tr("Blablablabla."));
    To copy to clipboard, switch view to plain text mode 

    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."),Q Object::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!

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QFileDialog and QMessageBox not working. How to fix it?

    Quote Originally Posted by robgeek View Post
    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
    Last edited by jefftee; 3rd April 2015 at 05:17.

  3. #3
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: QFileDialog and QMessageBox not working. How to fix it?

    Quote Originally Posted by jefftee View Post
    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.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileDialog and QMessageBox not working. How to fix it?

    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.

Similar Threads

  1. QFileDialog::setSidebarUrls not working in MacOSX
    By Machado in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2011, 11:53
  2. QFileDialog/ QMessageBox's Button Translation problem
    By santosh.kumar in forum Qt Programming
    Replies: 6
    Last Post: 10th August 2009, 07:07
  3. QMessageBox misbehaves after QFileDialog call
    By MikeG in forum Qt Programming
    Replies: 8
    Last Post: 2nd June 2009, 10:17
  4. resize of QMessageBox not working?
    By Sheng in forum Qt Programming
    Replies: 2
    Last Post: 13th November 2008, 14:25
  5. Replies: 11
    Last Post: 31st May 2007, 01:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.