C++ 只需在Qt中更新标签
我已经用Qt Creator创建了一个标签,使用了唯一的名称“statusLabel” 然后,我创建了一个函数来更新此状态标签,如下所示:C++ 只需在Qt中更新标签,c++,qt,user-interface,qt5,C++,Qt,User Interface,Qt5,我已经用Qt Creator创建了一个标签,使用了唯一的名称“statusLabel” 然后,我创建了一个函数来更新此状态标签,如下所示: //Function to set the text in the status bar. void AutoFish::updatelabel(QString str) { ui->statusLabel->setText(str); } 这不起作用,并产生以下错误: C:\Qt\Tools\QtCreator\bin\AutoFis
//Function to set the text in the status bar.
void AutoFish::updatelabel(QString str)
{
ui->statusLabel->setText(str);
}
这不起作用,并产生以下错误:
C:\Qt\Tools\QtCreator\bin\AutoFish\autofish.cpp:24: error: C2227: left of '->statusLabel' must point to class/struct/union/generic type
我不确定我做错了什么,我只是尝试使用该函数更新标签文本。我应该使用标签以外的东西吗?我一直在寻找插槽以创建一个事件来更新标签,但我发现的大多数插槽示例都涉及一个按钮作为事件开始,这不是我需要的
多谢各位
编辑:根据请求,这里是我所有的源代码(不是很大):因为您的方法声明为
静态
,所以您不能直接访问非静态成员ui
改变
static void AutoFish::updatelabel(QString str);
到
在头文件中
不需要使用static
关键字,因为您希望为窗口的特定实例设置标签。另外,当您在类声明中声明一个方法时,不需要AutoFish::
(但是,您在cpp文件中需要它)
根据第二个错误-在getWindow
函数中,需要有AutoFish
对象的实例才能调用updateLabel
。因此,将getWindow
定义更改为:
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;
}
或者使getWindow
成为AutoFish
类的成员:
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;
}
class AutoFish:公共QMainWindow
{
// ...
HWND getWindow(LPCSTR进程名);
// ...
};
HWND AutoFish::getWindow(LPCSTR进程名){
HWND HWND=FindWindowA(0,进程名);
如果(!hwnd){
std::cout感谢您的帮助!我收到以下代码错误:C:\Qt\Tools\QtCreator\bin\AutoFish\AutoFish.cpp:24:error:C2228:left of'.statusLabel'必须具有类/结构/union@Vektor好的,您的AutoFish
类中是否有ui
字段?您可以在问题中发布该类的声明吗?它发布在t中他问了一个问题。再次感谢。ui中有statusLabel吗?现在我得到以下错误:C2352:“AutoFish::updatelabel”:非法调用非静态成员函数。下面是我在另一个函数中调用updatelabel函数的代码:AutoFish::updatelabel(“错误:找不到窗口”)有趣。显然,ui
是一个指针,所以你需要使用->
访问它。尝试在ui->statusLabel->setText(“Hello”);
的ui->setupUi(this);
行下面添加ui。你能发布包含此类声明的头文件吗?@Nemanja发布了!希望一切都好了!
HWND window = getWindow(this, "FFXIVLauncher");
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;
}