PDA

View Full Version : QTextEdit drag and drop



bunjee
6th February 2008, 23:46
Hey there trolltechers,

I'm using a QTextEdit dynaically allocated inside a widget:
- When the mouse enters: I create and place it.
- When the mouse leaves: I delete it.

Now I have glitches with the following:
- Contextual menu makes the app crash since the mouse leaves the parent widget:
solved it with : mTextBrowser->setContextMenuPolicy(Qt::NoContextMenu);

But :
- Drag and dropping makes the app crashes since the mouse leaves the parent too when dragging something.

I tried with deleteLater() and that doesn't solve a thing.
Any way to disable drag and dropping on a QTextEdit? Am I doing something wrong by creating deleting in the enter/leave event?

Thanks.

wysota
7th February 2008, 00:25
What exactly are you creating and deleting? The text edit widget? What is your purpose?

bunjee
7th February 2008, 01:57
Ok,

I have a custom "bubble widget" like the one in an iChat conversation.

When hoovering the widget with the mouse I'm replacing painted text by a QTextEdit to get the "copy", "open links" (etc) functionnalities.

Only problem is when drag and dropping a block of text out of the bubble it crashes because the leave event deletes my QTextEdit.

So my question is :
- Is there a way to disable Drag and drop on a QTextEdit.
- Or do you have a better approach for doing it.

wysota
7th February 2008, 10:53
Try using a QLabel and use QLabel::setTextInteractionFlags() with Qt::TextBrowserInteraction. If it doesn't give you what you want, use QTextBrowser or QTextEdit instead but don't delete it when leaving its borders - instead remove its frame so that it looks like an ordinary text. Or you can even have the frame removed all the time. QTextEdit has QFrame as its ancesstor so you can manipulate its frame shape.

bunjee
7th February 2008, 14:18
//================================================== ===========================
//================================================== ===========================

/* virtual */ void ZeParagraphWidget::enterEvent(QEvent * event)
{
if (mTextBrowser)
{
mTextBrowser->document()->deleteLater();
mTextBrowser->deleteLater();
mTextBrowser = NULL;
}

mTextBrowser = new QTextBrowser(this);
mTextBrowser->setDocument(mTextDocument->clone());
mTextBrowser->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
mTextBrowser->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
mTextBrowser->setStyle(ZeStyle::get());

mTextBrowser->setOpenExternalLinks(true);
mTextBrowser->setContextMenuPolicy(Qt::NoContextMenu);

mTextBrowser->move(-1, -1);
mTextBrowser->resize(mTextDocument->idealWidth() + 2, height() + 2);
mTextBrowser->show();
update();
}

That stuff is working. Instead of tweaking the QFrame I set my own style.
Only problem is : when entered the newly created TextBrowser never gets deleted.

I guess would need some kind of isActive() flag for the QTextBrowser in order to choose wether I can delete it or not in the leaveEvent.

Thanks man.

bunjee
7th February 2008, 15:07
Can someone offer advice on how to disable drag and drop in a QTextEdit? I
would like to handle it manually.

wysota
7th February 2008, 17:41
Only problem is : when entered the newly created TextBrowser never gets deleted.
Why should it get deleted? Create it when you create the widget and leave it there, regardless of entering or leaving the area. Just use QTextBrowser instead of whatever you used to use there.


Can someone offer advice on how to disable drag and drop in a QTextEdit? I
would like to handle it manually.

Looks like the only thing you want is copy & paste (+drag and drop which is really copy&paste too). Why don't you forget about using a text edit and implement copy & paste functionality for a completely custom widget?