Results 1 to 4 of 4

Thread: QTextEdit height using Qt3.3.5

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Angry QTextEdit height using Qt3.3.5

    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

  2. #2
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit height using Qt3.3.5

    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...

    Qt Code:
    1. chemMainWindow::chemMainWindow(QWidget* parent, const char* name)
    2. :QMainWindow(parent, name)
    3. {
    4. header = new ChemHeader(this);
    5. footer = new ChemFooter(this);
    6. setGeometry(80, 300, 300, 50);
    7.  
    8. _headerEditor = header->headerEditor();
    9. _headerEditor->installEventFilter( this );
    10.  
    11. QPopupMenu * disable = new QPopupMenu( this );
    12. menuBar()->insertItem( "&Disable", disable );
    13. disable->insertItem( "&Disable", this, SLOT(slotDisable()), Key_F1 );
    14.  
    15. QPopupMenu * enable = new QPopupMenu( this );
    16. menuBar()->insertItem( "&Enable", enable );
    17. enable->insertItem( "&Ensable", this, SLOT(slotEnable()), Key_F2 );
    18. }
    19.  
    20. bool chemMainWindow::eventFilter( QObject *obj, QEvent *ev )
    21. {
    22. if ( obj == _headerEditor )
    23. {
    24. if ( _headerEditor->lines() >= 14 && ev->type() == QEvent::KeyPress )
    25. {
    26. QKeyEvent *k = (QKeyEvent*)ev;
    27. qDebug( "Ate key press %d", k->key() );
    28. return TRUE;
    29. } else
    30. {
    31. return FALSE;
    32. }
    33. }
    34. else
    35. {
    36. // pass the event on to the parent class
    37. return QMainWindow::eventFilter( obj, ev );
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Posts
    157
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QTextEdit height using Qt3.3.5

    I could not fully catchup your problem. But if you need to avoid the scroll view
    you can use like this

    Qt Code:
    1. QTextEdit * m_textEdit = new QTextEdit ( .... );
    2.  
    3. m_textEdit-> setHScrollBarMode ( QScrollView::AlwaysOff ); //or use any mode as u prefer
    To copy to clipboard, switch view to plain text mode 

    else if you want more than the feature of a normal QTextEdit..
    Why don't you create your own TextEdit ( subclass QTextEdit ).
    Last edited by jacek; 15th December 2006 at 09:47. Reason: changed [html] to [code]

  4. #4
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit height using Qt3.3.5

    Thankx, I have got it...

  5. The following user says thank you to vermarajeev for this useful post:

    joseph (15th December 2006)

Similar Threads

  1. Re-implement mouse events of QTextEdit
    By Ankitha Varsha in forum Qt Programming
    Replies: 2
    Last Post: 14th October 2006, 16:55
  2. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  3. height()
    By mickey in forum Newbie
    Replies: 1
    Last Post: 22nd March 2006, 20:32
  4. [QT 4] QTextEdit performance
    By fellobo in forum Qt Programming
    Replies: 8
    Last Post: 6th March 2006, 19:27
  5. Painting to QTextEdit
    By gesslar in forum Qt Programming
    Replies: 8
    Last Post: 18th February 2006, 18:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.