Results 1 to 7 of 7

Thread: Retrieving QString from TCHAR

  1. #1
    Join Date
    Jan 2008
    Posts
    31
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Retrieving QString from TCHAR

    My application requires me to obtain the first 7 characters from a window title strip. I've tried the following:

    Qt Code:
    1. #include <QWidget>
    2. #include <windows.h>
    3.  
    4. HWND hChild;
    5. PTSTR pTitle;
    6. QString szTitle;
    7.  
    8. hChild = GetForegroundWindow();
    9.  
    10. pTitle = (PTSTR) malloc ( 8 * sizeof (QChar)) ;
    11.  
    12. int iFlag = GetWindowText( hChild, pTitle, 8);
    13. szTitle = *pTitle;
    14.  
    15. labelTitle->setText( szTitle);
    To copy to clipboard, switch view to plain text mode 

    While szTitle is a buffer of TCHAR, QString is unicode so I set the datatype to QString in line 6.

    The above compiles, but line 12 returns 1 and line 15 only prints the first character in the title.

    I tried changing line 6 to TCHAR, but then I get a compile error at line 15, "invalid conversion from TCHAR to char *.

    An suggestions?

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Retrieving QString from TCHAR

    Qt Code:
    1. WCHAR myTitle[9];
    2.  
    3. hChild = GetForegroundWindow();
    4. GetWindowTextW( hChild, myTitle, 8);
    5. myTitle[8] = 0;
    6. labelTitle->setText( QString::fromUtf16(szTitle));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Retrieving QString from TCHAR

    What about more or less something like this:
    Qt Code:
    1. class NativeWindow : public QWidget
    2. {
    3. public:
    4. NativeWindow(WId wid) {
    5. QWidget::create(wid, false, false); // window, initializeWindow, destroyOldWindow
    6. }
    7. ~NativeWindow() {
    8. QWidget::destroy(false, false); // destroyWindow, destroySubWindows
    9. }
    10. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. NativeWindow window(GetForegroundWindow());
    2. labelTitle->setText(window.windowTitle());
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. #4
    Join Date
    Jan 2008
    Posts
    31
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Retrieving QString from TCHAR

    Thanks for the input, Christian and jpn.

    Note that the second argument in GetWindowTitle is a pointer to the buffer, not the buffer itself. Even correcting that unfortunately doesn't seem to work. Using WCHAR still gives compiler errors with type conversions.

    Using the NativeWindow class prints a blank line.

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Retrieving QString from TCHAR

    Quote Originally Posted by GTBuilder View Post
    Using the NativeWindow class prints a blank line.
    Then perhaps you'll have to pass true as initializeWindow parameter. I don't have access to a Windows machine atm so I cannot test myself, sorry.
    J-P Nurmi

  6. #6
    Join Date
    Jan 2008
    Posts
    31
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Retrieving QString from TCHAR

    I've solved the problem. The following code gives me what I want.

    Qt Code:
    1. #include <QWidget>
    2. #include <windows.h>
    3.  
    4. HWND hChild;
    5. PTSTR pTitle;
    6. QString str;
    7. char cTitle[8];
    8.  
    9. hChild = GetForegroundWindow();
    10. pTitle = (PTSTR) malloc ( 8 * sizeof (TCHAR)) ;
    11. GetWindowText( hChild, pTitle, 8);
    12.  
    13.  
    14. for( int i=0; i<7; i++)
    15. {
    16. cTitle[i] = static_cast<char>( *(pTitle + i));
    17. str.append( cTitle[i]);
    18. }
    19.  
    20. labelTitle->setText( str);
    To copy to clipboard, switch view to plain text mode 

    Not sure how universal such an approach is, but my target platform is specific, so not a problem.

  7. #7
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Retrieving QString from TCHAR

    @GTBuilder: Your approach is wrong as it only works for ascii window titles.

Similar Threads

  1. QString static callback function from CURL
    By tpf80 in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2007, 20:47
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  3. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  4. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10
  5. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 20:41

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.