【问题标题】:Qt resize frameless widget on MacOSXQt 在 MacOSX 上调整无框小部件的大小
【发布时间】:2017-07-19 10:49:02
【问题描述】:

我需要在 Qt 中创建一个无框架小部件,并且它需要可调整大小。该应用程序将在 Windows 和 Mac OSX 下运行。

如果我使用:setWindowFlags(Qt::FramelessWindowHint); 我可以从右下角调整窗口大小(出现一个 QSizeGrip,我猜它包含在 QMainWindow 小部件中)。

我可以在每个角落添加夹点,但我希望窗口可以从侧面调整大小(而不仅仅是角落)。有什么简单的方法可以让它像普通窗口一样从各个方向调整大小?

有一个适用于 Windows 的解决方法,包括覆盖 nativeEvent 处理程序:Qt/Windows, resizable frameless window,但我找不到适用于 Mac OSX 平台的解决方案。

【问题讨论】:

    标签: macos qt resize widget borderless


    【解决方案1】:

    最好的方法是捕捉鼠标移动事件并显示适合您所在位置的光标,并在按住按钮时调整大小。有一个示例提供了一个 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();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多