Results 1 to 16 of 16

Thread: Problem with paint()

  1. #1
    Join Date
    Dec 2012
    Posts
    13

    Default Problem with paint()

    Hello,

    I have a Node Item specified in C++ and in QML.

    I override the paint method in Node.cpp in the manner to draw a line between two nodes.

    Here the code extraction:

    Qt Code:
    1. void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. QPen pen;
    4. pen.setColor(Qt::blue);
    5. pen.setStyle(Qt::SolidLine);
    6. pen.setWidth(6);
    7. painter->setPen(pen);
    8. painter->setRenderHints(QPainter::Antialiasing, true);
    9.  
    10. for (QList<Node*>::const_iterator itParent = this->_parents.begin();
    11. itParent != this->_parents.end();
    12. ++itParent)
    13. {
    14. QPoint startPoint(this->posX(), this->posY());
    15. QPoint endPoint((*itParent)->posX(), (*itParent)->posY());
    16.  
    17. painter->drawLine(startPoint, endPoint);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. import QtQuick 1.0
    2.  
    3. Rectangle {
    4. width: 500
    5. height: 500
    6.  
    7. Repeater {
    8. anchors.fill: parent
    9. model: configModel
    10. delegate: Node {}
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. import QtQuick 1.0
    2. import NodeLib 1.0
    3.  
    4. Node {
    5. id: nodeDelegate
    6. x: posX
    7. y: posY
    8. visible: isDisplayed
    9.  
    10. Image{
    11. id : nodeIcon
    12. source: iconFilePath
    13. }
    14.  
    15. Text {
    16. anchors.top: nodeIcon.bottom
    17. text: nodeID
    18. }
    19.  
    20. MouseArea {
    21. anchors.fill: parent
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    I also do setFlag(ItemHasNoContents, false); in the constructor of Node.cpp

    The problem is I don´t get the line painted. I traced the code and I see that I pass through the paint function, but the this pointer points to an empty object and not to the object that I created in the main.cpp !
    I don´t understand why I don´t call the paint function on the Node objects that I have created in main.cpp

    Any idea ?

    Thanks in advance !

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with paint()

    My impression is that you are trying to draw outside the bounding rect defined by your Node class. As for you main(), I don't know, I haven't seen the code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2012
    Posts
    13

    Default Re: Problem with paint()

    Here the code of main.cpp

    Qt Code:
    1. Q_DECL_EXPORT int main(int argc, char *argv[])
    2. {
    3. QScopedPointer<QApplication> app(createApplication(argc, argv));
    4.  
    5. QDeclarativeView declarativeView;
    6. qmlRegisterType<Node>("NodeLib", 1, 0, "Node");
    7.  
    8. ConfigurationModel* p_configModel = ConfigurationModel::GetConfigModelInstance();
    9.  
    10. p_configModel->addNodeInConfigurationModel(new Node("PanelXXXX", PLUG_IN, EVALUATION_BLOCK, "./ui-images/cutetube.png"));
    11.  
    12. Node *p_nodeY = new Node("PanelYYYY", PLUG_IN, EVALUATION_BLOCK, "./ui-images/cutetube.png");
    13. p_configModel->addNodeInConfigurationModel(p_nodeY);
    14.  
    15. Node *p_nodeZ = new Node("PanelZZZZ", PLUG_IN, EVALUATION_BLOCK, "./ui-images/cutetube.png");
    16. p_nodeZ->setNodeID("NODE ZZZZ");
    17.  
    18. p_nodeY->setPosX(100);
    19. p_nodeY->setPosY(100);
    20. p_nodeY->setIsDisplayed(true);
    21.  
    22. p_nodeZ->setPosX(200);
    23. p_nodeZ->setPosY(200);
    24.  
    25. p_nodeZ->connectTo(p_nodeY);
    26.  
    27. p_configModel->addNodeInConfigurationModel(p_nodeZ);
    28.  
    29. // if the configuration has been changed, you need to call setContextProperty() again to update the QML view
    30.  
    31. declarativeView.rootContext()->setContextProperty("configModel", p_configModel);
    32.  
    33. declarativeView.setSource(QUrl::fromLocalFile("qml/ConfigurationView/main.qml"));
    34. declarativeView.show();
    35.  
    36. return app->exec();
    37. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with paint()

    So why would anything point to any of the Node instances you created in main? You're creating a new node instance in your QML file.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Dec 2012
    Posts
    13

    Default Re: Problem with paint()

    Could you please give me some simple example how to fix the issue ?


    Added after 4 minutes:


    What I thought and wanted is
    - create Node instances in main.cpp into the Model
    - display these created instances in the QML
    - call the paint function of each Node instance
    Last edited by Zaxy; 12th December 2012 at 11:03.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with paint()

    This doesn't sound like regular QML usage. If you create and expose a set of Node instances in main, then in QML you only use Javascript to manipulate the objects already there, you don't create new ones.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Dec 2012
    Posts
    13

    Default Re: Problem with paint()

    The problem is, when I traced the code, I call the Node:: paint() function, but on an empty Node object.

    Actually, I should call the paint() function of the already created Node objects instanciated in the main.cpp

    I don't understand why I call it on an empty Node object.

    Could you please advise something ?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with paint()

    I already told you. In your QML file you declare a "Node" element. That's none of the Node instances you create in main. Your Node instances created in main() are silently ignored by QML because they have no geometry and no relation to the items being painted. You can rem out all your main() code related to creating and setting up Node instances and it will not change the behaviour of your QML scene in any way. I understand you'd like each Node created in main() to have a text, icon and all the other stuff you declared in QML but that's not how it works.

    Your repeater declaration effectively creates empty nodes unrelated to any data.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Dec 2012
    Posts
    13

    Default Re: Problem with paint()

    Ok, but I got the Node instances properties like iconFilePath and nodeID displayed in QML. Just the paint function is not working.
    Otherwise, I got the properties from the model ...

    So at some point QML copies these values, but don't call the proper paint fucntion ...

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with paint()

    I don't understand what you mean by "proper" paint function. There is only one paint() method in Node.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Dec 2012
    Posts
    13

    Default Re: Problem with paint()

    Yes, there is only one paint() function in Node.

    The problem is that QML creates an object avec le default constructor of type Node. Than it calls its paint function.

    But this object hasn't its properties set, like e.g. _parentNodes list et donc my paint function is not working, because I want to draw a line between the current node and any parent node in _parents list.

    I thought maybe I could solve the problem by exposing _parents to QML and than draw the lines in the Component.onCompleted() function.
    But I don't know the right syntax for that.

    I have one question, when you call update() from C++ Qt, is after that the onCompleted() also called ?

    Here a proposal for the body of onCompleted, but it doesnt compile, I dont know the right syntax.
    Could you please advise me on that issue ?

    Qt Code:
    1. import QtQuick 1.0
    2. import NodeLib 1.0
    3.  
    4. Node {
    5. id: nodeDelegate
    6. x: posX
    7. y: posY
    8. visible: isDisplayed
    9.  
    10. Image{
    11. id : nodeIcon
    12. source: iconFilePath
    13. }
    14.  
    15. Text {
    16. anchors.top: nodeIcon.bottom
    17. text: nodeID
    18. }
    19.  
    20. MouseArea {
    21. anchors.fill: parent
    22. }
    23.  
    24. Component.onCompleted: {
    25. for (var i=0; i < parentNodes.count; i++)
    26. {
    27. Path: [
    28. // startX: posX,
    29. // startY: posY,
    30. PathLine {
    31. x: parentNodes.get(i).posX
    32. y: parentNodes[i].posY
    33. }
    34. ]
    35. }
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with paint()

    Quote Originally Posted by Zaxy View Post
    The problem is that QML creates an object avec le default constructor of type Node. Than it calls its paint function.

    But this object hasn't its properties set, like e.g. _parentNodes list et donc my paint function is not working, because I want to draw a line between the current node and any parent node in _parents list.
    Yes, and I already told you why this happens. The nodes QML creates have nothing to do with nodes you create in C++. They are separate entities.

    I have one question, when you call update() from C++ Qt, is after that the onCompleted() also called ?
    No, onCompleted is called once when the item definition it belongs to is parsed and constructed.

    Here a proposal for the body of onCompleted, but it doesnt compile, I dont know the right syntax.
    It doesn't compile because you can't mix JavaScript code and QML declarations.

    Could you please advise me on that issue ?
    Tell me why you want to use QML here at all.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Dec 2012
    Posts
    13

    Default Re: Problem with paint()

    I have to do this graph visualization:
    - nodes displayed with some info and an icon
    - connections between some nodes are displayed as graphical lines
    - onClick on a node, you can do some stuff, like rename the node, hide it, show it etc

    So either I do it entirely in C++/Qt or I do it in C++/QML.

    I thought, that with QML will be easier to do it and better displayed.

    I am doing a feasability study of both approeaches. But maybe it is not so easy to do it in C++ / QML ?

    What do you think about that. Is it feasible with QML and would be easier to do it ?

    Thanks again

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with paint()

    If you don't feel comfortable with QML and QtQuick and you don't need any fancy animations then I'd advise you to use QGraphicsView instead. It will feel much more natural.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Dec 2012
    Posts
    13

    Default Re: Problem with paint()

    Ok, and do you think that graph visualization is feasible in QML ?

    I don´t really need fancy animation, just an .png file representing the node, and some lines connecting the nodes. The rest should be simple text labels.

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with paint()

    Quote Originally Posted by Zaxy View Post
    Ok, and do you think that graph visualization is feasible in QML ?
    Yes, of course. Inpired by this thread I even implemented a small library for declaring edges in QtQuick 2.0 two days ago.

    I don´t really need fancy animation, just an .png file representing the node, and some lines connecting the nodes. The rest should be simple text labels.
    QGraphicsPixmapItem, QGraphicsTextItem and QGraphicsLineItem should be perfect for you.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QTextedit scroll or paint problem
    By pavanbarot in forum Qt Programming
    Replies: 0
    Last Post: 29th December 2010, 13:22
  2. QListWidget delegate paint problem.
    By rule in forum Qt Programming
    Replies: 0
    Last Post: 5th July 2010, 13:31
  3. Paint QGraphicsItem problem
    By dreamer in forum Qt Programming
    Replies: 3
    Last Post: 23rd June 2008, 18:18
  4. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  5. Replies: 1
    Last Post: 26th December 2007, 10:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.