V
V
vvafree2017-10-12 12:49:16
Qt
vvafree, 2017-10-12 12:49:16

How can QFileDialog pop up a file open dialog and get the filename in char?

There is a console application. In which you want to add qt openfiledialog.
Only the name of the file selected in char is of interest, since then opencv image opening functions are used.
All #include done.

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.toAscii());
  }

Here is the code I tried. Doesn't work, swears at "this".
Planned then QString to char like this:
QString *qs = new QString("переведи меня в чары! :)");
char const* ch = qs->toLocal8Bit().constData();

PS Windows 7 x64, Visual Studio 2015, QT VC2015++

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2017-10-12
@Mercury13

this is a hidden parameter of every non-static method that tells what object it is called on. If this object is not a QWidget, or a static function, or not a method at all, then either substitute another QWidget (meaning: the taskbar button is the same as this QWidget), or set it to NULL (create a new taskbar button).
In your case, of course, NULL. Or nullptr if you are working in C++11.

QString *qs = new QString("переведи меня в чары! :)");
char const* ch = qs->toLocal8Bit().constData();

They came up with the right idea, but there is no need to start a string on the heap with the new operation. A line in Qt is already quite "thrifty", so that's enough
QString qs("переведи меня в чары! :)");
char const* ch = qs.toLocal8Bit().constData();

Just do not forget about the lifetime of this char const *: it will live exactly as long as the specific value of the string will live.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question