Results 1 to 2 of 2

Thread: Can't convert QString to LPCWSTR

  1. #1
    Join Date
    Mar 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question Can't convert QString to LPCWSTR

    Hello guys, i have this code:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "urlmon.h"
    4. #include "string"
    5. #include "iostream"
    6. #include "cstring"
    7.  
    8. //...A lot of s*it!
    9.  
    10. void MainWindow::on_pushButton_clicked()
    11. {
    12. QString url;
    13. QString path;
    14.  
    15. path = ui->lineEdit_2->text();
    16. url= ui->lineEdit->text();
    17. ui->label_2->setText(url+"//"+path);
    18. URLDownloadToFile(NULL,&url,&path,NULL);
    19. }
    20.  
    21. //other sh*t
    To copy to clipboard, switch view to plain text mode 

    I've got an error in the line
    Qt Code:
    1. URLDownloadToFile(NULL,&url,&path,NULL);
    To copy to clipboard, switch view to plain text mode 

    ...\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);

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,248
    Thanks
    304
    Thanked 867 Times in 854 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can't convert QString to LPCWSTR

    That's because a pointer to a QString ("&url") is not a wchar_t pointer. Use one of these as the argument:

    Qt Code:
    1. url.toStdWString().c_str()
    2.  
    3. // or
    4.  
    5. url.utf16()
    To copy to clipboard, switch view to plain text mode 

    You will probably run into trouble later in your coding life if you continue to write code that #includes system files with the "" convention instead of the <> convention:

    Qt Code:
    1. // Should be
    2. #include <cstring>
    3. // and not
    4. #include "cstring"
    To copy to clipboard, switch view to plain text mode 

    This is because the "" convention tells the compiler to look first in the current directory (and in directories you explicitly tell the compiler to look in) for the include file, then look in system directories. The <> convention tells the compiler to look only in system directories. If one of those other directories accidentally contains a file with the same name as the system file, then you'll likely end up with all sorts of unexplained compilation errors that will take a while to figure out. If you mean to include a system file, then help the compiler out by telling it that.
    Last edited by d_stranz; 24th March 2016 at 17:12.

Similar Threads

  1. cannot convert cont char* to LPCWSTR
    By habbas33 in forum General Programming
    Replies: 1
    Last Post: 28th September 2014, 08:39
  2. Qstring convert to...
    By xxxollixxx in forum Qt Programming
    Replies: 4
    Last Post: 28th November 2013, 14:58
  3. How convert Qt::Key to QString?
    By lenny in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2012, 13:26
  4. how to convert int to QString?
    By phillip_Qt in forum Newbie
    Replies: 2
    Last Post: 5th October 2007, 09:07
  5. QString to LPCWSTR
    By bruccutler in forum Newbie
    Replies: 1
    Last Post: 1st May 2007, 17:32

Tags for this Thread

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.