GetWindowText(hwnd, title.data(), title.size());form.cpp:24:9: No matching function for call to 'GetWindowTextW' winuser.h:3638:25: candidate function not viable: no known conversion from 'QChar *' to 'LPWSTR' (aka 'wchar_t *') for 2nd argument
时间: 2024-03-12 18:48:51 浏览: 18
这个错误的原因是 GetWindowText 函数需要的第二个参数是 wchar_t* 类型,而 QString::data() 函数返回的是 QChar* 类型。
解决这个问题的方法是将 QString 转换为 wchar_t* 类型。可以使用 QString::toStdWString() 函数将 QString 转换为 std::wstring 类型,然后使用 std::wstring::c_str() 函数将 std::wstring 转换为 wchar_t* 类型。
以下是修改后的代码示例:
```c++
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QTextEdit>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout* layout = new QVBoxLayout(this);
textEdit = new QTextEdit(this);
layout->addWidget(textEdit);
setLayout(layout);
QVector<HWND> windows;
EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(&windows));
for (auto hwnd : windows)
{
wchar_t title[1024];
GetWindowText(hwnd, title, sizeof(title));
textEdit->append(QString::fromWCharArray(title));
}
}
BOOL CALLBACK MainWindow::EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
QVector<HWND>* windows = reinterpret_cast<QVector<HWND>*>(lParam);
if (IsWindowVisible(hwnd))
{
windows->push_back(hwnd);
}
return TRUE;
}
```
在这个示例程序中,我们将获取窗口标题的部分修改为了使用 wchar_t 类型的 title 数组,并将其传递给 GetWindowText 函数。然后,我们使用 QString::fromWCharArray() 函数将 wchar_t* 类型的标题转换为 QString 类型并添加到 QTextEdit 控件中。