[Interest] resizable QTextEdit

Igor Mironchik igor.mironchik at gmail.com
Fri Apr 13 10:22:14 CEST 2018


Hi,

Something like this?


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

class Text;

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

     virtual ~Corner()
     {
     }

     QSize sizeHint() const Q_DECL_OVERRIDE
     {
         return QSize( 10, 10 );
     }

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

         p.drawRect( rect() );
     }

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

         e->accept();
     }

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

         e->accept();
     }

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

             m_toResize->resize( m_toResize->size() + QSize( delta.x(), 
delta.y() ) );
         }

         e->accept();
     }

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

class Text
     :    public QTextEdit
{
public:
     Text( QWidget * parent )
         :    QTextEdit( parent )
         ,    m_corner( new Corner( this ) )
     {
         m_corner->move( width() - m_corner->width(),
             height() - m_corner->height() );
     }

     virtual ~Text()
     {
     }

protected:
     void resizeEvent( QResizeEvent * e ) Q_DECL_OVERRIDE
     {
         m_corner->move( width() - m_corner->width(),
             height() - m_corner->height() );

         e->accept();
     }

private:
     Corner * m_corner;
};

class Form
     :    public QWidget
{
public:
     Form()
     {
         QVBoxLayout * v = new QVBoxLayout( this );
         QHBoxLayout * h = new QHBoxLayout;
         v->addLayout( h );
         QLabel * label = new QLabel( "Resizeable Text Field", this );
         h->addWidget( label );
         Text * text = new Text( this );
         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 13.04.2018 10:34, Alexander Semke wrote:
> Hi,
>
> one frequently sees form text fields on the web where the actual text field can
> be resized by the user by clicking on the resize-symbol in the corner of the
> text field and dragging it until the desired size is achieved. I'm wondering
> how to get this for a QTextEdit  placed in a layout. Does somebody have an
> idea here?
>
>
> Thanks and Regards,
> Alexander
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest




More information about the Interest mailing list