跳到內容
  • 版面
  • 最新
  • 標籤
  • 熱門
  • 使用者
  • 群組
  • 搜尋
  • Get Qt Extensions
  • Unsolved
Collapse
品牌標誌
  1. 首頁
  2. Qt Development
  3. QML and Qt Quick
  4. Feasability study: Displaying a graph with QML ?
Forum Updated to NodeBB v4.3 + New Features

Feasability study: Displaying a graph with QML ?

已排程 已置頂 已鎖定 已移動 QML and Qt Quick
29 貼文 7 Posters 18.3k 瀏覽 1 Watching
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • sierdzioS 離線
    sierdzioS 離線
    sierdzio
    Moderators
    寫於 最後由 編輯
    #17

    You inherit from Node.h, so the properties are visible in QML without refering to the id. Instead of "source: nodeDelegate.iconFilePath" you can just write "iconFilePath". But that has nothing to do with the model - that just assigns the value set in c++.

    To access the model, you need to use variables declared in the model. See "this page":http://doc-snapshot.qt-project.org/4.8/qdeclarativemodels.html for reference.

    (Z(:^

    1 條回覆 最後回覆
    0
    • Z 離線
      Z 離線
      Zaxy
      寫於 最後由 編輯
      #18

      Hi Sierdzio,

      Thanks for the article. I implemented a model with nodeID and iconFilePath roles and now I can display the Node objects in a GridView for example or in a Repeater.

      But I need a way to draw the graph on the screen:
      - order the Node objects in a suitable way on the screen - how could I place the objects in a custom way ? Is there something like currentNode.left = previousNode.right etc. ?
      - draw connection lines between some nodes - for that could I use the paint function ?

      Thanks again for the help !

      1 條回覆 最後回覆
      0
      • sierdzioS 離線
        sierdzioS 離線
        sierdzio
        Moderators
        寫於 最後由 編輯
        #19

        I would use QML Rectangle for that. If you want to detect things like previousnode.right, you need to implement it yourself. You could base this on meta object's parent and children properties if you wish.

        Or, of course, you can use your own paint function.

        (Z(:^

        1 條回覆 最後回覆
        0
        • Z 離線
          Z 離線
          Zaxy
          寫於 最後由 編輯
          #20

          So , to use the paint function to draw a line between two nodes.

          And about displaying the QList<Node*> in a customized way - for example in a tree view, what could I do ? I think, whan a new node is added to the list, it is automatically displayed, but I want somehow to re-order the graphical representation. I have also additional llist, containing the root Node(s), so I could visit all tree nodes, but don't know how to display them node by node and place them on the screen.

          Thanks

          1 條回覆 最後回覆
          0
          • sierdzioS 離線
            sierdzioS 離線
            sierdzio
            Moderators
            寫於 最後由 編輯
            #21

            I'm afraid some custom x and y setting is the way to go. You could try anchoring, but it would be too rigid.

            (Z(:^

            1 條回覆 最後回覆
            0
            • Z 離線
              Z 離線
              Zaxy
              寫於 最後由 編輯
              #22

              yes, I think so as well, I will add posX, posY as roles in my model, in the Repeater will have something like x: posX, y: posY ... and use the paint function to draw a line.

              thanks

              1 條回覆 最後回覆
              0
              • Z 離線
                Z 離線
                Zaxy
                寫於 最後由 編輯
                #23

                just one question: when is the paint() function called ? Always, when a change on the Node object has been done ?

                1 條回覆 最後回覆
                0
                • Z 離線
                  Z 離線
                  Zaxy
                  寫於 最後由 編輯
                  #24

                  I implemented the paint() function of Node in the following way:

                  @void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                  {
                  QPen pen;
                  pen.setColor(Qt::blue);
                  pen.setStyle(Qt::SolidLine);
                  pen.setWidth(2);
                  painter->setPen(pen);
                  painter->setRenderHints(QPainter::Antialiasing, true);

                  for (QList<Node*>::const_iterator itParent = _parents.constBegin();
                       itParent != _parents.constEnd();
                       itParent++)
                  {
                      QPoint startPoint(this->posX(), this->posY());
                      QPoint endPoint((*itParent)->posX(), (*itParent)->posY());
                  
                      painter->drawLine(startPoint, endPoint);
                  }
                  

                  }@

                  Than in the main.cpp I create 3 Nodes, and two of them as connected (Node3 has as parent Node2). But I don't see the line in the DeclarativeView window.

                  Do I need to emit a signal that the line should be painted or how could I call the paint function explicitly ? I'd like to know how does it work and when is it called this paint function ?

                  Thanks in advance

                  1 條回覆 最後回覆
                  0
                  • Z 離線
                    Z 離線
                    Zaxy
                    寫於 最後由 編輯
                    #25

                    Once again the main.qml and Node.qml:

                    main.qml:
                    @import QtQuick 1.0

                    Rectangle {
                    width: 500
                    height: 500

                    Repeater {
                        anchors.fill: parent
                        model: configModel
                        delegate: Node {}
                    }
                    

                    }
                    @

                    Node.qml:
                    @import QtQuick 1.0
                    import NodeLib 1.0

                    Node {
                    id: nodeDelegate
                    x: posX
                    y: posY
                    visible: isDisplayed

                    Image{
                        id : nodeIcon
                        source: iconFilePath
                    }
                    
                    Text {
                       anchors.top: nodeIcon.bottom
                       text: nodeID
                    }
                    
                    MouseArea {
                       anchors.fill: parent
                    }
                    

                    }
                    @

                    1 條回覆 最後回覆
                    0
                    • A 離線
                      A 離線
                      aheller
                      寫於 最後由 編輯
                      #26

                      xCharts and D3 are pretty nice ...
                      but how can I use these scripts from within QML?
                      simply including the javascript files doesn't seem to work ...

                      with "try porting some small js library", you suggest to translate js to QML?
                      is there a way to leave the javascript files unchanged?
                      then one could also use the same code in webkit ...

                      any ideas?
                      thx

                      [quote author="nizeguy" date="1354837502"]Try porting some small js library that already does that to html canvas and tell us about it.

                      If it can't be done easily in javascript qml for some reason, it can surely be done in QtQuick in C++ .

                      ps: just found this one in reddit/programming : http://tenxer.github.com/xcharts/examples/[/quote]

                      1 條回覆 最後回覆
                      0
                      • C 離線
                        C 離線
                        chrisadams
                        寫於 最後由 編輯
                        #27

                        Hi,

                        If you want to use a third-party js library which does charting/graphing, you most likely want to follow the following steps:

                        1. use the QML Canvas type (QtQuick2 only) and pass this object to a compatibility JavaScript resource via an init function call.

                        eg:
                        @
                        import QtQuick 2.0
                        import "compat.js" as 3rdPartyWrapper

                        Item {
                        Canvas { id: c }
                        Component.onCompleted: { 3rdPartyWrapper.init(c) }
                        }
                        @

                        1. import your compatibility js resource, which sets up the context as required (eg, has the var properties defined which the 3rd party js lib expects) and then does a Qt.include("3rdParty.js")

                        eg:
                        @
                        // compat.js
                        var Window = []
                        var Canvas = {}
                        Qt.include("3rdParty.js")

                        function init(canvasObject c) {
                        Canvas = c
                        drawGraph();
                        }

                        function drawGraph(/* params ... */) {
                        // call necessary functionality from 3rdParty.js
                        }
                        @

                        In general, using 3rd party js libs is possible, but can be tricky, mainly because of the fact that it is impossible (currently) to modify the evaluation context of a script at import time. This is a well known issue, which used to be scheduled for 5.1, but now will most likely be addressed for some later release.

                        When it comes to graphing (or anything which requires drawing lines) in QML, just use the Canvas element. Take a look at the StocQt example for some inspiration.

                        Cheers,
                        Chris.

                        1 條回覆 最後回覆
                        0
                        • Q 離線
                          Q 離線
                          quantumavatar
                          寫於 最後由 編輯
                          #28

                          Im guessing you could also just use D3 via QtWebKit and have the javascript communicate with qml. I myself have this problem and am working on it.

                          1 條回覆 最後回覆
                          0
                          • D 離線
                            D 離線
                            dvolosnykh
                            寫於 最後由 編輯
                            #29

                            Hi, all!

                            Any successful results on integrating 3rd-party JavaScript libraries (i.e. D3.js) into QML or JavaScript resources so far? I am unable to find any concrete info on this topic, except for the example of "modifying QChart.js":http://jwintz.me/blog/2014/02/15/qchart-dot-js-qml-binding-for-chart-dot-js/ library's source code to make it work inside QML.

                            Regards,
                            Dmitrii.

                            1 條回覆 最後回覆
                            0

                            • 登入

                            • Login or register to search.
                            • 第一個貼文
                              最後的貼文
                            0
                            • 版面
                            • 最新
                            • 標籤
                            • 熱門
                            • 使用者
                            • 群組
                            • 搜尋
                            • Get Qt Extensions
                            • Unsolved