当前位置:   article > 正文

QML修改Control2中ToolTip附加属性的样式

QML修改Control2中ToolTip附加属性的样式

ToolTip是一个较为常用的组件,一般有两种使用方式,直接创建组件或者用附加属性:

  1. Text{
  2. text: "..."
  3. anchors.centerIn: parent
  4. //附加属性的方式
  5. ToolTip.text: "..."
  6. ToolTip.visible: mouse.containsMouse
  7. //直接创建组件
  8. ToolTip{
  9. id: tip
  10. text: "..."
  11. visible: !mouse.containsMouse
  12. }
  13. }

如果是直接创建组件,那很容易就可以替换为自定义的ToolTip,换个组件名就行了。如果想修改附加属性的样式就有问题了,直接把ToolTip替换为自定义的组件名的话,那么显示出来的效果还是默认的ToolTip 。

我们可以从源码中找到问题的根源(E:\Qt\qt-everywhere-src-5.15.2\qtquickcontrols2\src\quicktemplates2\qquicktooltip.cpp):

  1. QQuickToolTip *QQuickToolTipAttachedPrivate::instance(bool create) const
  2. {
  3. QQmlEngine *engine = qmlEngine(parent);
  4. if (!engine)
  5. return nullptr;
  6. static const char *name = "_q_QQuickToolTip";
  7. QQuickToolTip *tip = engine->property(name).value<QQuickToolTip *>();
  8. if (!tip && create) {
  9. // TODO: a cleaner way to create the instance? QQml(Meta)Type?
  10. QQmlComponent component(engine);
  11. component.setData("import QtQuick.Controls 2.4; ToolTip { }", QUrl());
  12. QObject *object = component.create();
  13. if (object)
  14. object->setParent(engine);
  15. tip = qobject_cast<QQuickToolTip *>(object);
  16. if (!tip)
  17. delete object;
  18. else
  19. engine->setProperty(name, QVariant::fromValue(object));
  20. }
  21. return tip;
  22. }

可以看到,附加属性中这个ToolTip的创建是写死的,并以动态属性的方式保存在engine对象。要改样式只有两种方式:创建同名组件(定义记得带限定符,如T.ToolTip),或者修改源码设置的Property。

如果创建同名组件,那可能有些地方会写漏导致没覆盖掉,所以我认为修改源码设置的Property是更好的方法。

下面是我的测试效果:

GitHub链接:https://github.com/gongjianbo/MyTestCode/tree/master/Qml/TestQml_20210303_ToolTip

完整代码如下(main.cpp+BasicToolTip.qml+main.qml三个文件):

  1. #include <QGuiApplication>
  2. #include <QQmlApplicationEngine>
  3. #include <QQmlComponent>
  4. #include <QDebug>
  5. void updateToolTip(QQmlApplicationEngine *engine);
  6. int main(int argc, char *argv[])
  7. {
  8. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  9. QGuiApplication app(argc, argv);
  10. QQmlApplicationEngine engine;
  11. const QUrl url(QStringLiteral("qrc:/main.qml"));
  12. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
  13. &app, [url](QObject *obj, const QUrl &objUrl) {
  14. if (!obj && url == objUrl)
  15. QCoreApplication::exit(-1);
  16. }, Qt::QueuedConnection);
  17. engine.load(url);
  18. updateToolTip(&engine);
  19. return app.exec();
  20. }
  21. void updateToolTip(QQmlApplicationEngine *engine)
  22. {
  23. //替换默认的ToolTip附加属性样式
  24. static const char *name = "_q_QQuickToolTip";
  25. QQmlComponent *component = new QQmlComponent(engine,QUrl("qrc:/BasicToolTip.qml"),qApp);
  26. auto create_func = [component,engine]{
  27. if(component->isError()){
  28. qWarning() << "create BasicToolTip failed." << component->errors();
  29. }else if(component->isReady()){
  30. QObject *object = component->create();
  31. if (object){
  32. object->setParent(engine);
  33. engine->setProperty(name, QVariant::fromValue(object));
  34. }
  35. }
  36. };
  37. if(component->isLoading()){
  38. QObject::connect(component,&QQmlComponent::statusChanged,create_func);
  39. }else{
  40. create_func();
  41. }
  42. }
  1. import QtQuick 2.12
  2. import QtQuick.Controls 2.12
  3. import QtQuick.Controls.impl 2.12
  4. import QtQuick.Templates 2.12 as T
  5. T.ToolTip {
  6. id: control
  7. x: parent ? Number.parseInt((parent.width - implicitWidth) / 2) : 0
  8. y: -implicitHeight - 2
  9. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  10. contentWidth + leftPadding + rightPadding)
  11. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  12. contentHeight + topPadding + bottomPadding)
  13. margins: 6
  14. verticalPadding: 8
  15. horizontalPadding: 12
  16. closePolicy: T.Popup.CloseOnEscape | T.Popup.CloseOnPressOutsideParent | T.Popup.CloseOnReleaseOutsideParent
  17. font{
  18. family: "Microsoft YaHei"
  19. pixelSize: 14
  20. }
  21. contentItem: Text {
  22. text: control.text
  23. font: control.font
  24. color: "red"
  25. }
  26. background: Rectangle {
  27. border.color: "red"
  28. color: "white"
  29. radius: 4
  30. }
  31. }
  1. import QtQuick 2.12
  2. import QtQuick.Window 2.12
  3. import QtQuick.Controls 2.12
  4. Window {
  5. visible: true
  6. width: 640
  7. height: 480
  8. title: qsTr("Qml EventLoop")
  9. Rectangle{
  10. width: 300
  11. height: 300
  12. border.color: "red"
  13. anchors.centerIn: parent
  14. Text{
  15. text: "hover时显示自定义样式"
  16. anchors.centerIn: parent
  17. ToolTip.text: "自定义样式"
  18. ToolTip.visible: mouse.containsMouse
  19. ToolTip{
  20. id: tip
  21. text: "默认样式"
  22. visible: !mouse.containsMouse
  23. }
  24. }
  25. MouseArea{
  26. id: mouse
  27. anchors.fill: parent
  28. hoverEnabled: true
  29. }
  30. }
  31. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/805985
推荐阅读
相关标签
  

闽ICP备14008679号