0

I have a widget with this structure: main->QMainWindow->QFrame->QGraphicsView->QScene->QGraphicsPixMapItem->QPixmap

I had to do it this way because im not using Qt creator or QML, just widgets. Anyway I added an event filter to my QMainindow to be movable when clicking and dragging whenever side of the window And it worked. But due to the QGraphicsView implementation if I try to drag it doesnt work but still receives input (a menu opens when i click). What makes QGraphicsview so stubborn and how do i make the window to be draggable when i click and drag on the QGraphics view, Even installing the eventFilter on the view and frame but with no results. Thanks in advance and this is my code. This is the problem, the red square is the QGrapicsView, the white one is the MainWindow/Qframe. See how i still can make right clic on the red one and the menu still appears. I made a debug and see i get 2 objs when i click, the mainwindow and the Qgraphicview so i dont know why the move functionality just doesn work.

enter image description here

Main.cpp

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return QApplication::exec();
}

MainWindow.h

protected:

    bool eventFilter(QObject *obj, QEvent *event) override{
        if (event->type() == QEvent::MouseButtonPress ) {
            qDebug() << event;
            auto *ev = (QMouseEvent *) event;
            if (ev->button() == Qt::RightButton) {
                //Opens menu, it works well when i click on the QGraphics as well
                auto *menu = new QMenu(this);
                auto *idle = new QAction("Idle", this);
                auto *close = new QAction("Quit", this);
                menu->addAction(idle);
                
                menu->popup(ev->globalPos());
                connect(close, &QAction::triggered, [](){QCoreApplication::quit();});
                connect(idle, &QAction::triggered, [this](){changeDance(0);});
            }
            else{
                // "grab" the mainWindow
                this->oldPos = ev->globalPos();
            }
        }
        if (event->type() == QEvent::MouseMove) {
            //drag the window
            auto *ev = (QMouseEvent *) event;
            const QPoint delta = ev->globalPos() - oldPos;
            move(x() + delta.x(), y() + delta.y());
            oldPos = ev->globalPos();
        }
    }

MainWindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent) {
    setWindowTitle("waifu"); //waifu
    setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
    setFixedSize(screenWidth, screenHeight);
    //setAttribute(Qt::WA_TranslucentBackground, true);
    setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);
    setWindowFlags(Qt::BypassGraphicsProxyWidget);

    //mainWindow->frame->view->scene->pixmap
    installEventFilter(this);
    frame =  new Frame(this);
    setCentralWidget(frame);

    view = new View(frame);
    view->setFixedSize(50, 50);
    scene = new Scene(view);
    view->setScene(this->scene); // That connects the view with the scene
    frame->layout()->addWidget(view);
    myItem = new QGraphicsPixmapItem(*new QPixmap());
    //scene->setBackgroundBrush(QBrush(Qt::yellow));
    scene->addItem(myItem);
    view->show();

Frame.h


class Frame : public QFrame {
Q_OBJECT

public:
    explicit Frame(QMainWindow *parent = 0) : QFrame(parent) {
        setMouseTracking(true);
        setStyleSheet("background-color: red;"); //delete
        setLayout(new QVBoxLayout);
        layout()->setContentsMargins(0, 0, 0, 0);
        setWindowFlags(Qt::FramelessWindowHint);   //No windowing
        setAttribute(Qt::WA_TranslucentBackground); // No background
        setStyleSheet("background-color: transparent;");
    };
};

QGraphicsView.h

class View : public QGraphicsView {
Q_OBJECT

public:
    explicit View(QFrame *parent) : QGraphicsView(parent) {
        setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        setMouseTracking(true);
    };
protected:

private:


};

Tried to install the event filter located in the main window, in the Qframe and QGraphicsView class. with the parameter event filter being *parent.

0 Answers0