vermarajeev
15th December 2006, 03:08
Hi All,
This is a simple problem but I'm having tough time to solve this.
My Problem::::
I have a QTextEdit, where the user can input text in it. Now I want to fix the number of lines say by 14. ie. I need to fix the contents height for QTextEdit. There should not be a scrollView and its contents should not move up/down. I have tried out the solution but is unable to fix the contents height
Here is what I have done
//header.cpp
ChemHeader::ChemHeader(QWidget* parent, const char* name )
:QFrame ( parent, name )
{
setBackgroundColor(Qt::white);
_headerText = new QTextEdit(this);
_headerLabel = new QLabel(this);
_headerLabel->setText("Header");
_headerLabel->setBackgroundColor(Qt::white);
_headerText->setFocus();
QRect rec = frameRect();
QPoint p= rec.topLeft();
_headerLabel->setGeometry(p.x()+10, p.y(), 45, 10);
QRect labelRect = _headerLabel->geometry();
QPoint pp = labelRect.bottomLeft();
_headerText->setGeometry(p.x(), pp.y()+3, width(),height()-_headerLabel->height()-20);
_headerText->setHScrollBarMode(QScrollView::AlwaysOff);
_headerText->setWrapPolicy ( QTextEdit::AtWordOrDocumentBoundary );
connect(_headerText, SIGNAL(textChanged()), this, SLOT( slotHeaderTextChanged()));
_headerNoOfLines = 2;
_headerMaxHeight = parent->height()/3;
//set Maximum height
_headerText->setMaximumHeight(_headerMaxHeight);
}
ChemHeader::~ChemHeader()
{}
//_headerText is a QTextEdit
void ChemHeader::slotHeaderTextChanged()
{
int noOfLines = _headerText->lines();
QFont f = _headerText->currentFont();
QFontMetrics fm(f);
int ht = fm.height();
if(noOfLines > _headerNoOfLines)
{
#ifdef _WIN32
resize(width(), height()+(ht) );
#else
resize(width(), height()+(ht+1) );
#endif
_headerText->resize(width(), height() -_headerLabel->height()-2);
_headerNoOfLines = noOfLines;
}
else if( _headerNoOfLines > 2 && noOfLines < _headerNoOfLines)
{
#ifdef _WIN32
resize(width(), height()-(ht) );
#else
resize(width(), height()-(ht+1) );
#endif
_headerText->resize(width(), height()-_headerLabel->height()-2);
_headerNoOfLines--;
}
}
void ChemHeader::resizeEvent(QResizeEvent*)
{
if(height() > _headerMaxHeight)
{
}
}
Any suggestions will be highly appreciated
Thankx
This is a simple problem but I'm having tough time to solve this.
My Problem::::
I have a QTextEdit, where the user can input text in it. Now I want to fix the number of lines say by 14. ie. I need to fix the contents height for QTextEdit. There should not be a scrollView and its contents should not move up/down. I have tried out the solution but is unable to fix the contents height
Here is what I have done
//header.cpp
ChemHeader::ChemHeader(QWidget* parent, const char* name )
:QFrame ( parent, name )
{
setBackgroundColor(Qt::white);
_headerText = new QTextEdit(this);
_headerLabel = new QLabel(this);
_headerLabel->setText("Header");
_headerLabel->setBackgroundColor(Qt::white);
_headerText->setFocus();
QRect rec = frameRect();
QPoint p= rec.topLeft();
_headerLabel->setGeometry(p.x()+10, p.y(), 45, 10);
QRect labelRect = _headerLabel->geometry();
QPoint pp = labelRect.bottomLeft();
_headerText->setGeometry(p.x(), pp.y()+3, width(),height()-_headerLabel->height()-20);
_headerText->setHScrollBarMode(QScrollView::AlwaysOff);
_headerText->setWrapPolicy ( QTextEdit::AtWordOrDocumentBoundary );
connect(_headerText, SIGNAL(textChanged()), this, SLOT( slotHeaderTextChanged()));
_headerNoOfLines = 2;
_headerMaxHeight = parent->height()/3;
//set Maximum height
_headerText->setMaximumHeight(_headerMaxHeight);
}
ChemHeader::~ChemHeader()
{}
//_headerText is a QTextEdit
void ChemHeader::slotHeaderTextChanged()
{
int noOfLines = _headerText->lines();
QFont f = _headerText->currentFont();
QFontMetrics fm(f);
int ht = fm.height();
if(noOfLines > _headerNoOfLines)
{
#ifdef _WIN32
resize(width(), height()+(ht) );
#else
resize(width(), height()+(ht+1) );
#endif
_headerText->resize(width(), height() -_headerLabel->height()-2);
_headerNoOfLines = noOfLines;
}
else if( _headerNoOfLines > 2 && noOfLines < _headerNoOfLines)
{
#ifdef _WIN32
resize(width(), height()-(ht) );
#else
resize(width(), height()-(ht+1) );
#endif
_headerText->resize(width(), height()-_headerLabel->height()-2);
_headerNoOfLines--;
}
}
void ChemHeader::resizeEvent(QResizeEvent*)
{
if(height() > _headerMaxHeight)
{
}
}
Any suggestions will be highly appreciated
Thankx