Qt-interest Archive, October 2007
A maximal line number in QTextEdit
Message 1 in thread
Hi!
I have a text editor (QTextEdit). The length of the text line is limited /
text wraped (QTextEdit::FixedPixelWidth).
Is there any possibility to limit the number of the input text lines (so by
example allow the user to type in 10 text lines, but no more)?
I use QT 4.3.2.
Thank you!
--
[ signature omitted ]
Message 2 in thread
I'm not sure this is quite what you want, but it sort of does the trick.
(Type in the front-most text edit -- it resizes accordingly and information about it is displayed in the other text edit.)
You might be able to do something better by catching the text change before it's done -- not sure how.
You'll need to adjust the handleTextChange() slot to do what you want, which may not be undo().
Sam
////////////////////////////////// main.cpp
#include "test.h"
int main(int argc, char **argv)
{
QApplication application(argc, argv);
QTextEdit *textEdit = new TextEdit();
textEdit->show();
return application.exec();
}
////////////////////////////////// end main.cpp
////////////////////////////////// test.cpp
#include "test.h"
TextEdit::TextEdit():
QTextEdit(),
_lineSpacing(fontMetrics().lineSpacing())
{
doConnections();
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
setContentsMargins(5, 12, 5, 5);
setMinimumSize(200, document()->size().height() + fontMetrics().lineSpacing() / 2);
_debug = new QTextEdit();
_debug->show();
_maxLines = 5;
}
bool TextEdit::cursorInFirstnumLines()
{
return cursorRect().y() < _lineSpacing;
}
bool TextEdit::cursorInLastnumLines()
{
_debug->setText("height: " + QString::number(document()->size().height()) +
"\ny: " + QString::number(cursorRect().y()) +
"\n_lineSpacing: " + QString::number(_lineSpacing));
return cursorRect().y() + 2 * _lineSpacing > document()->size().height();
}
void TextEdit::doConnections()
{
connect(this, SIGNAL(cursorPositionChanged()), SLOT(showCursorPosition()));
connect(this, SIGNAL(textChanged()), SLOT(handleTextChange()));
}
void TextEdit::handleTextChange()
{
if (numLines() > _maxLines)
document()->undo();
}
int TextEdit::numLines()
{
return document()->size().height() / fontMetrics().lineSpacing();
}
void TextEdit::showCursorPosition()
{
_debug->clear();
_debug->setText(
"page size height: " + QString::number(document()->size().height()) + "\n" +
"contentsRect height: " + QString::number(contentsRect().height()) + "\n" +
"contentsRect width: " + QString::number(contentsRect().width()) + "\n" +
"widget height: " + QString::number(height()) + "\n" +
"font height: " + QString::number(fontMetrics().height()) + "\n" +
"font leading: " + QString::number(fontMetrics().leading()) + "\n" +
"font line spacing: " + QString::number(fontMetrics().lineSpacing()) + "\n" +
"number of lines: " + QString::number(numLines()) + "\n" +
"x: " + QString::number(cursorRect().x()) + "\n" +
"y: " + QString::number(cursorRect().y()));
adjustSize();
}
QSize TextEdit::sizeHint() const
{
return QSize(200, document()->size().height() + fontMetrics().lineSpacing() / 2);
}
////////////////////////////////// end test.cpp
////////////////////////////////// test.h
#ifndef TEST_H
#define TEST_H
#include <qapplication>
#include <qtextedit>
class TextEdit: public QTextEdit
{
Q_OBJECT
public:
TextEdit();
int numLines();
protected:
virtual QSize sizeHint() const;
private:
bool cursorInFirstnumLines();
bool cursorInLastnumLines();
void doConnections();
int
_lineSpacing,
_maxLines;
QTextEdit
*_debug;
private slots:
void handleTextChange();
void showCursorPosition();
};
#endif
////////////////////////////////// end test.h
SAM DUTTON
SENIOR SITE DEVELOPER
200 GRAY'S INN ROAD
LONDON
WC1X 8XZ
UNITED KINGDOM
T +44 (0)20 7430 4496
F
E Sam.Dutton@xxxxxxxxx
WWW.ITN.CO.UK
P Please consider the environment. Do you really need to print this email?
-----Original Message-----
From: Andi [mailto:b_andi@xxxxxxxxxx]
Sent: Wednesday 24 October 2007 10:27
To: qt-interest@xxxxxxxxxxxxx
Subject: A maximal line number in QTextEdit
Hi!
I have a text editor (QTextEdit). The length of the text line is limited / text wraped (QTextEdit::FixedPixelWidth).
Is there any possibility to limit the number of the input text lines (so by example allow the user to type in 10 text lines, but no more)?
I use QT 4.3.2.
Thank you!
--
[ signature omitted ]
Message 3 in thread
Hi!
Thanks for your help. This is somehow the objective I want to reach, so at
the end of the 10-th line of the input text just say "Stop".
The QTextDocument emits its contentsChanged (int, int, int) signal just
before the document's layout manager is informed about the change.
So it can be used instead of textChanged() signal.
I tried to resolve this problem by making use of QTextCursor and QTextBlock.
But somehow, the the cursor returns always 0, as the number of the current
block.
Andi
"Dutton, Sam" <Sam.Dutton@xxxxxxxxx> schrieb im Newsbeitrag
news:DFFEE4C74F4F7C469FCEFF12FC55A80DB0F730@xxxxxxxxxxxxxxxx
I'm not sure this is quite what you want, but it sort of does the trick.
(Type in the front-most text edit -- it resizes accordingly and information
about it is displayed in the other text edit.)
You might be able to do something better by catching the text change before
it's done -- not sure how.
You'll need to adjust the handleTextChange() slot to do what you want, which
may not be undo().
Sam
////////////////////////////////// main.cpp
#include "test.h"
int main(int argc, char **argv)
{
QApplication application(argc, argv);
QTextEdit *textEdit = new TextEdit();
textEdit->show();
return application.exec();
}
////////////////////////////////// end main.cpp
////////////////////////////////// test.cpp
#include "test.h"
TextEdit::TextEdit():
QTextEdit(),
_lineSpacing(fontMetrics().lineSpacing())
{
doConnections();
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
setContentsMargins(5, 12, 5, 5);
setMinimumSize(200, document()->size().height() +
fontMetrics().lineSpacing() / 2);
_debug = new QTextEdit();
_debug->show();
_maxLines = 5;
}
bool TextEdit::cursorInFirstnumLines()
{
return cursorRect().y() < _lineSpacing;
}
bool TextEdit::cursorInLastnumLines()
{
_debug->setText("height: " + QString::number(document()->size().height()) +
"\ny: " + QString::number(cursorRect().y()) +
"\n_lineSpacing: " + QString::number(_lineSpacing));
return cursorRect().y() + 2 * _lineSpacing > document()->size().height();
}
void TextEdit::doConnections()
{
connect(this, SIGNAL(cursorPositionChanged()), SLOT(showCursorPosition()));
connect(this, SIGNAL(textChanged()), SLOT(handleTextChange()));
}
void TextEdit::handleTextChange()
{
if (numLines() > _maxLines)
document()->undo();
}
int TextEdit::numLines()
{
return document()->size().height() / fontMetrics().lineSpacing();
}
void TextEdit::showCursorPosition()
{
_debug->clear();
_debug->setText(
"page size height: " + QString::number(document()->size().height()) + "\n" +
"contentsRect height: " + QString::number(contentsRect().height()) + "\n" +
"contentsRect width: " + QString::number(contentsRect().width()) + "\n" +
"widget height: " + QString::number(height()) + "\n" +
"font height: " + QString::number(fontMetrics().height()) + "\n" +
"font leading: " + QString::number(fontMetrics().leading()) + "\n" +
"font line spacing: " + QString::number(fontMetrics().lineSpacing()) + "\n"
+
"number of lines: " + QString::number(numLines()) + "\n" +
"x: " + QString::number(cursorRect().x()) + "\n" +
"y: " + QString::number(cursorRect().y()));
adjustSize();
}
QSize TextEdit::sizeHint() const
{
return QSize(200, document()->size().height() + fontMetrics().lineSpacing()
/ 2);
}
////////////////////////////////// end test.cpp
////////////////////////////////// test.h
#ifndef TEST_H
#define TEST_H
#include <qapplication>
#include <qtextedit>
class TextEdit: public QTextEdit
{
Q_OBJECT
public:
TextEdit();
int numLines();
protected:
virtual QSize sizeHint() const;
private:
bool cursorInFirstnumLines();
bool cursorInLastnumLines();
void doConnections();
int
_lineSpacing,
_maxLines;
QTextEdit
*_debug;
private slots:
void handleTextChange();
void showCursorPosition();
};
#endif
////////////////////////////////// end test.h
SAM DUTTON
SENIOR SITE DEVELOPER
200 GRAY'S INN ROAD
LONDON
WC1X 8XZ
UNITED KINGDOM
T +44 (0)20 7430 4496
F
E Sam.Dutton@xxxxxxxxx
WWW.ITN.CO.UK
P Please consider the environment. Do you really need to print this email?
-----Original Message-----
From: Andi [mailto:b_andi@xxxxxxxxxx]
Sent: Wednesday 24 October 2007 10:27
To: qt-interest@xxxxxxxxxxxxx
Subject: A maximal line number in QTextEdit
Hi!
I have a text editor (QTextEdit). The length of the text line is limited /
text wraped (QTextEdit::FixedPixelWidth).
Is there any possibility to limit the number of the input text lines (so by
example allow the user to type in 10 text lines, but no more)?
I use QT 4.3.2.
Thank you!
--
[ signature omitted ]