PDA

View Full Version : QTextEdit height using Qt3.3.5



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

vermarajeev
15th December 2006, 03:56
I have also tried this, but after line number 14 whatever I enter, the QTextEdit does not accept anything, But what is I want the user to delete some lines, move cursor up or down...


chemMainWindow::chemMainWindow(QWidget* parent, const char* name)
:QMainWindow(parent, name)
{
header = new ChemHeader(this);
footer = new ChemFooter(this);
setGeometry(80, 300, 300, 50);

_headerEditor = header->headerEditor();
_headerEditor->installEventFilter( this );

QPopupMenu * disable = new QPopupMenu( this );
menuBar()->insertItem( "&Disable", disable );
disable->insertItem( "&Disable", this, SLOT(slotDisable()), Key_F1 );

QPopupMenu * enable = new QPopupMenu( this );
menuBar()->insertItem( "&Enable", enable );
enable->insertItem( "&Ensable", this, SLOT(slotEnable()), Key_F2 );
}

bool chemMainWindow::eventFilter( QObject *obj, QEvent *ev )
{
if ( obj == _headerEditor )
{
if ( _headerEditor->lines() >= 14 && ev->type() == QEvent::KeyPress )
{
QKeyEvent *k = (QKeyEvent*)ev;
qDebug( "Ate key press %d", k->key() );
return TRUE;
} else
{
return FALSE;
}
}
else
{
// pass the event on to the parent class
return QMainWindow::eventFilter( obj, ev );
}
}

joseph
15th December 2006, 08:26
I could not fully catchup your problem. But if you need to avoid the scroll view
you can use like this




QTextEdit * m_textEdit = new QTextEdit ( .... );

m_textEdit-> setHScrollBarMode ( QScrollView::AlwaysOff ); //or use any mode as u prefer




else if you want more than the feature of a normal QTextEdit..
Why don't you create your own TextEdit ( subclass QTextEdit ).

vermarajeev
15th December 2006, 08:43
Thankx, I have got it...