[SOLVED] Widget I can draw on.

  • Hello,
    I want to use widget that I could draw on and use it with Qt Designer/Qt Creator together. Do I have to write it on my own and implement paintEvent or is there any easier way? Maybe there is widget with "repainted()" signal implemented or something.

    I don't want to create widget dinamically because it could make problem with layout.

    PS: Sorry for my english.

  • bq. I want to use widget that I could draw on and use it with Qt Designer/Qt Creator together.

    How you plan to draw? Do you mean interactive drawing using mouse?

  • No. In my case I want to display an Image from QImage object. Maybe write some text from QString in addition. That's all.

  • Then you need write a Qt Designer plug-in

    Oh well, wait…
    If it is simple QImage and some QString, you could use QStackedWidget in Qt Designed it self

  • <merged with earlier post>

  • Could I? How? QImage does not inherits from QWidget...

  • You can set the background image using stylesheets

  • And if I want to create an animation by showing QImage's one-by-one?

  • For animations you can have a look at "QPropertyAnimation Class":http://qt-project.org/doc/qt-5.0/qtcore/qpropertyanimation.html

  • :/
    I can not imagine how should it work. I can't even display simple image loaded from file. So which property would I animate. On which widget?

    Ohh, I am afraid I will have to write my own widget indeed. But that's strange for me as I have noticed that Qt liblary is well-equipped compared to wxWidgets, where wxStaticBitmap exists.

    PS: Sorry for my English again. Is it hard to understand what I write? :D

  • Moderators

    For static images you can use simple QLabel and QLabel::setPixmap. For animation there's a QLabel::setMovie. You could play around with a custom QIODevice for the QMovie.

  • There is an example available in Qt creator "Basic Drawing Example":http://doc.qt.digia.com/stable/painting-basicdrawing.html and also for basic reference you can have a look at this "tutorial":http://www.voidrealms.com/viewtutorial.aspx?id=35. In order to set a background image you can use "Qt StyleSheets":http://qt-project.org/doc/qt-4.8/stylesheet-examples.html

    There are many tutorials available at "wwww.voidrealms.com":http://www.voidrealms.com/tutorials.aspx?filter=qt based on Qt 4 which are helpful for a start.


  • I have already seen the example. It is based on writing own widget, but I am asking about different way (I have written the reason earlier). Maybe Krzysztof's way is satisfactory, I will try.

    In the midtime I found a solution using QGraphicsView, which unfortunately doesn't work. My code:
    QImage Image("/path/to/image.png");
    QPixmap Pixmap;
    QGraphicsPixmapItem Item(Pixmap);
    QGraphicsScene *Scene = ui->GraphicsView->scene();
    The code compiles, but I receive an error during execution. In english it would be "Unexpected program termination" or sth. like that.

    Ok, Krzysztof's way is what I was looking for. About QMovie, I briefly checked this class documentation and I didn't notice the way to make an animation using several QImage's or QPixmap's. I will be thinking about it deeper tommorow. Is it better way than simple replacing images in a thread? Maybe simpler?

  • QGraphicsPixmapItem is created on stack, are you sure that it will remain in scope for all the time during scene is in scope. Instead you could create it on heap (using new)

  • No, the problem is different. I have already tried this way:
    QImage *Image = new QImage("/path/a.png");
    QPixmap *Pixmap = new QPixmap;
    QGraphicsPixmapItem *Item = new QGraphicsPixmapItem(*Pixmap);
    QGraphicsScene *Scene = ui->GraphicsView->scene();
    And the result is the same.
    The error-generating line is the last one.

  • Then I have say that the view does not have scene set, what deos ui->GraphicsView->scene(); return?

  • Of course.
    So there are two ways:

    using QGraphicsView:
    QImage *Image = new QImage("/home/foo/a.png");
    QPixmap *Pixmap = new QPixmap;
    QGraphicsPixmapItem *Item = new QGraphicsPixmapItem(*Pixmap);
    QGraphicsScene *Scene = new QGraphicsScene;

    or using QLabel
    QImage Image("/home/foo/a.png");
    QPixmap Pixmap;

    And this thread is SOLVED.
    Thanks a lot and deep Greathings.

Log in to reply

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