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();