我的UI出现问题。我正在尝试使其可移动,但我不断收到随机错误。这是错误的屏幕截图:http://prntscr.com/79esqu

DraggableWindow.cpp代码:

#include "DraggableWindow.h"

#include <QWidget>
#include <QMainWindow>
#include <QPoint>
#include <QMouseEvent>
#include <Qt>

DraggableWindow::DraggableWindow(QWidget *parent)
: QMainWindow(parent)
, m_captured(false)
, m_last_event_pos(QPoint(0, 0))
{
}

void DraggableWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_last_event_pos = event->pos();
        m_captured = true;
    }
}

void DraggableWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(m_captured)
    {
        this->move(this->pos() + (event->pos()-m_last_event_pos));
    }
}

void DraggableWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_captured = false;
    }
}


DraggableWindow.h代码:

#pragma once

#include <QMainWindow>
#include <QWidget>
#include <QMouseEvent>
#include <QPoint>

class DraggableWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit DraggableWindow(QWidget *parent = 0);

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);

private:
    bool m_captured;
    QPoint m_last_event_pos;
};


mainlauncher.cpp的启动脚本:

MainLauncher::MainLauncher(QWidget *parent)
: DraggableWindow(parent)
, ui(Ui::MainLauncher)
{
    ui->setupUi(this);
}


我有问题。请帮忙!

最佳答案

您得到的是链接器错误。似乎您的DraggableWindow.cpp尚未被编译。

确保已在.pro文件的DraggableWindow.cpp部分和SOURCES部分的DraggableWindow.h中包括了HEADERS

如果它们已正确添加到您的.pro文件中,则在添加它们后qmake可能尚未运行。尝试Build->Run Qmake,然后尝试Build->Rebuild All

关于c++ - 可移动的UI错误Qt5,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30447508/

10-10 21:54