2

I'm trying to write a simple C++ code using Qt to grab the path to a folder.

I got the code from this answer and tweaked it a bit to fit what I wanted. My problem is that it's flagging my 'this' declaration saying my class is incompatible with the 'QWidget *' parameter type.

#include <iostream>
#include <qt5/QtWidgets/qfiledialog.h>

using namespace std;

class TCC {
public:
    string openFile();
};

string TCC::openFile()
{
    QFileDialog::getOpenFileName(this, tr("Open Document"), QDir::currentPath(), tr("Document files (*.doc *.rtf);;All files (*.*)"), 0, QFileDialog::DontUseNativeDialog);

    QString filename = QFileDialog::getOpenFileName(
        this,
        tr("Open Document"),
        QDir::currentPath(),
        tr("Document files (*.doc *.rtf);;All files (*.*)"));
    if (!filename.isNull())
    {
        qDebug(filename.toUtf8());
    }
    return filename.toUtf8().constData();
}

int main()
{
    TCC tcc;
    cout << tcc.openFile();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I believe the pointer you pass to `getOpenFIleName` is expect to be of a class type that derives from `QWidget` – jkb Aug 28 '21 at 21:54
  • Passing a pointer to one of your QWidgets as the first argument will help Qt know which monitor to open the dialog on (it will open onto the same monitor as that widget is on) – Jeremy Friesner Aug 29 '21 at 01:40
  • The dialog will inherit the stylesheet from parent's widget too. – cbuchart Aug 31 '21 at 07:19

1 Answers1

6

There are several errors in your code:

  1. QFileDialog requires a QWidget or a nullptr as the first argument.
  2. tr() is a QObject method, since there is none, you must use QObject::tr().
  3. To convert QString to std::string you must use the toStdString() method.
  4. Any QWidget(like QFileDialog) requires a QApplication to have been created before.
#include <QApplication>
#include <QFileDialog>

#include <iostream>

class TCC {
public:
    std::string openFile();
};

std::string TCC::openFile()
{
    QString filename = QFileDialog::getOpenFileName(
                nullptr,
                QObject::tr("Open Document"),
                QDir::currentPath(),
                QObject::tr("Document files (*.doc *.rtf);;All files (*.*)"));
    return filename.toStdString();
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    TCC tcc;
    std::cout << tcc.openFile()<< std::endl;

    return EXIT_SUCCESS;
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I understood what you were saying! I maaged to get it working and to change it to only accept folders to grab folder paths. Thanks! – Vinícius Inacio Breda Aug 29 '21 at 06:33
  • Using `QObject::tr` only has sense if translations are required. As far as I can understand from OP code it doesn't seem to be the case. If so, it could be simplified passing the raw string (implicit construction of the `QString`). – cbuchart Aug 31 '21 at 07:17
  • Also, let's note that ' QString::toStdString` returns an UTF-8 string, so be careful if you are expecting any other page code. – cbuchart Aug 31 '21 at 07:18