PDA

View Full Version : no compile error, error on run



sincnarf
18th September 2007, 07:40
I can compile my project with no error, but a windows error (send report, end task) suddenly appears when the program executes this function.

By the way, I am trying to resize a loaded Image to the scene's size keeping the image's aspect ratio. any other suggestions would be nice.



void MainWindow::loadGraphicsViewParameter(QGraphicsVie w *graphicsView)
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), QDir::currentPath());
if (!fileName.isEmpty())
{
QImage tempImage(fileName);
if (tempImage.isNull())
{
QMessageBox::information(this, tr("Load Warning"),
tr("Cannot load %1.").arg(fileName));
return;
}
QImage image = tempImage.convertToFormat(QImage::Format_ARGB32);
QGraphicsScene *scene = new QGraphicsScene;
QGraphicsScene* viewScene = graphicsView->scene();
QPixmap pixmap = QPixmap::fromImage(image);

scene->addPixmap(pixmap.scaled(QSize((int)viewScene->width(), (int)viewScene->height()), Qt::KeepAspectRatio, Qt::SmoothTransformation));

qDebug() << "Width = " << viewScene->width();
qDebug() << "Height = " << viewScene->height();

graphicsView->setScene(scene);
graphicsView->show();
}
}

marcel
18th September 2007, 07:57
I don't think this works:


QGraphicsScene *scene = new QGraphicsScene;

It is unlikely to be able to add items to a scene without a view. It's view is NULL and I am sure it tries to use it somewhere.

Try setting the scene to the view immediately after creating scene.

sincnarf
18th September 2007, 08:17
how do I determine if the passed graphicsView and its graphicsScene is null?

i'm looking for something like a view.isNull() method and view->scene().isNull method

marcel
18th September 2007, 08:23
But I already told you. The solution is to setScene right after you create it. Nothing more.
Anyway, if you want to test the view of a scene use QGraphicsScene::views(). It returns a QList of QGraphicsView. In your case it should be empty.

sincnarf
18th September 2007, 09:09
void MainWindow::loadGraphicsViewParameter(QGraphicsVie w *graphicsView)
{

if (graphicsView == 0)
{
QMessageBox::warning(this, tr("Load Error"), tr("Graphics View is NULL"));
return;
}
QGraphicsScene *scene = graphicsView->scene();
QGraphicsView *view = graphicsView;
view->setScene(scene);
if (scene->views().size() == 0)
{
QMessageBox::warning(this, tr("Load Error"), tr("GraphicsView's GraphicsScene is NULL"));
return;
}
}


and this


void MainWindow::loadGraphicsViewParameter(QGraphicsVie w *graphicsView)
{

if (graphicsView == 0)
{
QMessageBox::warning(this, tr("Load Error"), tr("Graphics View is NULL"));
return;
}
if ( graphicsView->scene()->views().size() == 0)
{
QMessageBox::warning(this, tr("Load Error"), tr("GraphicsView's GraphicsScene is NULL"));
return;
}
}


causes the same windows error

marcel
18th September 2007, 09:39
void MainWindow::loadGraphicsViewParameter(QGraphicsVie w *graphicsView)
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), QDir::currentPath());
if (!fileName.isEmpty())
{
QImage tempImage(fileName);
if (tempImage.isNull())
{
QMessageBox::information(this, tr("Load Warning"),
tr("Cannot load %1.").arg(fileName));
return;
}
QImage image = tempImage.convertToFormat(QImage::Format_ARGB32);
QGraphicsScene *scene = new QGraphicsScene;
QGraphicsScene* viewScene = graphicsView->scene();
float w = viewScene->width();
float h = viewScene->height();

graphicsView->setScene(scene);
delete viewScene;

QPixmap pixmap = QPixmap::fromImage(image);
scene->addPixmap(pixmap.scaled(QSize((int)w, (int)h, Qt::KeepAspectRatio, Qt::SmoothTransformation));

qDebug() << "Width = " << w;
qDebug() << "Height = " << h;

graphicsView->show();
}
}

sincnarf
18th September 2007, 12:07
didn't work. . . same windows error problem

wysota
18th September 2007, 12:11
How about... using a debugger? :)

sincnarf
18th September 2007, 15:47
what's a debugger? I'm using eclipse CDT plugin and I don't know how to debug this

wysota
18th September 2007, 16:30
Debugger is a tool that let's you inspect the developped application while it's running. Among other things, it would tell you where exactly your application crashes. If you're using MinGW as a compiler using GDB for debugging is the best option.

sincnarf
19th September 2007, 09:21
GDB says something like this... I can't understand...

Program received signal SIGSEGV, Segmentation fault.
0x00b2b2be in ZNK14QGraphicsScene9sceneRectEv ()
(gdb) backtrace
#0 0x00b2b2be in ZNK14QGraphicsScene9sceneRectEv ()
#1 0x00407453 in ?? ()
#2 0x0022b00c in ?? ()
#3 0x00000000 in ?? ()

jpn
19th September 2007, 09:24
Try compiling the application in debug mode:


make clean
qmake -config debug
make

sincnarf
19th September 2007, 10:14
Try compiling the application in debug mode:

Sorry but what;s next?

anyway here's additional debugging info.


Program received signal SIGSEGV, Segmentation fault.
0x00fa8bc2 in QGraphicsScene::d_func (this=0x0) at graphicsview//qgraphicsscene.h:237
237 Q_DECLARE_PRIVATE(QGraphicsScene)


The line Q_DECLARE_PRIVATE(QGraphicsScene) obviously has an error using my function. but what does this mean? Anyway, I am out of ideas of how I can load an Image to a QGraphicsView and automatically scale that Image based on the GraphicsView's size.. keeping aspectratio. Maybe there are other ways to do this

All the modifications to my function all have the same error. :(

wysota
19th September 2007, 10:20
Show us the backtrace after compiling in debug mode. There is nothing obvious here :)

By the way - do you need to use graphics view or do you just want to display an image? If the latter then use QLabel instead. If you insist on using the graphics view then create a scene with size equal to the image size, add the image to the scene and tell the view to scale the scene using fitInView().

sincnarf
19th September 2007, 11:09
Show us the backtrace after compiling in debug mode. There is nothing obvious here :)

here's the backtrace



#0 0x00fa8bc2 in QGraphicsScene::d_func (this=0x0) at graphicsview//qgraphicsscene.h:237
#1 0x00e71695 in QGraphicsScene::sceneRect (this=0x0) at graphicsview/qgraphicsscene.cpp:1048
#2 0x0043369a in QGraphicsScene::width (this=0x0) at ../../Qt/4.3.1/include/QtGui/../../src/gui/graphicsview/qgraphicsscene.h:118
#3 0x0040ac7b in MainWindow::loadGraphicsViewParameter (this=0x22fbe0, graphicsView=0x4232d98) at mainwindow.cpp:828
#4 0x0040b17d in MainWindow::loadGraphicsViewParameter1 (this=0x22fbe0) at mainwindow.cpp:880
#5 0x00412d5f in MainWindow::qt_metacall (this=0x22fbe0, _c=InvokeMetaMethod, _id=35, _a=0x22b904) at debug/moc_mainwindow.cpp:187
#6 0x10119529 in QMetaObject::activate (sender=0x4250a68, from_signal_index=29, to_signal_index=30, argv=0x22b904) at kernel/qobject.cpp:3080
#7 0x10119950 in QMetaObject::activate (sender=0x4250a68, m=0x1049910, from_local_signal_index=2, to_local_signal_index=3, argv=0x22b904) at kernel/qobject.cpp:3159
#8 0x00eadd7f in QAbstractButton::clicked (this=0x4250a68, _t1=false) at tmp/moc/debug_shared/moc_qabstractbutton.cpp:180
#9 0x00c0105e in QAbstractButtonPrivate::emitClicked (this=0x4250708) at widgets/qabstractbutton.cpp:532
#10 0x00c00f79 in QAbstractButtonPrivate::click (this=0x4250708) at widgets/qabstractbutton.cpp:525
#11 0x00c0296c in QAbstractButton::mouseReleaseEvent (this=0x4250a68, e=0x22c1bc) at widgets/qabstractbutton.cpp:1102
#12 0x008c789f in QWidget::event (this=<incomplete type>, event=0x22c1bc) at kernel/qwidget.cpp:6080
#13 0x00c0281e in QAbstractButton::event (this=0x4250a68, e=0x22c1bc) at widgets/qabstractbutton.cpp:1064
#14 0x00cc278c in QPushButton::event (this=0x4250a68, e=0x22c1bc) at widgets/qpushbutton.cpp:667
#15 0x0087654d in QApplicationPrivate::notify_helper (this=0x3d4bb8, receiver=0x4250a68, e=0x22c1bc) at kernel/qapplication.cpp:3558
#16 0x00874e1a in QApplication::notify (this=0x22fdf0, receiver=0x4250a68, e=0x22c1bc) at kernel/qapplication.cpp:3257
#17 0x100ffc84 in QCoreApplication::notifyInternal (this=0x22fdf0, receiver=0x4250a68, event=0x22c1bc) at kernel/qcoreapplication.cpp:532
#18 0x00ef6504 in QCoreApplication::sendSpontaneousEvent (receiver=0x4250a68, event=0x22c1bc) at ../../include/QtCore/../../src/corelib/kernel/qcoreapplication.h:205
#19 0x008dad26 in QETWidget::translateMouseEvent (this=0x4250a68, msg=@0x22d9bc) at kernel/qapplication_win.cpp:2774
#20 0x008d56b1 in QtWndProc (hwnd=0x504be, message=514, wParam=0, lParam=786451) at kernel/qapplication_win.cpp:1407
#21 0x77d48709 in USER32!GetDC () from C:\WINDOWS\system32\user32.dll




By the way - do you need to use graphics view or do you just want to display an image? If the latter then use QLabel instead. If you insist on using the graphics view then create a scene with size equal to the image size, add the image to the scene and tell the view to scale the scene using fitInView().

Yes, I really need QGraphicsView. Testing your recommendation...

wysota
19th September 2007, 11:17
Look here:

#3 0x0040ac7b in MainWindow::loadGraphicsViewParameter (this=0x22fbe0, graphicsView=0x4232d98) at mainwindow.cpp:828
loadGraphicsViewParameter() is called and inside it:

#2 0x0043369a in QGraphicsScene::width (this=0x0) at ../../Qt/4.3.1/include/QtGui/../../src/gui/graphicsview/qgraphicsscene.h:118
You fetch the width of the scene, but look at the value of "this" - it's null. It means that the scene is invalid.

After looking at your code you may notice that you call width() on the "viewScene" which is obviously null.

sincnarf
19th September 2007, 14:53
At last the windows error is gone BUT I still can't get the scene/image to fit in the view using this



void MainWindow::loadGraphicsViewParameter(QGraphicsVie w *graphicsView)
{

QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), QDir::currentPath());
if (!fileName.isEmpty())
{
QImage tempImage(fileName);
if (tempImage.isNull())
{
QMessageBox::information(this, tr("Load Warning"),
tr("Cannot load %1.").arg(fileName));
return;
}
QImage image = tempImage.convertToFormat(QImage::Format_RGB32);
QGraphicsScene *scene = new QGraphicsScene(QRectF(0, 0, image.width(), image.height()), 0);
scene->addPixmap(QPixmap::fromImage(image));
int width = graphicsView->geometry().width();
int height = graphicsView->geometry().height();
// qDebug() << "W = " << width;
// qDebug() << "H = " << height;
graphicsView->setScene(scene);
graphicsView->fitInView(QRectF(0, 0, width,height), Qt::KeepAspectRatio);
graphicsView->show();
}
}

marcel
19th September 2007, 14:55
use scene->setSceneRect(0, 0, image->width(), image->height());

sincnarf
20th September 2007, 12:48
no effect, i put your recommendation before the setScene, after setScene, both before and after setScene, nothing .... the image does not fit to the graphics view

wysota
20th September 2007, 12:52
In what way does it "not fit"? Is it bigger or smaller than the view?

BTW. Are you aware that the view will only have its size determined after it is shown for the first time? Asking for its size before it is shown for the first time will return bogus data... And it seems to be what is happening here.

sincnarf
20th September 2007, 17:01
In what way does it "not fit"? Is it bigger or smaller than the view?
The scene/view displays the actual size of the image, so if the image is bigger than the view, it wont fit.



BTW. Are you aware that the view will only have its size determined after it is shown for the first time? Asking for its size before it is shown for the first time will return bogus data... And it seems to be what is happening here.
Hmmm. Thanks. Didn't know this

sincnarf
21st September 2007, 00:21
In what way does it "not fit"? Is it bigger or smaller than the view?
The scene/view displays the actual size of the image, so if the image is bigger than the view, it wont fit. What I wish to do is if the image's bounds are bigger than the graphicsView or the Scene, the image must fit into the view keeping aspectRatio. if the image's bounds are smaller there is no need for fitting the image to the view/scene.



BTW. Are you aware that the view will only have its size determined after it is shown for the first time? Asking for its size before it is shown for the first time will return bogus data... And it seems to be what is happening here.
Hmmm. Thanks. Didn't know this. . . . . will be back with this problem soon, still have other problems in this project... And I am running out of time.

magland
21st September 2007, 02:41
. . . . . will be back with this problem soon, still have other problems in this project... And I am running out of time.

Alas, you could have used chainlink and been done by now...:p

You might be interested to know that it is possible to link the (non-gui) chainlink engine into any Qt program and make calls to chainlink from within your C++ code like this



chainlink_workspace *WS=new_chainlink_workspace();

//...

QImage X;

//initialize X ...

WS->set_variable("my_image","QImage",&QImage);
WS->execute_command("my_image=custom_function(my_image);");
WS->get_variable("my_image","QImage",&QImage);

//display X in your app



Here custom_function would be written in C++ from within the ChainLink GUI.

Some people might ask -- what's the advantage of doing it this way? Why can't I just link to "custom_function" from within my app? My response -- I can think of at least 50 reasons to use the chainlink approach ...

But I've bothered you too much with these things... just a suggestion.... good luck with your deadline. :)

sincnarf
21st September 2007, 14:06
magland, chainlink seems to be very useful... how is the setup done?

What imports should I have within my Qt/c++ project?
While running my Qt/c++ project, is chainlink supposed to be running too? or I only need the imports?
is #include "chainlink.base.h" the import that I need?
It seems http://chainlink.sourceforge.net/documentation.html
is insufficient

magland
21st September 2007, 14:54
magland, chainlink seems to be very useful... how is the setup done?

What imports should I have within my Qt/c++ project?
While running my Qt/c++ project, is chainlink supposed to be running too? or I only need the imports?
is #include "chainlink.base.h" the import that I need?
It seems http://chainlink.sourceforge.net/documentation.html
is insufficient

Yes, I know the docs are insufficient (I like coding more than writing docs, but I do realize the importance of the latter)...

You would simply link your project to a (non-gui) "chainlink_plugin.dll" library (I assume you are using windows), and chainlink does not need to be running in the background.

If you want to pursue this, contact me privately, and I'll guide you through the installation and undocumented features. If you have success, then this would help me know how to document these things. Therefore, it could benefit both of us, and others.

JM

sincnarf
22nd September 2007, 03:39
You would simply link your project to a (non-gui) "chainlink_plugin.dll" library (I assume you are using windows), and chainlink does not need to be running in the background.

Hmmm, I've executed the instruction in the site


* Finally, if you are compiling from source on Windows, then run:

> build.bat
> chainlink.bat


I searched through my entire chainlink installation and I found no "chainlink_plugin.dll".
I tried to search it under google, I entered "chainlink_plugin.dll". . . No search results found, where can I possibly acquire this dll. . . Finally, assuming I've finished my project using chainlink, can I get it to run under a linux platform? (because DLLs are for windows only right?)

already PM'ed you.

magland
22nd September 2007, 10:29
Hmmm, I've executed the instruction in the site


I searched through my entire chainlink installation and I found no "chainlink_plugin.dll".
I tried to search it under google, I entered "chainlink_plugin.dll". . . No search results found, where can I possibly acquire this dll. . . Finally, assuming I've finished my project using chainlink, can I get it to run under a linux platform? (because DLLs are for windows only right?)

already PM'ed you.

Sincnarf,
This "chainlink_plugin.dll" feature is only contained in the development version of chainlink. If you are serious about pursuing this route, I would guide you through the procedure, sending you the necessary files. Yes, it will also work in linux, although I have not tested this particular feature there - so some tweaking may be necessary.

Pls contact me by email if you want to purue it... and I will give you step by step instructions.

JM
Jeremy dot Magland at gmail dot com