2

カスタムQTC++コードを使用してQMLファイルを表示する最良の方法は何ですか?次のようなウィンドウの境界線のないQWidgetを作成してみました

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);
}

そして、私のアプリケーションウィンドウはこのウィジェットです。したがって、表示されるのはQMLファイルの出力だけです。しかし、これにはいくつかの問題があります。ウィンドウの境界線がないため、サイズ変更/移動できません。

QMLでウィンドウの境界線を実装するにはどうすればよいですか?

4

1 に答える 1

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;
    }
}
于 2010-12-17T16:17:08.810 に答える