Results 1 to 6 of 6

Thread: QGraphicsLineItem - selection style

  1. #1
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsLineItem - selection style

    hi,
    i draw a line in QGraphicsScene, which can be selected and moved.
    Qt Code:
    1. MainPlot=new QGraphicsScene(centralWidget());
    2. MainPlot->setSceneRect(0,0,500,500);
    3.  
    4. QPen _Pen;
    5. _Pen.setColor(Qt::red);
    6. _Pen.setWidth(3);
    7.  
    8. QGraphicsLineItem* _Line=new QGraphicsLineItem(100,100,450,300);
    9. _Line->setPen(_Pen);
    10. _Line->setVisible(true);
    11. _Line->setFlags(QGraphicsLineItem::ItemIsSelectable | QGraphicsLineItem::ItemIsMovable);
    12. MainPlot->addItem(_Line);
    13.  
    14. ui.view->setScene(MainPlot);
    15. //ui.view is raphicsView
    To copy to clipboard, switch view to plain text mode 
    But i don't want that when line is selected, a rectangular is drawn (and line is its diagonal). I want line selection like in "diagramscene" example supplied with Qt documantation (thin rectangular paralel with line), or some custom selection style. How to do that? I can't find this in diagramscene source.

    Thanx
    Attached Images Attached Images
    Last edited by stefan; 7th August 2008 at 16:51.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsLineItem - selection style

    Well, you are using QGraphicsLIneItem, while in the Diagram scene example, they are using QGraphicsPolygonItem for arrows.

    Read the code for that example

  3. #3
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsLineItem - selection style

    Quote Originally Posted by aamer4yu View Post
    Well, you are using QGraphicsLIneItem, while in the Diagram scene example, they are using QGraphicsPolygonItem for arrows.

    Read the code for that example
    yes, but Arrow object consists from a line and arrow. It looks like arrow is just drawn in paint method. Arrow class inherits QGraphicsLIneItem
    Qt Code:
    1. class Arrow : public QGraphicsLineItem
    To copy to clipboard, switch view to plain text mode 
    why is selection different then in basic QGraphicsLIneItem?
    what am I missing?

  4. #4
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsLineItem - selection style

    In the diagram escen example they reimplement the boundingRect function .
    I think by default it draws the boundingRect with a dotted line .I have checked the QGraphicsLineItem::boundingrect function ,I dont know what does it do ,but ini some cases it returns that rectangle that is in the picture .
    If God has friends ,then I cant be God.

  5. #5
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsLineItem - selection style

    I found a solution. It seems that paint method handles "selection display style". so it's easy to set a custom selection style to QGraphicsItem or derivatives.
    Qt Code:
    1. void iddLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    2. {
    3. QLineF _Line=line();
    4. qint32 _x1=line().x1();
    5. qint32 _y1=line().y1();
    6. qint32 _x2=line().x2();
    7. qint32 _y2=line().y2();
    8.  
    9. QPen _Pen;
    10. _Pen.setColor(Qt::red);
    11. if (isSelected()) _Pen.setWidth(3);
    12. else _Pen.setWidth(1);
    13. painter->setPen(_Pen);
    14.  
    15. painter->drawLine(_Line);
    16.  
    17. if (isSelected())
    18. {
    19. //Draw whatever you want ;)
    20. //painter->drawRect(...)
    21. //painter->drawEllipse(...)
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Sep 2010
    Posts
    6

    Default Re: QGraphicsLineItem - selection style

    Hi,
    Can you please help me with QGraphicsLineItem paint() function:

    I set the pen width to 1 but the line has a width grater the 1 pixel.
    Do I have to change other settings?

    Thanks

Similar Threads

  1. Mac Style for Qt Application---help needed
    By swamyonline in forum Qt Programming
    Replies: 3
    Last Post: 15th June 2008, 21:49

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
  •  
Qt is a trademark of The Qt Company.