Qt - In QGraphicsScene how to put a picture dynamically - qt

Hai,
I am trying to create a chess board. Here I want to do a operation like when ever a coin is clicked and dragged, at that time that picture should be copied and be placed in that old position. I don't know in which function I have to do it. When I made it on mouse pressed, then tyhe copy is coming above the original(layer wise). So somebody please help me

When I made it on mouse pressed, then tyhe copy is coming above the original(layer wise).
When you got this far, simply adding a call to myItemCopy->setZValue( -1 ) would place the copy "behind" the original item.
A different approach, and possibly "cleaner", would be to use QDrag and tell Qt how to paint your item during the Drag operation. See Qt's "Drag and Drop Robot" example for reference: http://doc.qt.io/qt-5/qtwidgets-graphicsview-dragdroprobot-example.html
Happy coding!
Robin

Simplest way to do this will be to put 2 identical QGraphicsItems for each coin/piece at the same location. The top one will be dragged and moved while the bottom one will stay for the visual cue you want.
Once the top one is dropped to the new location, move the bottom one there too.

Related

Qt: Tab Icon when QDockWidget becomes docked

Qt's QMainWindow has a ability to dock windows derived from QDockWidget. It also would put one on top on the other if few of them are stacked, producing a tab bar. Whenever a QDockWidget's state changes a signal topLevelChanged() is emmitted. At this point I would like to get access to underlying QTabWidget to set an icon for a a newly added tab. How can I do it? My patience is over trying to dig the answer out from Qt's documentation and source code. Thank you in advance.
So icon I want to be on Contents/Index tabs.
Once at least one dockwidget has been tabified, the main-window will create a QTabBar to provide the dock-tabs. Each dock-area can have its own tab-bar. These tab-bars will become children of the main-window, so you can use findChildren() or children() to get references to them.
The main difficulty will be in finding which dock-widget belongs to which tab and in which tab-bar. If the dock-widget window-titles are all unique, you can just search using the tabText(). Otherwise, you might be able to use the tabData(), which Qt sets internally to a quintptr from the dock-widget.
Once you have the correct tab, you can of course use setTabIcon() to add your icon. But note that every time a dock-widget is untabified or moved to another tab-bar, the icon will be lost.

How do I get rid of this anonymous draggable bar in qtcreator?

I wrote a program in qt creator, but it includes this anonymous draggable toolbar that I would like to get rid of. It does not show up in design mode. If I right click on it, it gives me a menu with a checkbox, which I can select to get rid of it while the program is running. What is this bar? How do I get rid of it? If I knew what it was, I could just programmatically tell it to hide when the program loads.
Here are some screenshots demonstrating this weird problem. In the first screenshot, the mouse is pointed at the bar. The second one demonstrates the right-click context menu. The third one shows the program without the bar. The box can also be dragged out of the program's window entirely. I can also upload a picture of that, if necessary.
(mouse pointing at bar)
(bar has a right-click menu!)
(bar is gone)
First things first, make sure you haven't added the toolbar yourself in code (the Designer form is nothing magical, it just generates normal Qt code to ui_XXXX.h, which you should totally study, so you understand what is really happening "under the hood").
Second, perhaps that is the main menu bar, not the main tool bar.
Then, on to remove the nasty bar. On the upper right corner of Design view, you have a tree of all items in your UI. Find the QToolBar there. It is probably called mainToolBar. Right click it and remove it there. Or, if you actually have menu bar, then find QMenuBar, probably called menuBar, and remove it instead. Remember to make version control commit (or other backup) before doing this, so you can easily revert changes.
If this fails for whatever reason, and you just want to get it to work, you can always find and delete (be careful about the dangling pointers left behind, set them to nullptr if possible):
ui->setupUi(this);
// a hack: we want to get rid of the toolbar and the menubar,
// but can't remove it from the .ui, so we delete them like this.
delete findChild<QToolBar *>(); // NULL return value is ok for delete
delete findChild<QMenuBar *>(); // NULL return value is ok for delete
Doc links for findChild and alternative findChildren.
What you have there is probably the mainToolBar QToolBar object you get with every QDesigner-created QMainWindow .ui file.
If you want to get rid of it, locate it in your .ui file and just delete it.
But better think twice: you might later decide you want a QToolBar object and (at least AFAIK) there is no way to recreate it in QDesigner...
you must use this code in your constructor.
ui->mainToolBar->toggleViewAction()->setVisible(false);
mainToolBar is my toolbar name.

Panning QScrollArea with a mouse

I have a QScrollArea. I would like to make the context pannable with a mouse - such that, a hand cursor is shown over the area and click-and-hold gets us into dragging mode.
This would be similar to what QGraphicsView can do easily with QGraphicsView::ScrollHandDrag dragging mode, only that I need it for QScrollArea.
Any idea on how to have it out of the box? Or perhaps a piece of code to do it manually? I am certainly not the first one who wants it..
Should not be difficult, especially using event filter. Take a look at my old blog post on Flick list or kinetic scrolling (the code has been moved to http://qt.gitorious.org/qt-labs/graphics-dojo). As a bonus, you can get the momentum effect easily!
Okay. At the end I changed it to QGraphicsView and it was easy to do, worked great out of the box.

How can I create a draganddrop wrapper inside a table row, that can represent the whole row

When I use treetable in Vaadin, it is working fine drag and drop the whole row when click the left and right blank ereas. but that is not user friendly, I wanna create a draggable layout inside the row, let's say the 1st component of the each row, that when clicking it, the whole row is selected and can be draged and dropped to reorder.
The thing is I can create a vertical layout with wrapper doing that, but drag mode is only for its component or the wrapper itself, and when doing the drag action, it doesn't actually showing the whole row is moving, which may confuse the clients. What can I do to make it looking like I am dragging the whole row, similar with the one you drag the blank area of each row? Thanks.
I'm afraid there currently is no way for adding a wrapper that would extend the entire row instead of just one cell. To do this you'd need to extend the client-side implementation of Table (VScrollTable) and this is not something I would recommend as it is quite complex.
I do think that what you describe should be possible in core Vaadin (without using drag & drop wrappers), so could you please file a ticket at dev.vaadin.com? And please attach a small application showing the problem to make it easier for us to see the problem and fix it.
Anyways, in order for you to get it working today I'd suggest that you change your UI design a bit and for example borrow drag handles from iOS. By this I mean that you could add an icon that suggests "draggable" as a background-image in the first cell (or in the row header cell of each row) that encourages the user to grab the row there, where it is "empty" and dragging works. This way the user might not be too confused if dragging only works reliably in some parts of the rows.

Qt drag and drop animation

I have two QTreeViews side by side and I want to implement dragging one item from one table into the other table. What I would like to do however is that when the item to be dropped reaches the destination table, that the two rows sort of "make space for it" and separate to really show the user exactly where the item would be dropped. Could someone point me in the right direction as to how to do this as I have no idea really where to start.
Thanks,
Stephen
http://doc.trolltech.com/latest/dnd.html should lead you to pretty much anything you want to do re drag and drop.
As for your specific goal, you need to implement QWidget::dragMoveEvent(QDragMoveEvent* event), which will get called every time the mouse moves within the destination tree while dragging an item. Then get the position of the cursor with QDragMoveEvent::pos(). After that, while in dragMoveEvent, use QTreeView::indexAt(pos()) to get the item under the mouse. Now, I don't know how to get the items to separate in a smooth animated way, which would be ideal. But what you can do is (temporarily) add a blank item to the list, which will have the almost identical effect. Then implement QWidget::dropEvent() to handle the drop event, and when this happens delete the blank item and insert the dragged item in the location where it was.

Resources