qml中的背景透明设置:
view.setStyleSheet("background:transparent;");
view.setAttribute(Qt::WA_TranslucentBackground);
qml中设置背景透明后showMinimized()(最小化)不好用解决方案是:
对qml的焦点事件重载
头文件:
#ifndef QML_WIDGET_SET_H
#define QML_WIDGET_SET_H
#include <QDeclarativeView>
class qml_widget : public QDeclarativeView
{
Q_OBJECT
public:
explicit qml_widget(QDeclarativeView *parent = 0);
signals:
public slots:
virtual void focusInEvent(QFocusEvent *event);
};
#endif // QML_WIDGET_SET_H
cpp文件:
#include "qml_widget.h"
qml_widget::qml_widget(QDeclarativeView *parent) :
QDeclarativeView(parent)
{
}
void qml_widget::focusInEvent(QFocusEvent *event)
{
repaint();
}
main函数:
#include <QtGui/QApplication> #include <QResource> #include <QDeclarativeView> #include <QDeclarativeContext> #include <QTextCodec> #include <QDebug> #include "qml_widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("system"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("system"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("system"));
qml_widget playerview;
playerview.setSource(QUrl("qrc:player.qml"));
playerview.setWindowFlags(Qt::FramelessWindowHint);
playerview.setStyleSheet("background:transparent;");
playerview.setAttribute(Qt::WA_TranslucentBackground);
QObject *viewobj = dynamic_cast<QObject*>((QObject*)playerview.rootObject());
QObject::connect(viewobj,SIGNAL(min()),&playerview,SLOT(showMinimized()));
playerview.show();
return a.exec();
}
注:信号min()是qml页面中的发送的