[SOLVED] Widget I can draw on.
-
wrote on 21 Feb 2013, 17:59 last edited by
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.
-
wrote on 21 Feb 2013, 19:01 last edited by
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?
-
wrote on 21 Feb 2013, 19:36 last edited by
No. In my case I want to display an Image from QImage object. Maybe write some text from QString in addition. That's all.
-
wrote on 21 Feb 2013, 19:56 last edited by
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 -
wrote on 21 Feb 2013, 19:57 last edited by
<merged with earlier post>
-
wrote on 21 Feb 2013, 20:15 last edited by
Could I? How? QImage does not inherits from QWidget...
-
wrote on 21 Feb 2013, 20:18 last edited by
You can set the background image using stylesheets
-
wrote on 21 Feb 2013, 20:22 last edited by
And if I want to create an animation by showing QImage's one-by-one?
-
wrote on 21 Feb 2013, 20:30 last edited by
For animations you can have a look at "QPropertyAnimation Class":http://qt-project.org/doc/qt-5.0/qtcore/qpropertyanimation.html
-
wrote on 21 Feb 2013, 21:03 last edited by
:/
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
-
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.
-
wrote on 21 Feb 2013, 21:23 last edited by
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.
Regards
Soumitra -
wrote on 21 Feb 2013, 21:57 last edited by
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;
Pixmap.convertFromImage(Image);
QGraphicsPixmapItem Item(Pixmap);
QGraphicsScene *Scene = ui->GraphicsView->scene();
Scene->addItem(&Item);
@
The code compiles, but I receive an error during execution. In english it would be "Unexpected program termination" or sth. like that.
EDIT:
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? -
wrote on 22 Feb 2013, 08:44 last edited by
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)
-
wrote on 22 Feb 2013, 09:58 last edited by
No, the problem is different. I have already tried this way:
@
QImage *Image = new QImage("/path/a.png");
QPixmap *Pixmap = new QPixmap;
Pixmap->convertFromImage(*Image);
QGraphicsPixmapItem *Item = new QGraphicsPixmapItem(*Pixmap);
QGraphicsScene *Scene = ui->GraphicsView->scene();
Scene->addItem(Item);
@
And the result is the same.
The error-generating line is the last one. -
wrote on 22 Feb 2013, 10:34 last edited by
Then I have say that the view does not have scene set, what deos ui->GraphicsView->scene(); return?
-
wrote on 22 Feb 2013, 11:12 last edited by
Of course.
So there are two ways:using QGraphicsView:
@
QImage *Image = new QImage("/home/foo/a.png");
QPixmap *Pixmap = new QPixmap;
Pixmap->convertFromImage(*Image);
QGraphicsPixmapItem *Item = new QGraphicsPixmapItem(*Pixmap);
QGraphicsScene *Scene = new QGraphicsScene;
ui->GraphicsView->setScene(Scene);
Scene->addItem(Item);
@or using QLabel
@
QImage Image("/home/foo/a.png");
QPixmap Pixmap;
Pixmap.convertFromImage(Image);
ui->label->setPixmap(Pixmap);
@And this thread is SOLVED.
Thanks a lot and deep Greathings.
1/17