I m using Qt 4.6.2 on windows xp
I need to draw horizontal ,vertical lines and an arc over the image .
My code isCode:
But only image is displayed ,no line is drawn....
Printable View
I m using Qt 4.6.2 on windows xp
I need to draw horizontal ,vertical lines and an arc over the image .
My code isCode:
But only image is displayed ,no line is drawn....
you can only draw in a paintEvent().
You just need to setup your pixmap before you assign the painter:
Code:
Joh
@Joh,
did you try the code you posted?
This code should not work.
At the very least in runtime a Qt warning should be printed, saying that QPainter can not be used outside the paintEvent().
Sure I tried it! That's why I posted it as a complete application.
It doesn't show any warnings on the console, either. Why should it not be possible to use a QPainter on an 'offline' QPixMap?
Joh
you are right, QPixmap is not a widget.
Sorry.
Aah! Allright! That was the source of the confusion!
Yes, and not beeing able to draw onto a widget outside the paintEvent makes perfect sense.
Hope we didn't confuse the OP :->
Joh
I m still not able to draw line on image
My code is
Code:
QPixmap pixmap; ui->lbl_plane->resize(pixmap.size()); painter.setPen(GreenPen); painter.drawLine(250,300,500,300); ui->lbl_plane->setPixmap(pixmap); ui->lbl_plane->show();
The image format is "Format_RGB32"
what I m missing?
give your pixmap a size.
I have added line which is my image size to above code but still no line is drawn on image....
try adding painter.end() after the drawLine().
Try the following code.
Its works I tested it.
Once it works for you see what it is you are doing wrong:
It works for me also ...
But only if I dont add image to pixmap.
as soon as I add image it shows only image no red line..
show your full code.
Doorcontrol1 is a QDialog in which I need to show that image and lines.
main.cppCode:
#include <QtGui/QApplication> #include <QtCore> #include "bifold.h" int main(int argc, char *argv[]) { BiFold w; w.show(); return a.exec(); }
bifold.h
Code:
#ifndef BIFOLD_H #define BIFOLD_H #include <QMainWindow> namespace Ui { class BiFold; } class DoorControl1; Q_OBJECT public: ~BiFold(); DoorControl1 *ODoorControl1; protected: private: Ui::BiFold *ui; }; #endif // BIFOLD_H
bifold.cpp
Code:
#include "bifold.h" #include "ui_bifold.h" #include "doorcontrol1.h" #include <QMessageBox> ui(new Ui::BiFold) { ui->setupUi(this); removeToolBar(ui->mainToolBar); //Removes tool bar from main window ODoorControl1 = new DoorControl1(this); ODoorControl1->setWindowFlags( Qt::FramelessWindowHint ); } BiFold::~BiFold() { delete ui; } { switch (e->type()) { ui->retranslateUi(this); break; default: break; } }
doorcontrol1.h
Code:
#ifndef DOORCONTROL1_H #define DOORCONTROL1_H #include <QDialog> namespace Ui { class DoorControl1; } Q_OBJECT public: ~DoorControl1(); void DrawStuff(); protected: private: Ui::DoorControl1 *ui; }; #endif // DOORCONTROL1_H
doorcontrol1.cppCode:
#include <QtGui> #include <QtCore> #include <QMessageBox> #include "doorcontrol1.h" #include "ui_doorcontrol1.h" ui(new Ui::DoorControl1) { ui->setupUi(this); DrawStuff(); } DoorControl1::~DoorControl1() { delete ui; } { switch (e->type()) { ui->retranslateUi(this); break; default: break; } } { } void DoorControl1::DrawStuff() { ui->lbl_plane->resize(pixmap.size()); painter.setPen(Red); painter.drawLine(250,300,500,300); painter.end(); ui->lbl_plane->setPixmap(pixmap); ui->lbl_plane->show(); }
Can't you put your code into something simpler? Just one class, and without an UI-File, that you didn't provide!
Reducing complexity often helps to find problems in the first place, so you might even solve it yourself!
Johannes
PS: "Cross posting" meant, that you posted, while I was writing my message. After I posted mine.. I saw your new message and changed mine .. to "cross posting". After I read your message, I edited again :->
what is mean by "Cross posting :-> " ?????
you reimplemented the paintEvent() in your dialog, and left it blank.
So your dialog does not do any painting,I am not sure if this will cause its children not to draw as well.
Delete the paintEvent() re implementation and see if this helps.
I have removed reimplementation of paintEvent () from header as well as source file
but still no line is drawn on image .....
in BiFold construcotr add:
ODoorControl1->show();
It's already showing DoorControl1 Dialog with Plane's image...
still as you said I added ODoorControl1->show(); in Bifold constructor ,but no line still drawn..
you have to simplify and go step at a time.
Put your dialog in main, and show it.
See if you get the line drawn then.
I have added draw line code in main.cpp itself but still it only shows image and no line is drawn
main.cppCode:
#include <QtGui/QApplication> #include <QtCore> #include <QtGui> #include <QLabel> //#include "bifold.h" int main(int argc, char *argv[]) { // BiFold w; // w.show(); QLabel l; l.resize(pixmap.size()); painter.setPen(Red); painter.drawLine(250,300,500,300); painter.end(); l.setPixmap(pixmap); l.show(); return a.exec(); }
Is this a fault of my image,but I tried changing the image still no line is drawn..
I read in documentation of QPainter "Painting on a QImage with the format QImage::Format_Indexed8 is not supported."
But my image format is QImage::Format_RGB32.
I m not able to upload image here,it says "upload failed"
pls show me right direction ,I cannot proceed my work if this issues is not solved.
Are you sure that your resource file path is good?
Your code with some minor tweaks and a local file:
Input is:Code:
#include <QtCore> #include <QtGui> #include <QDebug> int main(int argc, char *argv[]) { QLabel l; QPixmap pixmap; qDebug() << goodLoad << pixmap.isNull() << pixmap.size() << pixmap.depth(); painter.setPen(Red); painter.drawLine(40, 40, 200, 280); painter.end(); pixmap.save("./test2.png"); l.setPixmap(pixmap); l.show(); return app.exec(); }
Attachment 4840
Output is:
and
Attachment 4839
Edit: the forum converted the PNG files to JPG
Finally the line is visible over the image
The co-ordinates I used for drawing line were main culprit here
painter.drawLine(250,300,500,300);
if I use ur co-ordinate,Code:
also qDebug() << goodLoad << pixmap.isNull() << pixmap.size() << pixmap.depth(); shows
painter.drawLine(40, 40, 200, 280);
line is drawn.....
My next query is
Can we directly paint QDialog ?
I need to draw some arcs, squares and lines on QDialog
You can paint on any widget, but only in a paintEvent().
hai to every one am new to qt actually am trying to draw line on image (qpixmap),i had tried all coeds u have given above but still i didnt get the result plz can any one give full code to me thanking you and sorry for my bad english
How to drag that line over the image?
I tried to drag. But it does not update.