PDA

View Full Version : MFC Program in QT



sabeesh
13th December 2007, 12:32
Hi,
I need to create a program, which one capture data from web camera using Qt 4.3.0 with Visual Studio 2005 ( VC ++ ) ( MFC program ). Can you help me for that? Please give a link to some examples for that.
Please help me

high_flyer
13th December 2007, 12:41
Amm... I think you are mixing things...
Do you mean you want to write an MFC program with visual studio, or do you mean you want to program a Qt application with the visual studio IDE ?
I guess its the later.
If so, please state the exact problem you need help with.

sabeesh
14th December 2007, 03:42
Hi,
My probs is that, I need to create a program in Qt 4.3.0 and compile it using Visual Studio 2005 ( VC++ only). Through that program, I need to create a screen for capture images from a web camera. So I think about MFC. If any other way to create it, Please give me the tutorial for that.
Please help me.

pherthyl
14th December 2007, 07:58
Hi,
My probs is that, I need to create a program in Qt 4.3.0 and compile it using Visual Studio 2005 ( VC++ only). Through that program, I need to create a screen for capture images from a web camera. So I think about MFC. If any other way to create it, Please give me the tutorial for that.
Please help me.

How would MFC help you? The window you can do with Qt. To get the webcam data you'll have to look at something like OpenCV. There might be other webcam access libs on windows but I've only used OpenCV so I don't know.

high_flyer
14th December 2007, 09:41
Hi,
My probs is that, I need to create a program in Qt 4.3.0 and compile it using Visual Studio 2005 ( VC++ only). Through that program, I need to create a screen for capture images from a web camera. So I think about MFC. If any other way to create it, Please give me the tutorial for that.
Please help me.

If you say you need to create a Qt application, why are you thinking of MFC?
Do you understand what MFC and Qt are?
If it is a requirement for you to use Qt, you don't need MFC.
On our wiki (http://wiki.qtcentre.org/index.php?title=Qt4_with_Visual_Studio)you can learn how to integrate Qt4 and MSVC, unless you are a commercial user of Qt, in that case you get a nice integration in to MSVC it self.

steg90
14th December 2007, 09:45
I think it is because he is using Visual Studio 2005 and that obviously comes with the MFC libraries. I use visual studio 2005 with a commercial version of QT which integrates into this environment.

high_flyer
14th December 2007, 09:48
I think it is because he is using Visual Studio 2005 and that obviously comes with the MFC libraries.
Yes, but that doesn't mean he should use them.
I think he does not understand what Qt and MFC are - look at the subject of the thread...

steg90
14th December 2007, 09:52
All depends on what he is comfortable with. I came from a background of having used MFC for 8 years and then recently moved to QT. The team I use to work with were all MFC developers and hence we stuck with it. Of course now, I am finding the switch to QT proving to be so much nicer and it helps me a lot more than MFC and also, this great site where people like yourself offer advice and help :D

high_flyer
14th December 2007, 09:59
All depends on what he is comfortable with.
I think requirements come before the comfort, and he did state:

My probs is that, I need to create a program in Qt 4.3.0 and compile it using Visual Studio 2005 ( VC++ only)
MFC or Qt both are fine.
I find mixing is not good, unless there is no way with out it, which doesn't look to be the case here, since he is starting a new application.

steg90
14th December 2007, 10:08
In my experience of working for various companies, most of them won't give you the time to learn a new library unless their budget covers it - but hey, I may have worked for some 'bad' companies?!

Requirements do play a BIG part of course and if certain requirements cannot be met using a certain library then obviously you look at alternatives.

Mixing MFC with Qt in certain circumstances you have to, although as you point out, this is a new application so I agree, don't mix them, using one or the other is enough.

Compiling a project in Visual Studio 2005 that uses Qt is simple with Qt VS-Integration, although you do need a commercial version of Qt.

Why does it need to be compiled using VS2005?

sabeesh
14th December 2007, 11:21
Do you have any examples with OpenCV in Qt.
Please give the link.

high_flyer
14th December 2007, 11:24
OpenCV (http://opencvlibrary.sourceforge.net/) is an external lib.
There is nothing special about using it with Qt, use it just as you would in any other application.

pherthyl
16th December 2007, 22:10
You should look at this: http://opencvlibrary.sourceforge.net/CameraCapture

Instead of using cvShowImage, you would copy the data from the IPLImage and create a QImage to show in your Qt application.

sabeesh
17th December 2007, 09:34
Hi,
Thankyou for your reply,
I use that program. For convert the Image from IplImageToQImage to QImage, I use the function http://www.bernardotti.it/portal/showthread.php?t=597 . But It return *QImage. How can I display it?
Sorry, Because I am not an expert in Qt. Please help me...

sabeesh
17th December 2007, 11:40
Hi,
I have a form in Qt 4.3 and through my program I create a QImage. I create a function from create this QImage and that function return *QImage and it store in another *QImage. How Can I display it in my from. I want to display this *QImage as a widget in my form. How can I do this?
Please help me

jacek
17th December 2007, 11:44
The easiest way is to convert that QImage to QPixmap and use QLabel.

Please, don't ask the same question twice.

sabeesh
17th December 2007, 12:31
Hi,
I do that like this,

QImage *ImageVid;
IplImage* frame = cvQueryFrame( capture );

ImageVId = IplImageToQImage(frame);


and the function is this, It return QImage *. How Can I display this QImage * in a Label, through QLabel->setPixmap().
Because the property is " setPixmap ( const QPixmap & )"



QImage * myQtApp::IplImageToQImage(const IplImage * iplImage) {

uchar *qImageBuffer = NULL;
int width = iplImage->width;


int widthStep = iplImage->widthStep;
int height = iplImage->height;

switch (iplImage->depth) {
case IPL_DEPTH_8U:

if (iplImage->nChannels == 1) {

// OpenCV image is stored with one byte grey pixel. We convert it
// to an 8 bit depth QImage.

qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
uchar *QImagePtr = qImageBuffer;
const uchar *iplImagePtr = (const uchar *) iplImage->imageData;

for (int y = 0; y < height; y++) {
// Copy line by line
memcpy(QImagePtr, iplImagePtr, width);
QImagePtr += width;
iplImagePtr += widthStep;
}

} else if (iplImage->nChannels == 3) {

// OpenCV image is stored with 3 byte color pixels (3 channels).
// We convert it to a 32 bit depth QImage.


qImageBuffer = (uchar *) malloc(width*height*4*sizeof(uchar));
uchar *QImagePtr = qImageBuffer;
const uchar *iplImagePtr = (const uchar *) iplImage->imageData;

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// We cannot help but copy manually.
QImagePtr[0] = iplImagePtr[0];
QImagePtr[1] = iplImagePtr[1];
QImagePtr[2] = iplImagePtr[2];
QImagePtr[3] = 0;
QImagePtr += 4;
iplImagePtr += 3;
}

iplImagePtr += widthStep-3*width;
}

} else {
qDebug("IplImageToQImage: image format is not supported : depth=8U and %d channels\n", iplImage->nChannels);

}

break;
default:
qDebug("IplImageToQImage: image format is not supported : depth=%d and %d channels\n", iplImage->depth, iplImage->nChannels);

}

QImage *qImage;
QImage qImag;
if (iplImage->nChannels == 1) {
QRgb *colorTable = new QRgb[256];

for (int i = 0; i < 256; i++)
colorTable[i] = qRgb(i, i, i);
qImage = new QImage(qImageBuffer, width, height, QImage::Format_RGB32);
QImage qImag(qImageBuffer, width, height, QImage::Format_RGB32 );

} else {
qImage = new QImage(qImageBuffer, width, height, QImage::Format_RGB32);
QImage qImag(qImageBuffer, width, height, QImage::Format_RGB32 );
}

return qImage;

}

Please help me to solve this probs...

jacek
17th December 2007, 12:49
Because the property is " setPixmap ( const QPixmap & )"
You have to convert QImage to QPixmap first. Check static methods of QPixmap in the docs.



...
QImage *qImage;
QImage qImag;
if (iplImage->nChannels == 1) {
QRgb *colorTable = new QRgb[256];

for (int i = 0; i < 256; i++)
colorTable[i] = qRgb(i, i, i);
qImage = new QImage(qImageBuffer, width, height, QImage::Format_RGB32);
QImage qImag(qImageBuffer, width, height, QImage::Format_RGB32 );

} else {
qImage = new QImage(qImageBuffer, width, height, QImage::Format_RGB32);
QImage qImag(qImageBuffer, width, height, QImage::Format_RGB32 );
}

return qImage;
}
What do you need qImag and colorTable for?

sabeesh
18th December 2007, 05:42
Hi,
I use that link ( http://opencvlibrary.sourceforge.net/CameraCapture ) for Qt. I face two probs in my program.
1. How can avoid cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
Because in Qt, I have a window and I need to display the image in that window. When I comment this line then the Image is not display in my Qt Widget.

2. When I use this function ( http://groups.google.com/group/OpenCV/browse_thread/thread/adecb85d719954e6/ee022f60542be011?lnk=st&q=opencv+qt&rnum=2 ) for copy IplImage To QImage, i can copy and display the image. But the image is display as 90 degree rotated. How can I solve these two probs.

Please Help me...

sabeesh
19th December 2007, 03:38
Hi,
No reply?????????????
:(

high_flyer
19th December 2007, 10:10
Hi,
No reply?????????????
No patience????????

1. just don't use that function, you don't need it.
You are using Qt, so Qt provides you with windows (widgets).
You stated you can display the image already with QImage/QPixmap, so you are all set.

2.have a looks at: http://doc.trolltech.com/4.3/qpainter.html#coordinate-transformations

sabeesh
21st December 2007, 08:59
Hi,
This is the code which one I use for display the image


QImage *ReturnImg;
CvCapture* capture = cvCaptureFromCAM(0);
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );

if( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
}
IplImage* img = 0;
while( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
ReturnImg = IplImageToQImage(frame);
Picture->setPixmap(QPixmap::fromImage(*ReturnImg));
if( (cvWaitKey(10) & 255) == 27 ) break;
}

and the function is


QImage* myQtApp::IplImageToQImage(const IplImage * iplImage) {
uchar *qImageBuffer = NULL;
int width = iplImage->width;
int widthStep = iplImage->widthStep;
int height = iplImage->height;
switch (iplImage->depth) {
case IPL_DEPTH_8U:

if (iplImage->nChannels == 1) {
qDebug()<<"iiplImage->nChannels is "<< iplImage->nChannels;

// OpenCV image is stored with one byte grey pixel. We convert it to an 8 bit depth QImage.

qImageBuffer = (uchar *) malloc(width*height*sizeof(uchar));
uchar *QImagePtr = qImageBuffer;
const uchar *iplImagePtr = (const uchar *) iplImage->imageData;

for (int y = 0; y < height; y++) {
// Copy line by line
memcpy(QImagePtr, iplImagePtr, width);
QImagePtr += width;
iplImagePtr += widthStep;
}

} else if (iplImage->nChannels == 3) {
uchar *QImagePtr = qImageBuffer;
const uchar *iplImagePtr = (const uchar *) iplImage->imageData;

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// We cannot help but copy manually.
QImagePtr[0] = iplImagePtr[0];
QImagePtr[1] = iplImagePtr[1];
QImagePtr[2] = iplImagePtr[2];
QImagePtr[3] = 0;
QImagePtr += 4;
iplImagePtr += 3;
}

iplImagePtr += widthStep-3*width;
}

} else {
qDebug("IplImageToQImage: image format is not supported : depth=8U and %d channels\n", iplImage->nChannels);

}

break;
default:
qDebug("IplImageToQImage: image format is not supported : depth=%d and %d channels\n", iplImage->depth, iplImage->nChannels);

}

QImage *qImage;
if (iplImage->nChannels == 1) {
QRgb *colorTable = new QRgb[256];

for (int i = 0; i < 256; i++)
colorTable[i] = qRgb(i, i, i);
qImage = new QImage(qImageBuffer, width, height, QImage::Format_RGB32);
} else {

qImage = new QImage(qImageBuffer, width, height, QImage::Format_RGB32);
}
return qImage;
}


and It is working. But I face two problem in this program.

1. It display a new window. I want to avoid that window. For avoid that window I comment the line
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
But after comment the line the program doesn't display the captured image.

2. The image is in 90 degree rotated mode.

Please help me to solve this problem.

high_flyer
21st December 2007, 09:23
Well Sabeesh, this post is the same as the previous one, except you added the code.
The answer is also the same...

1. It display a new window. I want to avoid that window. For avoid that window I comment the line
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
But after comment the line the program doesn't display the captured image.
Again, since you chose to use Qt, you need to use a widget (Qt window) to display your image.
I take it that 'Picture' is a QLabel.
QLabel is a QWidget, so you could show it to see your image, for example by adding:


Picture->show();

just before the while loop.

This however is not the way to do things in a nice way.
QLabel is usually used as a child widget on a "real" QWidget, or a QDialog for exmaple.


2. The image is in 90 degree rotated mode.
See my answer to that point in my previous post.

It looks to me you are trying to jump in the deep waters before you learned how to swim.
Read the Qt docs first, make acquaintance with the Qt way of doing things, the Qt documentation is the best I have seen so far.

sabeesh
21st December 2007, 09:58
Hi,
Sorry high_flyer,

Ok, I modify my code like this,


QImage *ReturnImg;
CvCapture* capture = cvCaptureFromCAM(0);

//cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );

if( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
}
IplImage* img = 0;
Picture->show();
while( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
ReturnImg = IplImageToQImage(frame);
Picture->setPixmap(QPixmap::fromImage(*ReturnImg));

if( (cvWaitKey(10) & 255) == 27 ) break;
}

But, I did not get any output from my program. The Image is not display
Please help me

marcel
21st December 2007, 10:00
Maybe the image is empty? Have you tried getting some image info, like size, etc, to see if it is ok? Also, you can save the images as jpeg or png as a test.

sabeesh
21st December 2007, 10:18
Hi,
Ok, I add these line in the while loop

[ qDebug() << "Frame is "<< cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);

qDebug()<< "frameH "<< cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
qDebug()<< "posMsec "<< cvGetCaptureProperty(capture, CV_CAP_PROP_POS_MSEC);
qDebug()<< "posFrames " << cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES);
]

and Qt print the output like this,

Frame is 0
frameH 144
posMsec 0
posFrames 0


in both case. ( before and after comment the line cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE ); )

high_flyer
21st December 2007, 10:27
As far as I understand openCV docs, you should call cvQueryFrame() before you call cvGetCaptureProperty(), did you?

In addition, you are entering a while loop, that is not allowing the Qt GUI event loop to breath.
Try adding QApplication::processEvents() in you while loop.

sabeesh
21st December 2007, 11:17
Hi,
Thankyou, I add this line ( qApp->processEvents(); ) in wihe loop. Then the Image is display. Then, How can I rotate the image, because it is 90 degree rotated. Please help me for that

high_flyer
21st December 2007, 11:46
Then, How can I rotate the image, because it is 90 degree rotated. Please help me for that
For the third time:

2.have a look at:http://doc.trolltech.com/4.3/qpainter.html#coordinate-transformations

sabeesh
24th December 2007, 06:53
Hi,
Can you help me for create the QPainter from Qlabel and rotate it?
Please

high_flyer
24th December 2007, 09:14
I am trying to... but it seems you fail to read what I write...
Or, it order to help you we need to know what problem you have, but you are not giving any problem description...

sabeesh
24th December 2007, 11:46
Hi,
I try for rotate the label. But, It doesn't work. My program is attached with this. Please check it and help me to solve that problem. I want to display the image in "Picture" label in that program. Please help me.

high_flyer
24th December 2007, 12:22
I want to display the image in "Picture" label in that program. Please help me.
You are intializing two QLables, one QLabel 'Picture1', and one your subclass of it "Picture'.


myQtApp::myQtApp(QWidget *parent)
{
setupUi(this); // this sets up GUI

Picture= new MyLabel("", this);
Picture->setObjectName(QString::fromUtf8("LblPlay01"));
Picture->setGeometry(QRect(10, 10, 10, 100));
Picture->setText(QString("AAAA"));
Picture->setStatusTip(tr(" Play "));

Picture1= new QLabel("", this);
Picture1->setObjectName(QString::fromUtf8("LblPlay02"));
Picture1->setGeometry(QRect(130, 10, 100, 100));
Picture1->setText(QString("AAAA"));
Picture1->setStatusTip(tr(" Play "));

// signals/slots mechanism in action
connect( pushButton_do, SIGNAL( clicked() ), this, SLOT( doSomething() ) );

}

In your code you set the QImage in to the QLabel 'Picture1' and not 'Picture', yet your rotating code is in 'Picture', so no wonder your rotating code does not "work".


ReturnImg = IplImageToQImage(frame);
Picture1->setPixmap(QPixmap::fromImage(*ReturnImg));
Picture1->setPixmap(QPixmap::fromImage(*ReturnImg));
if( (cvWaitKey(10) & 255) == 27 ) break;


Now, before you go change 'Picutre1' to 'Picture' and return here and say it still doesn't work:
try and THINK your self about solving the problem.
READ the docs.
TRY various solutions.
And ask SPECIFIC questions.
You can't expect us to spoon feed you everything, or to expect to send us your whole project and let us find your errors with out being specific about the problem you have.

We can help you with problems, but we can't think for you, or do your work for you!

high_flyer
24th December 2007, 12:32
I just checked QImage docs (which you could have done as well!) and noticed QImage has its own api for transformations:
QImage::transformed
This will save you the need to subclass and do it with the QPainter.
The transformation example (http://doc.trolltech.com/4.3/painting-transformations.html) might also help in understanding how to do transformations.

sabeesh
26th December 2007, 09:10
Hi,
Thank you for your help.
I use this code for rotate my image.
QImage hflip = ReturnImg->transformed(QMatrix().scale(1,-1));
Picture1->setPixmap(QPixmap::fromImage(hflip));
I am not sure, It is a good method. If any problem, Please inform me.
Thankyou