2017-07-19 18 views
0

Qtにフレームレスウィジェットを作成する必要があります。サイズ変更が必要です。このアプリはWindowsとMac OSXで動作します。Qt MacOSXのフレームレスウィジェットをサイズ変更

私が使用している場合:setWindowFlags(Qt::FramelessWindowHint); 右下隅からウィンドウのサイズを変更できます(QSizeGripが表示されますが、これはQMainWindowウィジェットに含まれているようです)。

私は各コーナーにグリップを追加できますが、ウィンドウを側面から(またコーナーだけでなく)サイズ変更できるようにしたいと思います。通常のウィンドウのようにすべての面からサイズを変更できる簡単な方法はありますか?

WindowsのオーバーライドnativeEventハンドラ:Qt/Windows, resizable frameless windowで構成される回避策がありますが、Mac OSXプラットフォーム用の解決策が見つかりませんでした。

答えて

0

これを実行する最も良い方法は、マウスの移動イベントをキャッチし、現在の場所に適したカーソルを表示し、ボタンを押したままサイズを変更することです。独自のコンテンツを入れることができるFrameウィジェットを提供する例があります。

#include <QtWidgets> 

    class Frame : public QFrame 
    { 
    public: 

    Frame() 
    { 
    m_mouse_down = false; 
    setFrameShape(Panel); 

    // Make this a borderless window which can't 
    // be resized or moved via the window system 
    setWindowFlags(Qt::FramelessWindowHint); 
    setMouseTracking(true); 

    m_content = new QWidget(this); 

    QVBoxLayout *layout = new QVBoxLayout; 
    layout->addWidget(m_content); 
    layout->setMargin(5); 
    layout->setSpacing(0); 
    setLayout(layout); 
    } 

    // Allows you to access the content area of the frame 
    // where widgets and layouts can be added 
    QWidget *contentWidget() const { return m_content; } 

    void mousePressEvent(QMouseEvent *e) 
    { 
    m_old_pos = e->pos(); 
    m_mouse_down = e->button() == Qt::LeftButton; 
    } 

    void mouseMoveEvent(QMouseEvent *e) 
    { 
    int x = e->x(); 
    int y = e->y(); 

    if (m_mouse_down) { 
    int dx = x - m_old_pos.x(); 
    int dy = y - m_old_pos.y(); 

    QRect g = geometry(); 

    if (left) 
    g.setLeft(g.left() + dx); 
    if (right) 
    g.setRight(g.right() + dx); 
    if (bottom) 
    g.setBottom(g.bottom() + dy); 
    if (top) 
    g.setTop(g.top() + dy); 

    setGeometry(g); 

    m_old_pos = QPoint(!left ? e->x() : m_old_pos.x(), e->y()); 
    } else { 
    QRect r = rect(); 
    top = qAbs(y - r.top()) <= 5; 
    left = qAbs(x - r.left()) <= 5; 
    right = qAbs(x - r.right()) <= 5; 
    bottom = qAbs(y - r.bottom()) <= 5; 
    bool hor = left | right; 

    if (hor && bottom) { 
    if (left) 
    setCursor(Qt::SizeBDiagCursor); 
    else 
    setCursor(Qt::SizeFDiagCursor); 
    } else if (hor) { 
    setCursor(Qt::SizeHorCursor); 
    } else if (bottom || top) { 
    setCursor(Qt::SizeVerCursor); 
    } else { 
    setCursor(Qt::ArrowCursor); 
    } 
    } 
    } 

    void mouseReleaseEvent(QMouseEvent *e) 
    { 
    m_mouse_down = false; 
    } 

    private: 
    QWidget *m_content; 
    QPoint m_old_pos; 
    bool m_mouse_down; 
    bool left, right, bottom, top; 
    }; 


    #include "main.moc" 

    int main(int argc, char **argv) 
    { 
    QApplication app(argc, argv); 

    Frame box; 

    QVBoxLayout *l = new QVBoxLayout(box.contentWidget()); 
    l->setMargin(0); 
    QTextEdit *edit = new QTextEdit(box.contentWidget()); 
    l->addWidget(edit); 

    box.show(); 
    return app.exec(); 
    } 
関連する問題