-
no compile error, error on run
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.
Code:
void MainWindow
::loadGraphicsViewParameter(QGraphicsView *graphicsView
) {
tr
("Open Image"),
QDir::currentPath());
if (!fileName.isEmpty())
{
if (tempImage.isNull())
{
tr("Cannot load %1.").arg(fileName));
return;
}
QImage image
= tempImage.
convertToFormat(QImage::Format_ARGB32);
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();
}
}
-
Re: no compile error, error on run
I don't think this works:
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.
-
Re: no compile error, error on run
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
-
Re: no compile error, error on run
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.
-
Re: no compile error, error on run
Code:
void MainWindow
::loadGraphicsViewParameter(QGraphicsView *graphicsView
) {
if (graphicsView == 0)
{
QMessageBox::warning(this, tr
("Load Error"), tr
("Graphics View is NULL"));
return;
}
view->setScene(scene);
if (scene->views().size() == 0)
{
QMessageBox::warning(this, tr
("Load Error"), tr
("GraphicsView's GraphicsScene is NULL"));
return;
}
}
and this
Code:
void MainWindow
::loadGraphicsViewParameter(QGraphicsView *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
-
Re: no compile error, error on run
Code:
void MainWindow
::loadGraphicsViewParameter(QGraphicsView *graphicsView
) {
tr
("Open Image"),
QDir::currentPath());
if (!fileName.isEmpty())
{
if (tempImage.isNull())
{
tr("Cannot load %1.").arg(fileName));
return;
}
QImage image
= tempImage.
convertToFormat(QImage::Format_ARGB32);
float w = viewScene->width();
float h = viewScene->height();
graphicsView->setScene(scene);
delete viewScene;
scene
->addPixmap
(pixmap.
scaled(QSize((int)w,
(int)h, Qt
::KeepAspectRatio, Qt
::SmoothTransformation));
qDebug() << "Width = " << w;
qDebug() << "Height = " << h;
graphicsView->show();
}
}
-
Re: no compile error, error on run
didn't work. . . same windows error problem
-
Re: no compile error, error on run
How about... using a debugger? :)
-
Re: no compile error, error on run
what's a debugger? I'm using eclipse CDT plugin and I don't know how to debug this
-
Re: no compile error, error on run
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.
-
Re: no compile error, error on run
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 ?? ()
-
Re: no compile error, error on run
Try compiling the application in debug mode:
Quote:
make clean
qmake -config debug
make
-
Re: no compile error, error on run
Quote:
Originally Posted by
jpn
Try compiling the application in debug mode:
Sorry but what;s next?
anyway here's additional debugging info.
Quote:
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. :(
-
Re: no compile error, error on run
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().
-
Re: no compile error, error on run
Quote:
Originally Posted by
wysota
Show us the backtrace after compiling in debug mode. There is nothing obvious here :)
here's the backtrace
Quote:
#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
Quote:
Originally Posted by
wysota
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...
-
Re: no compile error, error on run
Look here:
Quote:
#3 0x0040ac7b in MainWindow::loadGraphicsViewParameter (this=0x22fbe0, graphicsView=0x4232d98) at mainwindow.cpp:828
loadGraphicsViewParameter() is called and inside it:
Quote:
#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.
-
Re: no compile error, error on run
At last the windows error is gone BUT I still can't get the scene/image to fit in the view using this
Code:
void MainWindow
::loadGraphicsViewParameter(QGraphicsView *graphicsView
) {
tr
("Open Image"),
QDir::currentPath());
if (!fileName.isEmpty())
{
if (tempImage.isNull())
{
tr("Cannot load %1.").arg(fileName));
return;
}
QImage image
= tempImage.
convertToFormat(QImage::Format_RGB32);
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();
}
}
-
Re: no compile error, error on run
use scene->setSceneRect(0, 0, image->width(), image->height());
-
Re: no compile error, error on run
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
-
Re: no compile error, error on run
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.