6

嘿那里,

我是 Qt 的新手,我尝试创建一个可拖动的无框窗口。问题是,如果我移动窗口.. 有数千个(它?抱歉,不知道如何用英语描述它),直到我停下来。有没有办法解决这个问题,还是我想错了?

我的窗口.h

#include <QMainWindow>
#include <QLabel>
#include <QPushButton>
#include <QMouseEvent>

class MyWindow : public QMainWindow {
    Q_OBJECT
private:
    QLabel *label_title,*label_quit;
    QPushButton *bn_exit;
    QPixmap *pixmap;
    QPoint m_dragPosition;

public:
    MyWindow(QMainWindow *parent = 0, Qt::WindowFlags flags = 0);
protected:
    void paintEvent(QPaintEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
};

我的窗口.cpp

#include "MyWindow.h"
#include <QApplication>
#include <QPainter>
#include <QLabel>

MyWindow::MyWindow(QMainWindow *parent, Qt::WindowFlags flags) : QMainWindow(parent,flags) {

    resize(1024,576);
    setWindowTitle("Versuch1");

    //Titel
    label_title = new QLabel("irgendwas",this);
    label_title->setGeometry(10,10,500,40);
    label_title->setStyleSheet("font-family: Arial; letter-spacing: 4px;font-weight:bold; color : white;font-size: 30px");

    //Exit-Button
    bn_exit = new QPushButton("[Exit]",this);
    bn_exit->setGeometry(975,10,40,20);
    bn_exit->setStyleSheet("QPushButton {font-family: Verdana; font-size:15px; top:0px; border: none; background-color:#101010; color:white} QPushButton:hover {color: red }");
    connect(bn_exit,SIGNAL(clicked()),qApp,SLOT(quit()));
}


void MyWindow::paintEvent(QPaintEvent *event) // Painter
{
  QPainter painter(this);
  painter.setPen(Qt::NoPen); // deactivate Border

  painter.setBrush(QBrush("#101010")); // black title and footer
  painter.drawRect(0, 0, 1024, 60);

  painter.setBrush(QBrush("#101010"));
  painter.drawRect(0, 516, 1024, 576);
}
void MyWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton) {
        move(event->globalPos() - m_dragPosition);
        event->accept();
    }
}
void MyWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

主文件

#include <QApplication>
#include "MyWindow.h"

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    MyWindow* window =  new MyWindow(0, Qt::FramelessWindowHint);
    window->show();
    return app.exec();
}

问候,亚历克斯

4

2 回答 2

1

您可能想尝试在受保护的方法中调用基类方法。例如:

void MyWindow::paintEvent(QPaintEvent *event) // Painter
{
    QWidget::paintEvent(event);
    ...
}
void MyWindow::mouseMoveEvent(QMouseEvent *event)
{
    QWidget::moveMouseEvent(event);
    ...
}
于 2011-02-20T23:43:33.693 回答
0

我无法重现您的问题(Ubuntu 或 XP),但如何尝试处理重绘事件:

if (event->buttons() & Qt::LeftButton) {
    move(event->globalPos() - m_dragPosition);
    event->accept();
    QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}

可能是您的系统资源不足或您的显示驱动程序“慢”。尝试干净重启,只打开您的应用程序。

于 2011-03-26T10:47:48.247 回答