Hi again,

If you want to be able to shrink the width too, you can do something similar to:


#include <QTextEdit>
#include <QHBoxLayout>
#include <QLabel>
#include <QWidget>
#include <QApplication>
#include <QPainter>
#include <QVBoxLayout>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QFrame>


class Text;

class Corner
    :    public QWidget
{
public:
    Corner( Text * parent );

    virtual ~Corner();

    QSize sizeHint() const Q_DECL_OVERRIDE;

protected:
    void paintEvent( QPaintEvent * ) Q_DECL_OVERRIDE;

    void mousePressEvent( QMouseEvent * e ) Q_DECL_OVERRIDE;

    void mouseReleaseEvent( QMouseEvent * e ) Q_DECL_OVERRIDE;

    void mouseMoveEvent( QMouseEvent * e ) Q_DECL_OVERRIDE;

private:
    Text * m_toResize;
    QPoint m_pos;
    bool m_pressed;
};

class Text
    :    public QTextEdit
{
public:
    Text( QWidget * parent, QLayout * layout );

    virtual ~Text();

    void addSize( const QSize & size );

    QSize sizeHint() const Q_DECL_OVERRIDE;

protected:
    void resizeEvent( QResizeEvent * e ) Q_DECL_OVERRIDE;

private:
    Corner * m_corner;
    QSize m_size;
    QLayout * m_layout;
};

Corner::Corner( Text * parent )
    :    QWidget( parent )
    ,    m_toResize( parent )
    ,    m_pressed( false )
{
}

Corner::~Corner()
{
}

QSize
Corner::sizeHint() const
{
    return QSize( 10, 10 );
}

void
Corner::paintEvent( QPaintEvent * )
{
    QPainter p( this );
    p.setPen( Qt::red );
    p.setBrush( Qt::red );

    p.drawRect( rect() );
}

void
Corner::mousePressEvent( QMouseEvent * e )
{
    if( e->button() == Qt::LeftButton )
    {
        m_pos = e->pos();
        m_pressed = true;
    }

    e->accept();
}

void
Corner::mouseReleaseEvent( QMouseEvent * e )
{
    if( e->button() == Qt::LeftButton )
        m_pressed = false;

    e->accept();
}

void
Corner::mouseMoveEvent( QMouseEvent * e )
{
    if( m_pressed )
    {
        const QPoint delta = e->pos() - m_pos;

        m_toResize->addSize( QSize( delta.x(), delta.y() ) );
    }

    e->accept();
}

Text::Text( QWidget * parent, QLayout * layout )
    :    QTextEdit( parent )
    ,    m_corner( new Corner( this ) )
    ,    m_size( QTextEdit::sizeHint() )
    ,    m_layout( layout )
{
    m_corner->move( width() - m_corner->width(),
        height() - m_corner->height() );
}

Text::~Text()
{
}

void
Text::addSize( const QSize & size )
{
    setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );

    m_size = QSize( m_size.width() + size.width(), m_size.height() + size.height() );

    updateGeometry();
}

QSize
Text::sizeHint() const
{
    return m_size;
}

void
Text::resizeEvent( QResizeEvent * e )
{
    m_corner->move( e->size().width() - m_corner->width(),
        e->size().height() - m_corner->height() );

    m_size = e->size();

    e->accept();
}


class Form
    :    public QWidget
{
public:
    Form()
    {
        QVBoxLayout * v = new QVBoxLayout( this );
        QFrame * f = new QFrame( this );
        f->setFrameStyle( QFrame::Panel | QFrame::Sunken );
        QHBoxLayout * h = new QHBoxLayout( f );
        v->addWidget( f );
        QLabel * label = new QLabel( "Resizeable Text Field", f );
        label->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
        h->addWidget( label );
        Text * text = new Text( f, v );
        h->addWidget( text );
        QSpacerItem * s = new QSpacerItem( 10, 10, QSizePolicy::Fixed,
            QSizePolicy::Expanding );
        v->addItem( s );

        resize( 800, 800 );
    }

    virtual ~Form()
    {
    }
};

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

    Form f;
    f.show();

    return app.exec();
}


On 15.04.2018 11:53, Alexander Semke wrote:
On Freitag, 13. April 2018 10:22:14 CEST Igor Mironchik wrote:
Something like this?
[...]
Thanks Igor, I've got the idea. The resize works fine now but I have the
problem with the layout of the parent widget being not updated. My text edit
widget is embedded into a QFrame with a vertical layout. After the manual
resize I need the layout(s) and the sizes/positions of this frame and of all
the other widgets frame's parent to be adjusted to the new size of the text
edit.

https://imgur.com/a/eV2Fj - here the height of the text edit widget was made
smaller but the QFrame widget where the text edit is placed in a vertical
layout was not resized. I'm calling frame->layout()->activate() to trigger the
recalculation but this doesn't lead to the desired effect...  Do I new to
resize the frame widget here manually or are there any other methods to
update/recalculate the layout?



_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to