Results 1 to 5 of 5

Thread: QGraphicsRectItem with list of QStrings

  1. #1
    Join Date
    Apr 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post QGraphicsRectItem with list of QStrings

    Hi,
    I want to make a class which will represent single entity in entity relationship diagram.
    It should be an rect object with name and attributes which will adjust it's size to the text it is containing.
    Because of that I am new to qt I started to play with qt doc and intuition which have led me to something like this:

    Qt Code:
    1. class Entity : public QObject, public QGraphicsRectItem {
    2. Q_OBJECT;
    3.  
    4. public:
    5. Entity(QGraphicsItem *parent = 0);
    6.  
    7. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    8. QWidget *widget = 0);
    9. void setName(QString name);
    10. void addField(QString field);
    11.  
    12. private:
    13. void adjustRect();
    14.  
    15. QString name;
    16. QList<QString> fields;
    17.  
    18.  
    19. };
    20.  
    21. Entity::Entity(QGraphicsItem *parent)
    22. {
    23. setFlag(QGraphicsItem::ItemIsMovable);
    24. setFlag(QGraphicsItem::ItemIsSelectable);
    25. }
    26.  
    27. void Entity::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    28. {
    29. painter->setPen(Qt::black);
    30. painter->setBrush(QColor(230,230,230));
    31. painter->drawRect(rect());
    32.  
    33. QPointF pos = QPointF(rect().x() + 10, rect().y() + 20);
    34. painter->drawText(pos, name);
    35.  
    36. qDebug() << rect().width();
    37.  
    38. painter->drawLine(pos.x() - 10, pos.y() + 5, pos.x() + rect().width() - 10, pos.y()+5);
    39.  
    40. QString str;
    41. foreach(str, fields) {
    42. pos.setY(pos.y() + 20);
    43. painter->drawText(pos, str);
    44. }
    45.  
    46. }
    47.  
    48. void Entity::setName(QString name)
    49. {
    50. this->name = name;
    51. adjustRect();
    52. }
    53.  
    54. void Entity::addField(QString field)
    55. {
    56. fields.append(field);
    57. adjustRect();
    58. }
    59.  
    60. void Entity::adjustRect()
    61. {
    62. qreal maxx = 20;
    63.  
    64. maxx = (maxx < name.length()*10 ? name.length()*10 : maxx);
    65.  
    66. QString str;
    67. foreach(str, fields) {
    68. maxx = (maxx < str.length()*10 ? str.length()*10 : maxx);
    69. }
    70.  
    71. setRect(rect().x(), rect().y(), maxx, fields.count()*30 + 10);
    72. }
    To copy to clipboard, switch view to plain text mode 

    It even works but it seems weird to me and I know there is better solution. Can you point me what am I doing wrong and where should I look for (classes, etc.).

    I also don't know why entities are not selectable despite the fact I set the flag to ItemIsSelectable.
    Last edited by January; 23rd April 2012 at 23:03.

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsRectItem with list of QStrings

    Hi,

    QGraphic items are drawn in a scene. An and scene is displayed in a QGraphicsView. If you want is draw entities in a relationship diagram you can draw all the fields inside a rectangle... But I would encourage you to look at ProxyWidget. With it, you can have a widgets like listviews to be draw in a scene. You can create a widget to handle your entities as complex as needed and QT will draw it in the scene. You can embed a proxywidget inside a QGraphicsRectItem by setting the parent of proxy = the rectangle.

    For now, from your example, I reckon you don't need to re-implement the paint event.

    I can post an example if you need.

    Carlos

  3. #3
    Join Date
    Apr 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsRectItem with list of QStrings

    Hi Carlos,

    Thanks for reply
    Quote Originally Posted by qlands View Post
    I can post an example if you need.
    I would be grateful if you did.

  4. #4
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsRectItem with list of QStrings

    Hi,

    You can download the example from here:

    http://www.qlands.com/other_files/scenexample.tar.gz

    I documented the example. You will see that I used a container to embed the proxy widget. I use a container because then you can easily use free transformations in the container (size/rotate) and the proxy will react accordingly. See example on free mouse transformations here:

    http://qt-apps.org/content/show.php/...content=133531

    Carlos.

  5. #5
    Join Date
    Apr 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsRectItem with list of QStrings

    Thanks for your example. I was rather thinking of simple graphics objects to be entities not complex ones but it really helped me.
    I even figured out how to make an application using a designer
    Thank you again.

Similar Threads

  1. Can't see container contents, QStrings when debugging
    By MattPhillips in forum Installation and Deployment
    Replies: 3
    Last Post: 31st December 2011, 21:35
  2. Can't declare more than 7 QStrings ?!?!?!
    By ruben.rodrigues in forum Newbie
    Replies: 8
    Last Post: 21st July 2010, 14:42
  3. concatenating path QStrings
    By rbp in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2008, 04:46
  4. QSettings on QStrings
    By OriginalCopy in forum Qt Programming
    Replies: 4
    Last Post: 4th November 2007, 10:57
  5. dynamic matrix of QStrings
    By QiT in forum Newbie
    Replies: 19
    Last Post: 4th April 2007, 09:26

Tags for this Thread

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.