[Solved] QGraphicsPixmapItem && QGraphicsGridLayout



  • Hi all,

    is there a way to put some QGraphicsPixmapItem in a grid?

    In QT 4.6 documentation there is a QGraphicsGridLayout example:

    @
    QGraphicsScene scene;
    QGraphicsWidget *textEdit = scene.addWidget(new QTextEdit);
    QGraphicsWidget *pushButton = scene.addWidget(new QPushButton);

    QGraphicsGridLayout *layout = new QGraphicsGridLayout;
    layout->addItem(textEdit, 0, 0);
    layout->addItem(pushButton, 0, 1);

    QGraphicsWidget *form = new QGraphicsWidget;
    form->setLayout(layout);
    scene.addItem(form);
    @

    but it use a QGraphicsWidget and I need to use a QGraphicsPixmapItem to show some pixmap in a grid.



  • try this: layout->addItem(graphicsPixmapItem->parentWidget(), 0, 2); //if you are using QT 4.4+



  • Well said it too early without trying. I don't think there is a direct way to use QGraphicsGridLayout for QGraphicsPixmapItem. It is for QGraphicWidgets only.

    May be you can determine view/scene size calculate positions on the fly and position them accordingly in the scene.



  • Thanks,

    Now I'm trying the example I posted before:

    @
    QGraphicsScene scene;
    QGraphicsWidget *textEdit = scene.addWidget(new QTextEdit);
    QGraphicsWidget *pushButton = scene.addWidget(new QPushButton);

    QGraphicsGridLayout *layout = new QGraphicsGridLayout;
    layout->addItem(textEdit, 0, 0);
    layout->addItem(pushButton, 0, 1);

    QGraphicsWidget *form = new QGraphicsWidget;
    form->setLayout(layout);
    scene.addItem(form);
    @

    but I get a compiling error:

    @
    error: cannot convert 'QGraphicsProxyWidget*' to 'QGraphicsWidget*' in assignment
    @

    and it refer to the line:

    @
    QGraphicsWidget *textEdit = scene.addWidget(new QTextEdit);
    @

    What is the problem?



  • add

    @#include <QtGui>@

    or

    specifically you are missing:

    @#include <QtGui/QGraphicsProxyWidget>@

    Below find full working example
    @
    #include <QtGui/QApplication>
    #include <QtGui/QGraphicsProxyWidget>
    #include <QtGui/QGraphicsScene>
    #include <QtGui/QTextEdit>
    #include <QtGui/QPushButton>
    #include <QtGui/QGraphicsGridLayout>
    #include <QtGui/QGraphicsView>

    int main(int argc, char **argv)
    {
    QApplication app(argc, argv);

    QGraphicsScene scene;
    QGraphicsWidget *textEdit = scene.addWidget(new QTextEdit);
    QGraphicsWidget *pushButton = scene.addWidget(new QPushButton);

    QGraphicsGridLayout *layout = new QGraphicsGridLayout;
    layout->addItem(textEdit, 0, 0);
    layout->addItem(pushButton, 0, 1);

    QGraphicsWidget *form = new QGraphicsWidget;
    form->setLayout(layout);
    scene.addItem(form);

    QGraphicsView view(&scene);
    view.show();

    return app.exec();
    }

    @



  • Ok, thanks and sorry for the stupid question, it solved... :-)

    Now I'm trying to put my pixmap in a QLabel and the use the same way of the example.

    I'll tell you if it works...



  • QLabel with QPixmaps laied out with QGraphicsGridLayout would do the trick and would definitely work. Threre is not need to do QGraphicsPixmapItem.



  • The new solution works!

    @
    QGraphicsGridLayout *ggl = new QGraphicsGridLayout;
    QPixmap pixmap1;
    QPixmap pixmap2;
    ...
    QLabel *pixmap_widget = new QLabel;
    pixmap_widget->setPixmap(pixmap1);
    QGraphicsProxyWidget *pixmap_graphics_widget1 = graphicsScene->addWidget(pixmap_widget);
    pixmap_widget = new QLabel;
    pixmap_widget->setPixmap(pixmap2);
    QGraphicsProxyWidget *pixmap_graphics_widget2 = graphicsScene->addWidget(pixmap_widget);
    ggl->addItem(pixmap_graphics_widget1,0,0);
    ggl->addItem(pixmap_graphics_widget2,0,1);
    QGraphicsWidget *form = new QGraphicsWidget;
    form->setLayout(ggl);
    graphicsScene->addItem(form);
    @



  • Thanks for your help!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.