Qt 句柄


在Windows下:
Qt3: 通过QWidget::handle()方法获取得窗体句柄,但这个函数到了Qt4后被封装起来了。
Qt4: 通过WId QWidget::winId () const 成员函数获取窗口句柄(WId就是HWND的typedef)。
Qt5:通过WId QWidget::winId () const

setWindowFlags(Qt::FramelessWindowHint); 再调用winId(), 窗体最小化后恢复,往往不响应,这时需要重写showEvent
void showEvent(QShowEvent *event) {
    this->setAttribute(Qt::WA_Mapped);
    QWidget::showEvent(event);
}

 


句柄直接传递给VC库,往往达不到效果,要使用下面的转换
1 HWND temp = static_cast<HWND>((void*)playWidget->winId());



 1 #include <QtGui/5.4.1/QtGui/qpa/qplatformnativeinterface.h>
 2 QWindow *windowForWidget(const QWidget *widget)
 3 {
 4     QWindow *window = widget->windowHandle();
 5     if (window) {
 6         return window;
 7     }
 8     
 9     const QWidget *nativeParent = widget->nativeParentWidget();
10     if (nativeParent) {
11         return nativeParent->windowHandle();
12     }
13     
14     return NULL;
15 }
16 HWND getHWNDForWidget(const QWidget *widget)
17 {
18     QWindow *window = ::windowForWidget(widget);
19     if (window)
20     {
21         QPlatformNativeInterface *interfacep = QGuiApplication::platformNativeInterface();
22         return static_cast<HWND>(interfacep->nativeResourceForWindow(QByteArrayLiteral("handle"), window));
23     }
24     
25     return NULL;
26 }

 



posted @ 2015-03-11 17:11  菩提树~今生  阅读(4180)  评论(0)    收藏  举报