PDA

View Full Version : passing arguments from mainwindow to graphic viewer widget



rakefet
27th February 2014, 21:04
Hi,

I built a project that displays 2 images on a graphics view widget. At first, I created a scene and displayed the images with QPixmap pix("<file_name>"), where <file_name> is hard coded. My second try is to pass the images file name through the main window.
In mainwindow.h:
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QString image1, QString image2, QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
};
in mainwindow.cpp:
MainWindow::MainWindow(QString image1, QString image2, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//qDebug()<<image1;
//qDebug()<<image2;
ui->widget1->setImageFileName(image1);
ui->widget2->setImageFileName(image2);
ui->setupUi(this);
}
widget1 and widget2 are from class MyGraphicsView.
in mygraphicsview.h:
class MyGraphicsView : public QGraphicsView
{
public:
MyGraphicsView(QWidget* parent = NULL);
QString ImageFileName;
QString getImageFileName();
void setImageFileName(QString ImageFileName);
};
in mygraphicsview.cpp:
QPixmap pix(this->getImageFileName());
.
.
.
.
void MyGraphicsView::setImageFileName(QString FileName)
{
MyGraphicsView::ImageFileName = FileName;
}

QString MyGraphicsView::getImageFileName()
{
QString FileName = MyGraphicsView::ImageFileName;
return FileName;
}

Running the project I am getting the error:
Error - RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly
and it crashes.

It looks like this is not the way to pass parameters from mainwindow to the child widget.
Any other way to do it?

Thanks,

anda_skoa
28th February 2014, 10:26
The member access kooks a bit strange but not wrong.

Usually you would write the access to the member like this


void MyGraphicsView::setImageFileName(QString FileName)
{
ImageFileName = FileName;
}

QString MyGraphicsView::getImageFileName()
{
return ImageFileName;
}


Your problem is accessing pointers that are invalid


ui->widget1->setImageFileName(image1);
ui->widget2->setImageFileName(image2);
ui->setupUi(this);

The last line creates the widgets, so if you try to access them before that line you are in fact accessing invalid pointers.

Cheers,
_

rakefet
28th February 2014, 14:59
OK, thanks. If I'm placing them after I created the widget, then the images are not showing up. Is there another way to pass the strings? The problem I have is that there is no event I trigger when I create the graphics viewer widget so I don't know how to pass the string through a signal.

Thanks,

Added after 43 minutes:

Correction!

Assigning the images to the scene after ui->setupUi(this) does work. the images that were not displayed were TIFF. Other images were displayed.
Thank you for the input.