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

Qt Code:
  1. //header.cpp
  2. ChemHeader::ChemHeader(QWidget* parent, const char* name )
  3. :QFrame ( parent, name )
  4. {
  5. setBackgroundColor(Qt::white);
  6. _headerText = new QTextEdit(this);
  7.  
  8. _headerLabel = new QLabel(this);
  9. _headerLabel->setText("Header");
  10. _headerLabel->setBackgroundColor(Qt::white);
  11. _headerText->setFocus();
  12. QRect rec = frameRect();
  13. QPoint p= rec.topLeft();
  14. _headerLabel->setGeometry(p.x()+10, p.y(), 45, 10);
  15.  
  16. QRect labelRect = _headerLabel->geometry();
  17. QPoint pp = labelRect.bottomLeft();
  18.  
  19. _headerText->setGeometry(p.x(), pp.y()+3, width(),height()-_headerLabel->height()-20);
  20.  
  21. _headerText->setHScrollBarMode(QScrollView::AlwaysOff);
  22. _headerText->setWrapPolicy ( QTextEdit::AtWordOrDocumentBoundary );
  23. connect(_headerText, SIGNAL(textChanged()), this, SLOT( slotHeaderTextChanged()));
  24.  
  25. _headerNoOfLines = 2;
  26. _headerMaxHeight = parent->height()/3;
  27. //set Maximum height
  28. _headerText->setMaximumHeight(_headerMaxHeight);
  29. }
  30.  
  31. ChemHeader::~ChemHeader()
  32. {}
  33.  
  34. //_headerText is a QTextEdit
  35. void ChemHeader::slotHeaderTextChanged()
  36. {
  37. int noOfLines = _headerText->lines();
  38.  
  39. QFont f = _headerText->currentFont();
  40. QFontMetrics fm(f);
  41. int ht = fm.height();
  42. if(noOfLines > _headerNoOfLines)
  43. {
  44. #ifdef _WIN32
  45. resize(width(), height()+(ht) );
  46. #else
  47. resize(width(), height()+(ht+1) );
  48. #endif
  49. _headerText->resize(width(), height() -_headerLabel->height()-2);
  50. _headerNoOfLines = noOfLines;
  51. }
  52. else if( _headerNoOfLines > 2 && noOfLines < _headerNoOfLines)
  53. {
  54. #ifdef _WIN32
  55. resize(width(), height()-(ht) );
  56. #else
  57. resize(width(), height()-(ht+1) );
  58. #endif
  59. _headerText->resize(width(), height()-_headerLabel->height()-2);
  60. _headerNoOfLines--;
  61. }
  62. }
  63.  
  64. void ChemHeader::resizeEvent(QResizeEvent*)
  65. {
  66. if(height() > _headerMaxHeight)
  67. {
  68.  
  69. }
  70. }
To copy to clipboard, switch view to plain text mode 



Any suggestions will be highly appreciated
Thankx