I am trying to create a PieMenu in a QtWidgets application and that would mean I need to use QQuickWidget to display the QQuick component.
When using PySide2 all the hover effects and animations are disabled but the menu clicking appears to be working. I tried the same code in C++ Qt and it works correctly.
Is this a limitation in PySide2 or am I supposed to do it differently?
here's my code:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtQuickWidgets/QQuickWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void menuTriggered();
private:
Ui::MainWindow *ui;
QQuickWidget *view;
QQuickItem *object;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QCursor>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->view = new QQuickWidget(this);
view->setSource(QUrl("qrc:/main.qml"));
view->setClearColor(Qt::transparent);
view->setWindowFlags(Qt::FramelessWindowHint|Qt::Tool|Qt::WindowStaysOnTopHint);
view->setAttribute(Qt::WA_TranslucentBackground);
view->setAttribute(Qt::WA_NoSystemBackground);
view->setAttribute(Qt::WA_TransparentForMouseEvents);
view->setStyleSheet("background:transparent; border:none; color:white;");
view->hide();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::menuTriggered()
{
view->move(QCursor::pos());
view->show();
QMetaObject::invokeMethod(object, "openMenu");
}
main.qml
import QtQuick 2.0
import QtQuick.Extras 1.4
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
Item {
id: item1
width: pieMenu.width+50
height: pieMenu.height+80
function openMenu(){
pieMenu.popup(width/2,height/2)
}
PieMenu {
id: pieMenu
anchors.verticalCenter: parent.verticalCenter
anchors.verticalCenterOffset: 0
height:width
objectName: "pieMenu"
triggerMode: TriggerMode.TriggerOnPress
MenuItem {
text: "Action 1"
onTriggered: print("Action 1")
}
MenuItem {
text: "Action 2"
onTriggered: print("Action 2")
}
MenuItem {
text: "Action 3"
onTriggered: print("Action 3")
}
}
}
the problem code in python:
import sys
from PySide2 import QtCore, QtWidgets, QtGui
from PySide2.QtQuickWidgets import QQuickWidget
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
...
...
self.view = QQuickWidget(self)
self.view.setSource(QtCore.QUrl.fromLocalFile("main.qml"))
self.view.setClearColor(QtCore.Qt.transparent)
self.view.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.view.setAttribute(QtCore.Qt.WA_NoSystemBackground)
self.view.setWindowFlags(QtCore.Qt.FramelessWindowHint |
QtCore.Qt.Tool | QtCore.Qt.WindowStaysOnTopHint)
self.object = self.view.rootObject()
self.object.visibilityChanged.connect(self.menuTriggered)
@QtCore.Slot()
def menuTriggered(self):
self.view.move(QtGui.QCursor.pos())
self.view.show()
QtCore.QMetaObject.invokeMethod(self.object, "openMenu")
if __name__ == "__main__":
app = QtWidgets.QApplication([])
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec_())
from PySide2 QQuickWidget hover effects not working
No comments:
Post a Comment