PDA

View Full Version : hangup vertical scroll bar in qtextedit



mero
16th February 2011, 23:00
I've got textedit and i want display menu so i do:



void MainTextEdit::mousePressEvent(QMouseEvent *event)
{
if (parent->isActiveWindow() == false) QTextEdit::mousePressEvent(event); // prevent bug

if (event->button() == Qt::RightButton)
{
if (this->textCursor().selectedText().isEmpty() == true) // if nothing selected
{
QTextCursor cursor = cursorForPosition(event->pos());
cursor.select(QTextCursor::WordUnderCursor);
if (!cursor.selectedText().isEmpty())
{
QString strText = cursor.selectedText();
int iPos = cursor.position() - cursor.block().position(); // cursor.positionInBlock()

cursor.select(QTextCursor::BlockUnderCursor);
QString strBlock = cursor.selectedText().trimmed();
QStringList strlBlock = strBlock.split(" ");

QString strWord = strlBlock[1];

// channel
if (strText.at(0) == '#')
{
...
QMenu *menu = new QMenu(strChannel);
...
menu->popup(mapToGlobal(event->pos()));
}

// nick
if ((iPos > 11) && (iPos < 11+2+strWord.length()))
{
...
QMenu *menu = new QMenu(strNick);
...
menu->popup(mapToGlobal(event->pos()));
}
}
}
}
}

QTextEdit::mousePressEvent(event);
}


this code is working good but sometimes vertical scroll bar stops, and moving it to bottom by mouse don't work - because after adding next line of text it back to "saved"? position ...

maybe I forget to add something ?

mero
19th February 2011, 19:59
I change to contextMenuEvent:



void MainTextEdit::contextMenuEvent(QContextMenuEvent *event)
{
if (this->textCursor().selectedText().isEmpty() == true) // if nothing selected
{
QTextCursor cursor = cursorForPosition(event->pos());
cursor.select(QTextCursor::WordUnderCursor);
if (!cursor.selectedText().isEmpty())
{
QString strText = cursor.selectedText();
int iPos = cursor.position() - cursor.block().position(); // cursor.positionInBlock()

cursor.select(QTextCursor::BlockUnderCursor);
QString strBlock = cursor.selectedText().trimmed();
QStringList strlBlock = strBlock.split(" ");

QString strWord = strlBlock[1];

// channel
if (strText.at(0) == '#')
{
...
QMenu *menu = new QMenu(strChannel);
...
menu->popup(event->globalPos());
return;
}

// nick
if ((iPos > 11) && (iPos < 11+2+strWord.length()))
{
...
QMenu *menu = new QMenu(strNick);
...
menu->popup(event->globalPos());
return;
}
}
}

QTextEdit::contextMenuEvent(event);
}


but still sometimes vertical scroll bar stops ...