Accessing QTextEdit from another program



  • I have a program written in C++ using QTextEdit. I have another program written in C#, which I need to access a QTextEdit that is in the C++ program. From C#, I am able to find the parent window with FindWindow api call but cannot find the child window with FindWindowEx. My objective when finding the child window was then get the text of the QTextEdit box. It looks like QWidget does not allow messages using SendMessageA and WM_GETTEXT. Is there another way to get the text from QTextEdit from a C# program?

    Thanks Before Hand



  • Hi,

    this question isn't Qt related.
    A solution (if exists) must be searched in Windows API.

    Hi



  • I disagree, a normal textbox uses FindWindowEx to reveal itself. Even though QWidget reveals itself with FindWindow. QTextEdit does not use the FindWindowEx to reveal itself. It is definitely not a Windows API issue, it is an issue with the particular way that QTextEdit behaves.



  • Hi,

    what I mean is that there's no solution using Qt API.
    If you want you can try to open a Issue Request on QT Bug Report.

    P.S. what do you mean with normal textbox ?



  • Thank you for your help. Normal textbox would be a textbox created with the standard windows controls in Microsoft Visual Studio.

    Regarding the suggestion you made to try a microsoft forum, it has already been attempted and the issue was pointed back to qt:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/061413af-04a8-404e-a824-f43f7614ed5f/reading-text-in-third-party-software-to-visual-studio-2010-express?forum=vbgeneral



  • I am going to start putting my progress so far in case someone can help me. So far I understand I need to make a reference to the dll's used by Qt in C#:

    [DllImport("QtCore4.dll")]

    Now I need to figure out how to receive data from QTextEdit...



  • I dont know if WinAPI has a function to list all elements of a window. But if there is a function like this than you could simply read all of these values one time, watch the values manually and find out this way how the Edit-Box is called.

    I guess that the WinAPI-Control-Elements are not named the same like the abstract Qt-Elements.

    This way you wouldnt need to import Qt-Stuff. Or do you need a more generic solution?



  • If you see link above, it has already been attempted using windows api and your suggestion (using the FindWindow/FindWindowEx api's). It finds the handle to the QWidget (using FindWindow) but then it does not find any child textbox handles for QTextEdit (using FindWindowEx). If it was a standard windows textbox (Using Microsoft Visual Studio with the common controls), it finds it using FindWindowEx as it normally should. That is when I thought of using the QtCore4.dll or similar Qt dll. From there maybe something with QProcess can be called to get the text from a QTextEdit textbox?



  • I was able to resolve this using SendKeys command, select of all text (CTRL-A) and then a copy (CTRL-C) to the clipboard. I then retrieved the text from the clipboard into my C# program. Here is the code in case anyone is interested: (The only caveat is that the application cannot be minimized, even then there might be a way to maximize the application before sending the commands) Note: If you have more than one QTextEdit box, you might need to send some tab commands using SendKeys.SendWait("{TAB}") before you send the CTRL commands.
    @
    [DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    public void getAppData()
    {

    // Get AppName handle
    System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("AppName"); //search for process
    if (p.Length > 0) //check if window was found
    {
    SetForegroundWindow(p[0].MainWindowHandle); //bring window handle to foreground
    }
    else
    {
    MessageBox.Show("Error: Please make sure AppName is running");
    }

    // Wait 500 milliseconds
    System.Threading.Thread.Sleep(500);

    // Send CTRL-A (Select All) command to AppName to select all text
    SendKeys.SendWait("^(a)");

    // Send CTRL-C (Copy) command to AppName to send data to clipboard
    SendKeys.SendWait("^(c)");

    // Wait 3 seconds before checking clipboard (To give chance to data to move to clipboard)
    System.Threading.Thread.Sleep(3000);

    // Copy data from the clipboard
    string text;
    try
    {
    text = Clipboard.GetText().Trim();
    }
    catch
    {
    text = Clipboard.GetText().Trim(); // sometimes you have to try again...
    }

    // Update Text Data
    if (text != "")
    {
    txtData.Text = text;
    toolStripStatusLabel1.Text = "Data retrieved from AppName.";
    }
    else
    {
    toolStripStatusLabel1.Text = "No data retrieved from AppName.";
    }

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.