I'm trying to create a popup text editor in Qt. The problem I'm having is that since I'm making the dialog borderless, I have to call the move() function myself. This is causing some weird behaviors that I would like to figure out. I've noticed the following scenarios:
- If I make the box without a text editor, then moving is fine. It is only when I add the editor that it becomes choppy.
- If I make it a normal dialog (instead of using Qt::Popup flag in constructor), then movement is fine with or without editor.
Here is some code to demonstrate:
#include "tex开发者_如何学JAVAtpopup.h"
#include <QPoint>
#include <QMouseEvent>
#include <QPushButton>
#include <QDebug>
TextPopup::TextPopup(QWidget* parent) :
QDialog(parent, Qt::Popup) // removing Qt::Popup flag gets rid of choppy-ness
{
setLayout(&layout);
layout.addWidget(&textEdit); // removing this gets rid of choppy-ness
resize(200, 200);
setFocusPolicy(Qt::StrongFocus);
}
void TextPopup::mousePressEvent(QMouseEvent* event)
{
offset = event->globalPos() - frameGeometry().topLeft();
previous = event->globalPos();
QDialog::mousePressEvent(event);
}
// move whenever user drags widget (does not apply to text editor)
void TextPopup::mouseMoveEvent(QMouseEvent* event)
{
qDebug() << "move";
if(event->buttons() == Qt::LeftButton) {
move(event->globalPos() - offset);
}
}
In case anyone is interested, I found a solution - it at least works. Instead of using the Qt::Popup flag I used the Qt::SplashScreen flag which also does not have the title bar and does not exhibit choppy behavior.
精彩评论