2010-12-17 17 views
1

用自定義QT C++代碼顯示QML文件的最佳方式是什麼?我想沒有一個窗口邊框一樣用自定義QT代碼顯示QML文件(實現調整大小/移動功能)

的main.cpp

#include "stdafx.h" 
#include "myqmlapp.h" 
#include <QtGui/QApplication> 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    MyQMLApp w(NULL, Qt::CustomizeWindowHint | Qt::FramelessWindowHint); 
    w.show(); 
    return a.exec(); 
} 

myqmlapp.cpp

MyQMLApp::MyQMLApp(QWidget *parent, Qt::WFlags flags) 
    : QWidget(parent, flags), qmlView(this) 
{ 
    QApplication::instance()->connect(qmlView.engine(), SIGNAL(quit()), SLOT(quit())); 

    qmlView.setSource(QUrl("qrc:test1.qml")); 
    qmlView.show(); 

    ui.setupUi(this); 
} 

而且我的應用程序窗口中創建一個QWidget被這個小程序。所以唯一可見的是我的QML文件的輸出。但是這有一些問題。由於我沒有窗口邊框,我無法調整大小/移動。

如何實現與QML的窗口邊界?

回答

2

您可以手動編寫它們。 例如,捕獲鼠標事件,確定點擊區域,並使用它,就好像它是窗口標題或邊框一樣。 y座標低於30的所有座標都可以是「標題」區域,所有在小部件邊緣附近的5個像素內可以是「邊界」區域等。 之後,重新實現鼠標捕捉事件,如mouseMoveEvent,mouseClickEvent等來做你需要基於當前的鼠標區域。

移動窗口的代碼片段。

typedef enum WidgetRegion {HEADER_REGION, BORDER_REGION, ... } WidgetRegion; 

windowlessWidget::windowlessWidget(QWidget* parent):QWidget(parent) 
{ 
... 
setMouseTracking (true); 

} 

WidgetRegion windowlessWidget::calculateWindowRegion(QPoint mousePos) 
{ 
    ... 
    return region; 
} 
void windowlessWidget::mousePressEvent(QMouseEvent* event) 
{ 
    if(calculateWindowRegion(event->pos())==HEADER_REGION) 
    if(event->button() == Qt::LeftButton) 
    { 
     mMoving = true; 
     mLastMousePosition = event->globalPos(); 
    } 
} 

void windowlessWidget::mouseMoveEvent(QMouseEvent* event) 
{ 
    if(calculateWindowRegion(event->pos())==HEADER_REGION) 
    if(event->buttons().testFlag(Qt::LeftButton) && mMoving) 
    {         //offset 
     window()->move(window()->pos() + (event->globalPos() - mLastMousePosition)); 
     mLastMousePosition = event->globalPos(); 
    } 
} 

void windowlessWidget::mouseReleaseEvent(QMouseEvent* event) 
{ 
    if(calculateWindowRegion(event->pos())==HEADER_REGION) 
    if(event->button() == Qt::LeftButton) 
    { 
     mMoving = false; 
    } 
} 
相關問題