Qt之Gui

打印 上一主题 下一主题

主题 1814|帖子 1814|积分 5442

组件依赖关系


应用

     QApplication:widget对应的应用
QGuiApplication :gui对应的应用
QCoreApplication :无gui对应的应用
widget

     QPlatformIntegration:平台抽象
QPlatformWindow :平台 抽象窗口
windows平台

     在QWindowsIntegration创建createPlatformWindow时,其先创建QWindowsWindowData::create
  1. QWindowsWindowData
  2.     WindowCreationData::create(const QWindow *w, const WindowData &data, QString title) const
  3. {
  4.     WindowData result;
  5.     result.flags = flags;
  6.     const auto appinst = reinterpret_cast<HINSTANCE>(GetModuleHandle(nullptr));
  7.     const QString windowClassName = QWindowsContext::instance()->registerWindowClass(w);
  8.     const QScreen *screen{};
  9.     const QRect rect = QPlatformWindow::initialGeometry(w, data.geometry,
  10.                                                         defaultWindowWidth, defaultWindowHeight,
  11.                                                         &screen);
  12.     if (title.isEmpty() && (result.flags & Qt::WindowTitleHint))
  13.         title = topLevel ? qAppName() : w->objectName();
  14.     const auto *titleUtf16 = reinterpret_cast<const wchar_t *>(title.utf16());
  15.     const auto *classNameUtf16 = reinterpret_cast<const wchar_t *>(windowClassName.utf16());
  16.     // Capture events before CreateWindowEx() returns. The context is cleared in
  17.     // the QWindowsWindow constructor.
  18.     const QWindowCreationContextPtr context(new QWindowCreationContext(w, screen, data.geometry,
  19.                                                                        rect, data.customMargins,
  20.                                                                        style, exStyle));
  21.     QWindowsContext::instance()->setWindowCreationContext(context);
  22.     const bool hasFrame = (style & (WS_DLGFRAME | WS_THICKFRAME));
  23.     QMargins invMargins = topLevel && hasFrame && QWindowsGeometryHint::positionIncludesFrame(w)
  24.             ? invisibleMargins(QPoint(context->frameX, context->frameY)) : QMargins();
  25.     qCDebug(lcQpaWindows).nospace()
  26.         << "CreateWindowEx: " << w << " class=" << windowClassName << " title=" << title
  27.         << '\n' << *this << "\nrequested: " << rect << ": "
  28.         << context->frameWidth << 'x' <<  context->frameHeight
  29.         << '+' << context->frameX << '+' << context->frameY
  30.         << " custom margins: " << context->customMargins
  31.         << " invisible margins: " << invMargins;
  32.     QPoint pos = calcPosition(w, context, invMargins);
  33.     // Mirror the position when creating on a parent in RTL mode, ditto for the obtained geometry.
  34.     int mirrorParentWidth = 0;
  35.     if (!w->isTopLevel() && QWindowsBaseWindow::isRtlLayout(parentHandle)) {
  36.         RECT rect;
  37.         GetClientRect(parentHandle, &rect);
  38.         mirrorParentWidth = rect.right;
  39.     }
  40.     if (mirrorParentWidth != 0 && pos.x() != CW_USEDEFAULT && context->frameWidth != CW_USEDEFAULT)
  41.         pos.setX(mirrorParentWidth - context->frameWidth - pos.x());
  42.     result.hwnd = CreateWindowEx(exStyle, classNameUtf16, titleUtf16,
  43.                                  style,
  44.                                  pos.x(), pos.y(),
  45.                                  context->frameWidth, context->frameHeight,
  46.                                  parentHandle, nullptr, appinst, nullptr);
  47.     qCDebug(lcQpaWindows).nospace()
  48.         << "CreateWindowEx: returns " << w << ' ' << result.hwnd << " obtained geometry: "
  49.         << context->obtainedPos << context->obtainedSize << ' ' << context->margins;
  50.     if (!result.hwnd) {
  51.         qErrnoWarning("%s: CreateWindowEx failed", __FUNCTION__);
  52.         return result;
  53.     }
  54.     if (mirrorParentWidth != 0) {
  55.         context->obtainedPos.setX(mirrorParentWidth - context->obtainedSize.width()
  56.                                   -  context->obtainedPos.x());
  57.     }
  58.     QRect obtainedGeometry(context->obtainedPos, context->obtainedSize);
  59.     result.geometry = obtainedGeometry;
  60.     result.fullFrameMargins = context->margins;
  61.     result.embedded = embedded;
  62.     result.hasFrame = hasFrame;
  63.     result.customMargins = context->customMargins;
  64.     return result;
  65. }
复制代码
内部会先注册registerWindowClass,设置窗口的处理函数registerWindowClass(cname, qWindowsWndProc, style, GetSysColorBrush(COLOR_WINDOW), icon);
qWindowsWndProc处理函数重要是调用 QWindowsContext的windowsProc
  1. QString QWindowsContext::registerWindowClass(QString cname,
  2.                                              WNDPROC proc,
  3.                                              unsigned style,
  4.                                              HBRUSH brush,
  5.                                              bool icon)
  6. {
  7.     // since multiple Qt versions can be used in one process
  8.     // each one has to have window class names with a unique name
  9.     // The first instance gets the unmodified name; if the class
  10.     // has already been registered by another instance of Qt then
  11.     // add a UUID. The check needs to be performed for each name
  12.     // in case new message windows are added (QTBUG-81347).
  13.     const auto appInstance = static_cast<HINSTANCE>(GetModuleHandle(nullptr));
  14.     WNDCLASS wcinfo;
  15.     const bool classExists = GetClassInfo(appInstance, reinterpret_cast<LPCWSTR>(cname.utf16()), &wcinfo) == TRUE
  16.         && wcinfo.lpfnWndProc != proc;
  17.     if (classExists)
  18.         cname += QUuid::createUuid().toString();
  19.     if (d->m_registeredWindowClassNames.contains(cname))        // already registered in our list
  20.         return cname;
  21.     WNDCLASSEX wc;
  22.     wc.cbSize       = sizeof(WNDCLASSEX);
  23.     wc.style        = style;
  24.     wc.lpfnWndProc  = proc;
  25.     wc.cbClsExtra   = 0;
  26.     wc.cbWndExtra   = 0;
  27.     wc.hInstance    = appInstance;
  28.     wc.hCursor      = nullptr;
  29.     wc.hbrBackground = brush;
  30.     if (icon) {
  31.         wc.hIcon = static_cast<HICON>(LoadImage(appInstance, L"IDI_ICON1", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE));
  32.         if (wc.hIcon) {
  33.             int sw = GetSystemMetrics(SM_CXSMICON);
  34.             int sh = GetSystemMetrics(SM_CYSMICON);
  35.             wc.hIconSm = static_cast<HICON>(LoadImage(appInstance, L"IDI_ICON1", IMAGE_ICON, sw, sh, 0));
  36.         } else {
  37.             wc.hIcon = static_cast<HICON>(LoadImage(nullptr, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED));
  38.             wc.hIconSm = nullptr;
  39.         }
  40.     } else {
  41.         wc.hIcon    = nullptr;
  42.         wc.hIconSm  = nullptr;
  43.     }
  44.     wc.lpszMenuName  = nullptr;
  45.     wc.lpszClassName = reinterpret_cast<LPCWSTR>(cname.utf16());
  46.     ATOM atom = RegisterClassEx(&wc);
  47.     if (!atom)
  48.         qErrnoWarning("QApplication::regClass: Registering window class '%s' failed.",
  49.                       qPrintable(cname));
  50.     d->m_registeredWindowClassNames.insert(cname);
  51.     qCDebug(lcQpaWindows).nospace() << __FUNCTION__ << ' ' << cname
  52.         << " style=0x" << Qt::hex << style << Qt::dec
  53.         << " brush=" << brush << " icon=" << icon << " atom=" << atom;
  54.     return cname;
  55. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

商道如狼道

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表