Results 1 to 5 of 5

Thread: QGraphicsRectItem with list of QStrings

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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