Can't convert QString to LPCWSTR

Hello guys, i have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "urlmon.h"
#include "string"
#include "iostream"
#include "cstring"

//...A lot of s*it!

void MainWindow::on_pushButton_clicked()
{
    QString url;
    QString path;

    path = ui->lineEdit_2->text();
    url= ui->lineEdit->text();
    ui->label_2->setText(url+"//"+path);
    URLDownloadToFile(NULL,&url,&path,NULL);
}

//other sh*t


I've got an error in the line
URLDownloadToFile(NULL,&url,&path,NULL);

...\mainwindow.cpp:27: error: cannot convert 'QString*' to 'LPCWSTR {aka const wchar_t*}' for argument '2' to 'HRESULT URLDownloadToFileW(LPUNKNOWN, LPCWSTR, LPCWSTR, DWORD, LPBINDSTATUSCALLBACK)'
URLDownloadToFile(NULL,&url,&path,NULL);
^
Hi xMarioz,

Your code looks fairly alien to me because I don't do QT. However, I looked up QString to see what it was and it appears to be some sort of QT String Class. That being the case, it seems likely to me that the address of a QString object (which isn't a pointer to a character string, but rather a pointer to a container which contains a pointer to a string), which is what you are passing to URLDownloadToFile in the 2nd parameter, isn't the pointer to a string that the function wants. I realize that's about as clear as mud! Think of it this way, if you were using wstring::c_str() you would be passing the address of the string that the function wants.
hi @freddie1

yeah but in QT i get an error if i try to write &path.c_str() so how i must write?
I just briefly looked at QString xMario; enough though to see it had a lot of member functions as is typical of any String Class. Actually, I have my own String Class which I wrote many years ago and use in lieu of the C++ Std. Lib. String Class. But to answer your question you need to research the QString Class to determine which member returns a wchar_t* to the underlying character string controlled by the String object. Naturally, QString::c_str() won't work because that is the name of the member function of the std::string and std::wstring classes which return a pointer to the character string controlled by the class. In my string class my member that does that is named String::lpStr(). So you need to read through all the member function names to find which member returns a wchar_t* to the string controlled by the class. That is the only type of object/parameter URLDownloadToFile() will take. Hope I've explained it OK. Good luck!
Try

URLDownloadToFileA(NULL,url.c_str(),path.c_str(),NULL);

Notice the 'A' at the end of the function name (= ANSI). See:

https://msdn.microsoft.com/en-us/library/ms775123%28v=vs.85%29.aspx
1
2
3
4
5
6
7
8
9
10
11
char *qtToCString(QString &str)
{
  int length = str.length();
  char *str = new char[length + 1];
  for (int i = 0; i < length; ++i)
  {
    str[i] = str.at(i).toAscii();
  }
  str[length] = 0;
  return str;
}


Explaination for the function:
The function I provided will let you pass in a Qt string by reference and then extracts each of its characters. As each character is extracted, it is placed into the "c" string.

You can use this string (char* type) for URLDownloadToFileA specified by coder777. You might need to cast the result of "qtToCString" to a const char* to use URLDownloadToFileA though.

Edit:
It looks like you could also take the qt string and do something like:
qstring.toAscii().data();
Last edited on
Topic archived. No new replies allowed.