PDA

View Full Version : QGraphicsView, QSignalMapper and creating widgets by code (not using .ui )



dpatel
9th June 2010, 07:41
Hi,

I am creating an application which has following features/limitation
-I have to create all the widgets in UI along with the layout by code. I don't have .ui file.
-The number of widgets to be created is dynamic
-Since the number of widgets is not fixed, I will use QSignalMapper to map signals for the widgets and slots.
-I have to provide Zoom in and Zoom out feature for the application

Currently I am trying to create all the widgets and add it to layout. And then add the layout to another QWidget by setLayout. Then I add this widget to scene which is associated to QGraphicsView. I am getting 'Access violation' while I am trying to connect the signal slots.

Can someone advise me on possible approach that will work for this application.

Thanks

tbscope
9th June 2010, 07:49
I guess your real question is "Why do I get these errors?"

Can you post the code please?

dpatel
9th June 2010, 08:29
The reason I didn't ask that question is that I am not sure my approach to this requirement is correct.

But lets start with the cause of error.

main.cpp


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);

ParseAndCreateWidgets p;
scene.addWidget(p.myWidget);
view.show();

return a.exec();
}



This is how I create widgets and do signal mapping




if (className.compare("QPushButton")==0)
{
//create pushbutton
QString btnText=subItem.firstChildElement().firstChildElem ent().text();
QString btnName=subItem.attribute("name");
QPushButton* btn=new QPushButton;
btn->setObjectName(btnName);
btn->setText(btnText);
layout->addWidget(btn);
connect(btn,SIGNAL(clicked()),signalMapper,SLOT(ma p()));
signalMapper->setMapping(btn,btnName);
}


I add the 'layout' object in above code snippet to a widget which is then added to the scene



myWidget=new QWidget;
myWidget->setLayout(layout);


This code compiles ok, but when I run it I get Access violation at


connect(btn,SIGNAL(clicked()),signalMapper,SLOT(ma p()));


statement in the code.

Currently I haven't added support for zooming in for which I guess I will need to use QGraphicsProxyWidget.

tbscope
9th June 2010, 08:36
Where do you defiine signalMapper?
Maybe it doesn't exist when you call connect

dpatel
9th June 2010, 08:46
singalMapper is a member of ParseAndCreateWidgets and is created in constructor. The code to create the widgets gets called after it.
Also if I remove the QGraphicView support and bring up UI by just doing setLayout(layout) and show in ParseAndCreateWidgets (which is derived from QWidget) everything works fine. I only get the error when I use QGraphicsView to bring up the UI.