[Interest] resizable QTextEdit
Igor Mironchik
igor.mironchik at gmail.com
Sun Apr 15 18:59:56 CEST 2018
Hi,
#include <QTextEdit>
#include <QHBoxLayout>
#include <QLabel>
#include <QWidget>
#include <QApplication>
#include <QPainter>
#include <QVBoxLayout>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QFrame>
class Resizable {
public:
Resizable()
{
}
virtual ~Resizable()
{
}
virtual void addSize( const QSize & size ) = 0;
protected:
QSize m_size;
};
class Corner
: public QWidget
{
public:
Corner( Resizable * resizable, QWidget * parent )
: QWidget( parent )
, m_toResize( resizable )
, m_pressed( false )
{
resize( sizeHint() );
}
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->addSize( QSize( delta.x(), delta.y() ) );
}
e->accept();
}
private:
Resizable * m_toResize;
QPoint m_pos;
bool m_pressed;
};
class Text
: public QTextEdit
, public Resizable
{
public:
Text( QWidget * parent )
: QTextEdit( parent )
, m_corner( new Corner( this, this ) )
, m_size( QTextEdit::sizeHint() )
{
m_corner->move( width() - m_corner->width(),
height() - m_corner->height() );
}
virtual ~Text()
{
}
void addSize( const QSize & size ) Q_DECL_OVERRIDE
{
setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
m_size += size;
updateGeometry();
}
QSize sizeHint() const Q_DECL_OVERRIDE
{
return m_size;
}
protected:
void resizeEvent( QResizeEvent * e ) Q_DECL_OVERRIDE
{
QTextEdit::resizeEvent( e );
m_corner->move( e->size().width() - m_corner->width(),
e->size().height() - m_corner->height() );
m_size = e->size();
e->accept();
}
private:
Corner * m_corner;
QSize m_size;
};
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( "Resizable Text Field", f );
label->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
h->addWidget( label );
Text * text = new Text( f );
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 19:30, Alexander Semke wrote:
> Hi Igor,
>
>> As a possible solution:
>> [...]
> Works now. I pushed a modified version of your code to our repository in
> https://cgit.kde.org/labplot.git/commit/?
> id=1cacf9f095b5e850baa58584b071986fbfe790fb
>
>
> The only problem now is that the entered text is not shown in TextEdit with
> this new class. The text changes are accepted, but nothing is shown. Any idea
> here?
>
>
> 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