3

我使用唯一的名称“statusLabel”使用 Qt Creator 创建了一个标签。

然后我做了一个函数来更新这个状态标签,如下所示:

//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

我不确定我做错了什么,我只是想使用该功能更新标签文本。我应该使用标签以外的东西吗?我一直在研究插槽以创建一个事件来更新标签,但我发现的大多数插槽示例都涉及一个 pushButton 作为事件开始,这不是我所需要的。

谢谢你。

编辑:根据要求,这是我所有的源代码(不是很大): http: //pastebin.com/CfQXdzBK

4

1 回答 1

3

因为您的方法被声明为static,所以您不能直接访问非静态成员ui

改变

static void AutoFish::updatelabel(QString str);

void updatelabel(QString str);

在你的头文件中。

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.

于 2013-09-06T13:25:29.847 回答