make a QGraphicsItemGroup with two QGraphicsSimpleTextItem-s?

Assume I want to display in some QGraphicsScene a simple genealogy tree. Each person there has exactly a first name and a last name (and nothing else displayed).

For each person there, I want to build a QGraphicsItemGroup made of two QGraphicsSimpleTextItem-s vertically aligned and centered. One for first names of persons, one for their family names. Each having its own font & color. I don't want to use a heavy QGraphicsTextItem because it is too heavy (a person name with first and last names only don't deserve a full-fledged QTextDocument).

So I was thinking of

 struct Person {
   QGraphicsItemGroup _group;
   QGraphicsLinearLayout _lay;
   QGraphicsSimpleTextItem _firstname;
   QGraphicsSimpleTextItem _lastname;
 public:
   Person(QGraphicsScene*scene, 
          const std::string& first, const std::string& last) 
   : group(), _lay(Qt::Vertical), 
     _firstname(first.c_str()), _lastname(last.c_str()) {
      _lay.addItem(&_firstname);
      _lay.setSpacing(0, 5);
      _lay.addItem(&_lastname);
      _group.addToGroup(&_firstname);
      _group.addToGroup(&_lastname);
      scene->addItem(&_group);
   };
 };

but this don't work because _lay.addItem(&_firstname); don't compile, since a QGraphicsSimpleTextItem is not a QGraphicsLayoutItem

Any hints? Or is my entire approach wrong?

Should I define a class inheriting both from QGraphicsSimpleTextItem and from QGraphicsLayoutItem ?

NB: the actual code (GPlv3 licensed) is in my basixmo project on github, file guiqt.cc, commit 99fd6d7c1ff261. It is not a genealogy project, but an abstract syntax tree editor like thing for an interpreter with persistence. The concerned class is BxoCommentedObjrefShow; instead of a first name, I am showing an id like _7rH2hUnmW63o78UGC, instead of the last name, I am showing a short comment like body of test1_ptrb_get. The basixmo project itself is a tentative rewrite of melt-monitor-2015 in C++11 & Qt5

Upvotes: 1

Views: 672

Answers (1)

Adrian Maire
Adrian Maire

Reputation: 14865

It seem you supposed a purpose to the QGraphicsLinearLayout which is not its real purspose:

From Official QT 5.7 documentation

The QGraphicsLinearLayout class provides a horizontal or vertical layout for managing widgets in Graphics View

It is not supposed to layout normal drawing items in you scene, but QWidgets. Unfortunately, the documentation and examples about it seem not to work.

How to implement vertical layout

Fortunately, the QGraphicsItem is not difficult to extends, so you may implement your pair of text in few lines:

#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsSimpleTextItem>
#include <QGraphicsItemGroup>

class QGraphicsPerson: public QGraphicsItemGroup
{
public:
    QGraphicsPerson( const QString& firstStr,
                     const QString& secondStr,
                     QGraphicsItem *parent=nullptr)
    {
        first.setText(firstStr);
        second.setText(secondStr);
        addToGroup(&first);
        addToGroup(&second);
        second.setPos(first.pos().x(), first.pos().y()+first.boundingRect().height());
    }

protected:
    QGraphicsSimpleTextItem first, second;
};

int main(int n, char **args)
{
    QApplication app(n, args);

    QGraphicsScene scene;
    QGraphicsView window(&scene);

    QGraphicsPerson person( "Adrian", "Maire");
    scene.addItem(&person);
    person.setPos(30,30);

    QGraphicsPerson person2( "Another", "Name");
    scene.addItem(&person2);

    window.show();
    return app.exec();
}

Note: Scene space extends to the infinite in both direction, so space management is very simplified (no minimum/maximum sizes, stretching, etc.)

Upvotes: 1

Related Questions