Simply update label in Qt - c++

I have created a label with Qt Creator using a unique name, 'statusLabel'.
I then made a function to update this status label like so:
//Function to set the text in the status bar.
void AutoFish::updatelabel(QString str)
{
ui->statusLabel->setText(str);
}
This does not work and gives the following error:
C:\Qt\Tools\QtCreator\bin\AutoFish\autofish.cpp:24: error: C2227: left of '->statusLabel' must point to class/struct/union/generic type
I'm not sure what I'm doing wrong, I'm just trying to update the label text using that function. Should I be using something other than a label? I've been looking into slots to create an event to update the label, but most slot examples I find involve a pushButton as an event start, and that is not what I need.
Thank you.
EDIT: As per request, here is all of my source code(it's not very big): http://pastebin.com/CfQXdzBK

Because your method is declared as static, you can't acces non-static member ui directly.
Change
static void AutoFish::updatelabel(QString str);
to
void updatelabel(QString str);
in your header file.
There is no need for static keyword, because you want to set label for the specific instance of the window. Also, there is no need for AutoFish:: as you are declaring a method inside class declaration (however, you do need it in your cpp file).
As per the second error - inside your getWindow function, you need to have a instance of the AutoFish object in order to call updateLabel. So, either change your getWindow definition to:
HWND getWindow(AutoFish *af, LPCSTR processName)
{
HWND hwnd = FindWindowA(0, processName);
if(!hwnd) {
std::cout << "Error: Cannot find window!" << std::endl;
af->updatelabel("Error: Cannot find window.");
}
else {
std::cout << "Seccess! Window found!" << std::endl;
af->updatelabel("Seccess! Window Found!");
}
return hwnd;
}
and call it like this:
HWND window = getWindow(this, "FFXIVLauncher");
or make getWindow member of AutoFish class:
class AutoFish : public QMainWindow
{
// ...
HWND getWindow(LPCSTR processName);
// ...
};
HWND AutoFish::getWindow(LPCSTR processName) {
HWND hwnd = FindWindowA(0, processName);
if(!hwnd) {
std::cout << "Error: Cannot find window!" << std::endl;
updatelabel("Error: Cannot find window.");
}
else {
std::cout << "Seccess! Window found!" << std::endl;
updatelabel("Seccess! Window Found!");
}
return hwnd;
}
and this pointer will be implicitely passed to the getWindow.

Related

How can I show a window with the a name given as parameter in Windows in C++

I would like to make my software usable for Linux and Windows (Linux already works). Now I still need some functions so I can run the software on Windows, too.
I am currently trying to use the EnumWindows() function to get the window names and then show the window in the foreground (which matches the parameter).
static BOOL CALLBACK setWindowFocus(HWND hWnd, LPARAM lparam) {
int length = GetWindowTextLength(hWnd);
char* buffer = new char[length + 1];
GetWindowText(hWnd, buffer, length + 1);
std::string windowTitle(buffer);
// List visible windows with a non-empty title
if (IsWindowVisible(hWnd) && length != 0) {
// Check if it is the right Windowshandle
if ( windowTitle.compare(programname) == 0 ) <-- programname is a static variable
{
// Set application to the foreground
SetForegroundWindow(hWnd);
}
}
return TRUE;
}
Additionally, I used this to create the variable:
std::string programname;
And this to call it:
static void setWindowFocus(std::string programname)
{
std::cout << "Setting focus to window." << std::endl;
tempsavedProgramname=programname;
EnumWindows(setWindowFocus, NULL);
}
That is working, as long as it is in main(). But, I would like to have it in an extra class with some other functions (I would like to remove the static variable too, if possible).
Is there a way I can use the EnumWindows() function with an anonymous function, or something?
Can I use something like this to pass a string to the function:
EnumWindows(setWindowFocus, reinterpret_cast<LPARAM>(stringvariable));
Or, are there other ways which I can try to reach my goal?
Includes which I used for the code:
Windows.h
winuser.h
string
iostream
I hope that I did not forgot one.
Yes, you can use the LPARAM to pass a string variable into your callback, eg:
static BOOL CALLBACK setWindowFocus(HWND hWnd, LPARAM lparam) {
std::string &programname = *reinterpret_cast<std::string*>(lparam);
int length = GetWindowTextLength(hWnd);
char* buffer = new char[length + 1];
GetWindowText(hWnd, buffer, length + 1);
std::string windowTitle(buffer);
delete[] buffer; // <-- ADD THIS!
/* I would use this instead:
int length = GetWindowTextLength(hWnd);
std::string windowTitle(length+1, '\0');
windowTitle.resize(GetWindowText(hWnd, &windowTitle[0], length + 1));
*/
// List visible windows with a non-empty title
if (IsWindowVisible(hWnd) && (length != 0)) {
// Check if it is the right Windowshandle
if (windowTitle == programname)
{
// Set application to the foreground
SetForegroundWindow(hWnd);
return FALSE;
}
}
return TRUE;
}
static void setWindowFocus(std::string programname)
{
std::cout << "Setting focus to window." << std::endl;
EnumWindows(setWindowFocus, reinterpret_cast<LPARAM>(&programname));
}
And yes, you can use a C++11 lambda for the callback, rather than a static class method, but only if you use a non-capturing lambda, which is implicitly convertible to a function pointer (capturing lambdas are not). Fortunately, the LPARAM makes that a possibility, eg:
static void setWindowFocus(std::string programname)
{
std::cout << "Setting focus to window." << std::endl;
EnumWindows(
[](HWND hWnd, LPARAM lparam) -> BOOL {
std::string &programname = *reinterpret_cast<std::string*>(lparam);
// ...
},
reinterpret_cast<LPARAM>(&programname)
);
}
Now, that being said, there is a much simpler solution - since you already know the exact window text you are looking for, you can use FindWindow() instead of EnumWindows(), eg:
static void setWindowFocus(std::string programname)
{
std::cout << "Setting focus to window." << std::endl;
HWND hWnd = FindWindowA(NULL, programname.c_str());
if (hWnd != NULL) {
// Set application to the foreground
SetForegroundWindow(hWnd);
}
}
Here's the callback wrapped up in a class
class enum_windows {
protected:
virtual BOOL call_back(HWND hwnd) {
// Your code here
return TRUE;
}
public:
void start() {
EnumWindows([](HWND hwnd, LPARAM lParam) -> BOOL {
enum_windows * obj = reinterpret_cast<enum_windows *>(lParam);
return obj->call_back(hwnd);
}, reinterpret_cast<LPARAM>(this));
}
};
(You've already accepted an answer - I'm a slow typer 😄). I'll leave this here anyway.

MFC Debug Assertation Failed!! wincore.cpp Line 972

I have created an MFC Dialog box in a DLL for use in multiple projects and it has functionalities such as:
Getting Listbox data from the main application using the DLL. I can push string data through the main application to the MFC Dialog box but I am getting Assertation errors after compilation.
This process happens in a thread where one thread keeps the Dialog box active and another pushes data as shown in the code below.
void dbox(CDialogClass *dlg)
{
dlg->ShowDlg();
}
void input(CDialogClass *dlg)
{
string str1= "";
while (1)
{
getline(cin, str1);
//cin >> str1;
dlg->SetData(str1);
}
}
int main()
{
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
}
else
{
CDialogClass *dlg = new CDialogClass("Title Dbox",300.0f, 300.0f, 0);
thread t1(input, dlg);
thread t2(dbox, dlg);
t1.join();
t2.join();
}
}
return 0;
}
Here dbox() invokes a ShowDlg function which is in an MFC DLL as below:
void CDialogClass::ShowDlg()
{
dlgg->title = title;
dlgg->dialogWidth = D_width;
dlgg->dialogHeight = D_height;
dlgg->pos = pos;
dlgg->Create(IDD_DIALOG1);
dlgg->ShowWindow(SW_SHOWNORMAL);
dlgg->RunModalLoop();
//dlgg->DoModal();
}
SetData() is called by thread input() and it has the below code in the DLL:
void CDialogClass::SetData(string data)
{
p_text = data;
dlgg->calldata(data);
}
Below is the code for my Dialog class in the DLL just for reference if needed-
#include "stdafx.h"
#include "DlgDisp.h"
#include "afxdialogex.h"
#include "Resource.h"
#include <fstream>
#include <Thread>
IMPLEMENT_DYNAMIC(CDlgDisp, CDialogEx)
CDlgDisp::CDlgDisp(CWnd* pParent /*=NULL*/)
: CDialogEx(CDlgDisp::IDD, pParent)
{
}
CDlgDisp::~CDlgDisp()
{
}
void CDlgDisp::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_listbox);
}
BOOL CDlgDisp::OnInitDialog()
{
//Title manipulations
char *str_title;
str_title = &title[0];
SetWindowText((CAtlString)str_title);
//Size manipulations
CWnd* pctrl = GetDlgItem(IDC_LIST1);
CRect rectctrl;
SetWindowPos(NULL, 0, 0, dialogWidth, dialogHeight, SWP_NOMOVE | SWP_NOZORDER);
pctrl->GetWindowRect(rectctrl);
pctrl->SetWindowPos(NULL, 20, 20, dialogWidth-120, dialogHeight-80, SWP_NOMOVE | SWP_NOZORDER);
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
BEGIN_MESSAGE_MAP(CDlgDisp, CDialogEx)
END_MESSAGE_MAP()
void CDlgDisp::calldata(string strdata)
{
char *str_parameter;
str_parameter = &strdata[0];
CString param = _T("");
param.Format(_T("%s"), (CAtlString)str_parameter);
if (pos == 0)
{
m_listbox.InsertString(0, param);
}
else
m_listbox.AddString(param);
UpdateData(FALSE);
}
the flow of the program for references:
CDlgDisp class is the Dialog class derived from CDialogEx class.
CDialogClass is for interaction with external applications which is derived from CDialog class.
CDialogClass has a public member variable of CDlgDisp class.
external application -> object.CdialogClass -> object.CDlgdisp class
when I execute the program it runs well, and I get an error when I try to input data through the console. It does get printed in the Listbox dynamically but then it shows the Assertation Error.
Here is an image after execution
[enter image description here][1]
and here is the image after I enter the data in console and press enter
[enter image description here][2]
what do you guys think the problem is?
[1]: https://i.stack.imgur.com/pXFMD.png
[2]: https://i.stack.imgur.com/eUXZ7.png
Look into the source where the ASSERT ion take place
You find this comment:
// Note: if either of the above asserts fire and you are
// writing a multithreaded application, it is likely that
// you have passed a C++ object from one thread to another
// and have used that object in a way that was not intended.
// (only simple inline wrapper functions should be used)
//
// In general, CWnd objects should be passed by HWND from
// one thread to another. The receiving thread can wrap
// the HWND with a CWnd object by using CWnd::FromHandle.
//
// It is dangerous to pass C++ objects from one thread to
// another, unless the objects are designed to be used in
// such a manner.
MFC window objects use handle maps per thread. This usually doesn't allow you to use the objects for some functions from other threads. It is possible but this is discussed in many other threads.
The way you want to use the MFC isn't possible. Think about synchronisation. If the functions are thread safe that you want to use with the other window you may use SendMessage and the m_hWnd handle directly.
Thank you guys for being first responders to my problem. All the comments were useful and helped me in understanding the problem.
Problem: Since MFC is thread-safe therefore using an object to SetData was creating a memory sharing conflict between both the threads.
Solution: I used a custom message to pass information to be displayed dynamically. Below Links helped completely-
https://blog.csdn.net/xust999/article/details/6267216
On sending end:
::PostMessage(HWND_BROADCAST, WM_UPDATE_DATA, 0, 0);
On receiving end in the header file:
const UINT WM_UPDATE_DATA = ::RegisterWindowMessage(_T("UpdateData"));
Also, in the header file in the Dialog class:
afx_msg LRESULT OnUpdateData(WPARAM wParam, LPARAM lParam);
The above function will be called when the message is posted and all functionalities to be added to it such as-
LRESULT CDlgDisp::OnUpdateData(WPARAM wParam, LPARAM lParam)
{
char *str_parameter;
str_parameter = &parameter[0];
CString param = _T("");
param.Format(_T("%s"), (CAtlString)str_parameter);
if (pos == 0)
{
m_listbox.InsertString(0, param);
}
else
m_listbox.AddString(param);
return 1;
}
Thank you, Everyone.

GetWindowTextA unable to get the text of ComboBox MFC C++

I'm using GetWindowTextA to get the text of ComboBox but it will be an empty string "" even the hwnd is correct.
No problem when using GetWindowTextA to get the text from other class, but it won't work for the class ComboBox. Is it something related to this that need other function to get text from ComboBox?
Thanks.
Editied:
The combobox is from a control in some other app's window
#include<windows.h>
#include<iostream>
using namespace std;
int main() {
POINT pt;
Sleep(3000);
GetCursorPos(&pt);
HWND hWnd = WindowFromPoint(pt);
char class_name[100];
char title[100];
GetClassNameA(hWnd,class_name, sizeof(class_name));
GetWindowTextA(hWnd,title,sizeof(title));
cout <<"Window name : "<<title<<endl;
cout <<"Class name : "<<class_name<<endl;
return 0;
}
If you've added a CComboBox variable to your dialog class, using the "Add Control Variable" wizard, as described here Add a member variable, you can readily use the CComboBox methods to retrieve the text of the selected combo item, as illustrated below:
void CMFCDlgAppDlg::OnBnClickedButton1()
{
CString itemText;
m_Combo.GetLBText(m_Combo.GetCurSel(), itemText);
AfxMessageBox(itemText);
}
Adding TCHAR szBuf[100]; and using SendMessage will be done.
#include<windows.h>
#include<iostream>
using namespace std;
int main() {
POINT pt;
Sleep(3000);
GetCursorPos(&pt);
HWND hWnd = WindowFromPoint(pt);
char class_name[100];
char title[100];
TCHAR szBuf[100];
GetClassNameA(hWnd,class_name, sizeof(class_name));
GetWindowTextA(hWnd,title,sizeof(title));
SendMessage(hWnd, WM_GETTEXT, 100, (LPARAM)szBuf);
//cout <<"Window name : "<<title<<endl;
cout <<"Class name : "<<class_name<<endl;
wcout <<"Window name : "<<szBuf<<endl;
system("PAUSE");
return 0;
}

Unable to find child window of a parent window that don't have WindowName with ClassName #32770 (Dialog)

I am trying to get the hwnd of a child window(caption = "Reset") to apply in IsWindowVisible() function but the child window could not be found.
This is the code:
#include <iostream>
#include <windows.h>
using namespace std;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
char class_name[100];
char title[100];
GetClassNameA(hwnd,class_name, sizeof(class_name));
GetWindowTextA(hwnd,title,sizeof(title));
cout <<"Window title: "<<title<<endl;
cout <<"Class name : "<<class_name<<endl;
cout <<"hwnd : " <<hwnd<<endl<<endl;
return TRUE;
}
int main()
{
HWND hwnd = ::FindWindowA("#32770",NULL);
EnumChildWindows(hwnd,EnumWindowsProc,0);
system("PAUSE");
return 0;
}
There are a lots of window with the same classname #32770 (Dialog), also without title. After run the code, the result came out different types of window (with classname like WorkerW, IME, etc.).
The tree diagram get from Spy++ are like this:
...Window 00180726 "" #32770 (Dialog)
...Window 001F0962 "Reset" Button
I tried to find the child window if window title and window class(#32770) are included, it succeed.
My question is: How to find the child window (Reset) if we don't have a specific parent window? I tried apply EnumWindows, EnumChildWindows, FindWindows, FindWindowsEx in main() but still can't get what I expect.
Advance thanks for any kind of helps.
You can use WindowFromPoint to get parent dialog if you know its position then call EnumChildWindows to get its child

How can I skip hidden windows when using FindWindow()?

I create a window without showing it:
int main()
{
CreateWindow("SysListView32","Geek",0, 0, 0, 0, 0,NULL, NULL, (HINSTANCE)GetCurrentProcess(), NULL);
getch();
}
...and in another process use FindWindow() to find its handle:
int main()
{
HWND H = FindWindow("SysListView32", "Geek");
std::cout<< "The handle of created window is : " <<H;
getch();
}
How is FindWindow finding its handle? I assumed it would not find it, because process1 is not showing the window.
How can I find only visible windows?
Even if a window is not visible, it is of course in the list of all existing windows that FindWindow enumerates (you can display this list using Spy++ for example). If you do not want to search for hidden windows, you have to check their flags:
HWND H = FindWindow("SysListView32", "Geek");
if (H) {
LONG style = GetWindowLong(H, GWL_STYLE);
if (style & WS_VISIBLE)
std::cout << "The handle of created visible window is : " << H << std::endl;
else
std::cout << "The handle of created hidden window is : " << H << std::endl;
} else {
std::cout << "No such window found" << std::endl;
}
FindWindow finds top-level windows.
While your CreateWindow call is creating a top-level window (i.e. one with no parent) I'm not convinced it will actually work.
It's certainly very unusual (if not wrong) to create a top-level SysListView32. ListView controls should be the children of top-level windows, not top-level windows in their own right.