I've got textedit and i want display menu so i do:
{
if (parent
->isActiveWindow
() == false) QTextEdit::mousePressEvent(event
);
// prevent bug
if (event->button() == Qt::RightButton)
{
if (this->textCursor().selectedText().isEmpty() == true) // if nothing selected
{
if (!cursor.selectedText().isEmpty())
{
QString strText
= cursor.
selectedText();
int iPos = cursor.position() - cursor.block().position(); // cursor.positionInBlock()
QString strBlock
= cursor.
selectedText().
trimmed();
// channel
if (strText.at(0) == '#')
{
...
...
menu->popup(mapToGlobal(event->pos()));
}
// nick
if ((iPos > 11) && (iPos < 11+2+strWord.length()))
{
...
...
menu->popup(mapToGlobal(event->pos()));
}
}
}
}
}
}
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);
}
To copy to clipboard, switch view to plain text mode
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 ?
Bookmarks