PDA

View Full Version : Giving user facility to resize widgets run time



Charvi
16th March 2012, 07:26
Hi,

I have a an application where on one side I display a list of widgets and on the other side I have a QGraphicsView. When the application runs the user can drag the widget from the list and when he/she drops it, that widget will appear on the QGraphicsView. Now I want to add a facility that the user can resize the widget with the help of mouse after dropping it on the view. Does anybody have an idea how to do it?

Thanks in advance.
Charvi.

Lykurg
16th March 2012, 10:50
I guess you are using QGraphicsWidget. Then you can alter the size with QGraphicsWidget::resize(). To react on mouse event you can either use QGraphicsScene::mousePressEvent() + QGraphicsScene::mouseMoveEvent() or you can create handler items on the edges of the dropped widget. Johan has an example of the later in his book Foundations of Qt Development.

Charvi
16th March 2012, 11:34
Thanks for answering.

But my problem is a bit different. Like on my mainwindow on the right hand side I have a QWIdgetList wherein I have Items named Line Edit, Text edit, Label and Push Button. Now on the left hand side I have a QGraphicsView. When I drag the name Text Edit from the list and drop it on the QGraphicsView then I create an instance of Text Edit runtime and a text edit is shown at the place of the drop event. Now when i click on the text edit I get a cursor there and the click will be detected as a Text Edit mouse press event and not a QGraphicsView mouse press event. I want 2 things:

1) I want to change the size of any widget at run time like text edit or label.

2) I also want to be able to move the widgets around.


Also in the drop event I have created the instance of the Text Edit by QText Edit. Do you think creating my custom class and then reimplementing some method would help? Do you know any such method?

Charvi.

Charvi
20th March 2012, 05:19
Ok. I tried doing the following.

For 1) I tried reimplementing the resizeEvent() of the iwdget. But for some reason my resizeEvent() is not being called. Does anybody have any idea when the resizeEvent() of any widget will be called?

For 2) I tried reimplementing the mousePressEvent and the mouseMoveEvent. In the first one I stored the position of the mouse Press and then int he second one I detected the mouse move event position and simply called the move(new_position - old_position) to move the widget. It works to some extent, the widget does move but not so smoothly there's some problem, it sometimes goes to the top left corner. It just doesn't work correctly some of the times. :(

Does anybody have any idea about these?

Charvi.

wysota
20th March 2012, 07:37
For 1) I tried reimplementing the resizeEvent() of the iwdget. But for some reason my resizeEvent() is not being called. Does anybody have any idea when the resizeEvent() of any widget will be called?
When the widget is resized.


For 2) I tried reimplementing the mousePressEvent and the mouseMoveEvent. In the first one I stored the position of the mouse Press and then int he second one I detected the mouse move event position and simply called the move(new_position - old_position) to move the widget. It works to some extent, the widget does move but not so smoothly there's some problem, it sometimes goes to the top left corner. It just doesn't work correctly some of the times. :(
Apparently you are not doing any checks on the data you receive.

Have you seen the Moving widgets article in our wiki?

aamer4yu
20th March 2012, 08:43
How did you implement the resizeEvent function ?
When you are adding the widget to the scene, it becomes a graphics item. May be you will need to enable QGraphicsItem::ItemIsMovable.

Charvi
20th March 2012, 09:33
Have you seen the Moving widgets article in our wiki?

Thanks a ton. The article helped. Now I can move my widgets without any problem.
Though one quick question. Before reading this article i was using globalPos() to find the position of the event occurrence and then was using it to move the widget. According to the documentation, mapToParent(), used in the article, does the same. Then why is it that the app works fine with the latter and not with the former?


When the widget is resized.

Ya but how should the widget be resized? I want a functionality in which the widget should be able to be resized by the user run time with the help of mouse, like we do it in Qt Designer. I don't want to do it through programming.

Added after 4 minutes:


When you are adding the widget to the scene, it becomes a graphics item. May be you will need to enable QGraphicsItem::ItemIsMovable.

I am adding the widgets in the GraphicsView run time, by drag and drop. There is a list of widgets on one side and Qt GraphicsView on the other side. When the user drags and drops the widget name on the graphics view it is replaced by the widget instance. So the widgets are being added run time. And about the ItemIsMovable flag, I think it is true by default.