将QSizeGrip添加到QLabel的角落 [英] Adding a QSizeGrip to the corner of a QLabel

查看:94
本文介绍了将QSizeGrip添加到QLabel的角落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个由文本显示组成的小部件,用户可以抓住右下角来调整其大小.到目前为止,我已经能够生成这个:

I'm attempting to produce a widget that consists of a text display that can be resized by the user grabbing the lower right corner. So far I've been able to generate this:

我在布局上应用了红色背景,以使事情变得更加明显.我使用以下代码生成了此代码:

I've applied a red background to the layout to make it more obvious what's going on. I've used the following code to generate this:

  m_sizeGrip = new QSizeGrip( this );
  m_layout = new QHBoxLayout( this );
  m_label = new QLabel( this );

  m_layout->setContentsMargins( QMargins() );
  m_layout->setSpacing( 0 );
  m_layout->addWidget( m_label );
  m_layout->addWidget( m_sizeGrip, 0, Qt::AlignBottom | Qt::AlignRight );

  setWindowFlags( Qt::SubWindow );

基本上,它是一个水平布局,其中添加了标签和夹点,然后将其安装在QWidget上.我的问题是我希望夹点位于标签的右下角,而不是父窗口小部件.我还想在保持启用状态的同时使其不可见.

Basically, it's a horizontal layout with the label and grip added to it, which is then installed on a QWidget. My problem is that I'd like the grip to be on the lower right corner of the label, rather than the parent widget. I'd also like to make it invisible while keeping it enabled.

或者我可能会以错误的方式进行操作.我的最终目标是拥有一个文本显示窗口小部件,用户可以水平或垂直调整其大小,但不具有使文本模糊的可见抓地力.我是否已经在使用上面的代码的正确轨道上,或者有更好的方法来实现这一目标?

Or perhaps I'm going about this the wrong way. My ultimate goal is to have a textual display widget that can be resized by the user either horizontally or vertically, but doesn't have a visible grip that would obscure the text. Am I already on the right track with the code above, or is there a better way to achieve this?

推荐答案

您可以为此创建一个自定义QLabel.想法是跟踪鼠标移动事件(默认情况下仅在按下鼠标按钮时触发),并根据自上次事件以来鼠标移动的次数来调整大小.

You could create a custom QLabel for that. The idea would be to track mouse move events (which by default only fire when a mouse button is pressed), and resize based on how much the mouse traveled since the last event.

这使您可以精确控制如何显示夹具"(如果有的话)以及它应具有的形状.您可以将调整大小限制为垂直或水平(或不限制).

This allows you to control exactly how to display the "gripper" (if at all) and what shape it should have. You can constrain the resizing to vertical or horizontal (or not).

这是一个演示如何执行此操作的示例(同时调整两种方式的大小).警告:这可能会严重破坏您的布局.

Here's a demo of how you could do that (resizes both ways). Warning: this might wreak havoc in your layout.

#include <QtGui>

class GripLabel: public QLabel
{
    Q_OBJECT

    public:
        GripLabel(QString const& title, QWidget* parent = 0)
            : QLabel(title, parent),
              resizing(false),
              gripSize(10, 10)
        {
            // Prevent the widget from disappearing altogether
            // Bare minimum would be gripSize
            setMinimumSize(100, 30);
        }

        QSize sizeHint() const
        {
            return minimumSize();
        }

    protected:
        bool mouseInGrip(QPoint mousePos)
        {
            // "handle" is in the lower right hand corner
            return ((mousePos.x() > (width()  - gripSize.width()))
                &&  (mousePos.y() > (height() - gripSize.height())));
        }

        void mousePressEvent(QMouseEvent *e)
        {
            // Check if we hit the grip handle
            if (mouseInGrip(e->pos())) {
                oldPos = e->pos();
                resizing = true;
            } else {
                resizing = false;
            }
        }

        void mouseMoveEvent(QMouseEvent *e)
        {
            if (resizing) {
                // adapt the widget size based on mouse movement
                QPoint delta = e->pos() - oldPos;
                oldPos = e->pos();
                setMinimumSize(width()+delta.x(), height()+delta.y());
                updateGeometry();
            }
        }

        void paintEvent(QPaintEvent *e)
        {
            QLabel::paintEvent(e);
            QPainter p(this);
            p.setPen(Qt::red);
            p.drawRect(width()-gripSize.width(), height()-gripSize.height(),
                       gripSize.width(), gripSize.height());
        }

    private:
        bool resizing;
        QSize gripSize;
        QPoint oldPos;
};

主要样本:

#include "griplabel.h"

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QWidget *w = new QWidget;
    QPushButton *b = new QPushButton("button");
    GripLabel *l = new GripLabel("Hello");
    QHBoxLayout *y = new QHBoxLayout;
    y->addWidget(b);
    y->addWidget(l);
    y->setSizeConstraint(QLayout::SetFixedSize);
    w->setLayout(y);
    w->show();
    return app.exec();
}

这篇关于将QSizeGrip添加到QLabel的角落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆