QT Drag and Drop Event Between List Widget and GraphicView - list

I have a ListWidget whose elements are all icons, and I have a GraphicsView on the other side. Here is the ListWidget event code:
void MainWindow::on_zemin_buton_clicked()
{
ui->aksesuar_ornekleri->clear();
ui->aksesuar_ornekleri->setViewMode(QListWidget::IconMode);
ui->aksesuar_ornekleri->setIconSize(QSize(50,50));
ui->aksesuar_ornekleri->setResizeMode(QListWidget::Adjust);
ui->aksesuar_ornekleri->addItem(new QListWidgetItem(QIcon("../1.jpg"),"11"));
ui->aksesuar_ornekleri->addItem(new QListWidgetItem(QIcon("../2.jpg"),"12"));
ui->aksesuar_ornekleri->addItem(new QListWidgetItem(QIcon("../3.jpg"),"13"));
}
When I click the push button, it clears the list and fills it with the right icons. (I have 4-5 more button click events like this.)
I just want to drag these icons and drop them into the GraphicsView. How can I do that? Should I set up the right design settings or write some drag-drop event code myself?
Edit: Now I realize that I want to do the exact thing with the Qt GUI toolbox. I want to add icons but don't want them to disappear from my list too.

Related

QTableWidget check right mouse even cell pressed

I am a beginer with QT.
I want to display menu ( copy, paste, clear) when right-click mouse btuton on QTableWidget. In mainwindow i can use mousePressEvent(QMouseEvent* event), but in QTableWidget i can't do that.
One way is to have widgets show a context menu from the list of actions you set on the widget. See QWidget::addAction() and the related functions. Set the widgets contextMenuPolicy to Qt::ActionsContextMenu to use this approach. You can also handle the menu manually by modifying the contextMenuPolicy to be Qt::CustomContextMenu and then connect to the customContextMenuRequested signal.

Is there a way to attach or anchor two QWidgets together?

I'm getting started with Qt and decided to build a full-screen text editor. I want to have a button (button with arrow in screenshot) attached to a QDockWidget which opens and closes it so the button is always visible to the right side of the screen and stay anchored to it when dock is visible or resized.
My current app is a simple fullscreen textEdit set to centeralwidget in Mainwindow.
I haven't found a way to do this yet with layouts or existing addAnchor() functions so any help or direction is appreciated.
You can achieve what you want by using a container for your text edit and the button. A QWidget instance can be used as an "invisible"*** container for other widgets.
So in Qt Designer you add a widget as a central widget of the main-window, inside this widget you add the text edit and the button, then you set a vertical layout for this container widget.
Don't forget to restrict the docking widget to only dock to the right side, you can do that with: dock->setAllowedAreas(Qt::DockWidgetArea::RightDockWidgetArea); //assuming dock is the pointer to your QDockWidget.
In case you want the dockWidget to be able to dock to any side and the button to follow, you can do that too, but it get a little bit more complicated. Basically you need to connect a slot to dockLocationChanged of your dockWidget and based on where it's docked you need to set-up a new layout for the container widget to be vertical or horizontal and the order of the textEdit and the button based on the side the dock happened.
LE:*** you will most likely need to set the margins you want, since both the widget and it's layout can have them and the actual content might have higher spacing than you want.

Close button on QTabWidget not the Tabs in the QTabWidget

I am using Qt-5.8 over Ubuntu.
This is how my QTabWidget appears :
And the black dot is where I want the Minimize Button to happen.
One way I came across is that I can use QToolButton *qToolButton to create a new button and tabWidget->setCornerWidget(qToolButton) and then add the implementation over its click event.
But should there not be any other way to just show the minimize button as like in MainWindow or SubWindows has. Which just minimizes it.
Minimize button on top panel of your QMainWindow's instance is part of Windows Manager subsystem of your OS. So you can't use similar approach inside your window as toolbox with buttons, etc.
As you wrote, try to use tabWidget->setCornerWidget(qToolButton) to place your custom minimize button inside your window.

How to manage QSplitter in Qt Designer

When I press a button, I bring up a dialog where user select things and press 'Ok' at the end. I want a splitter in this dialog. Left pane will show tree and right will show something else. How do I do that right?
From Qt example itself:
QSplitter *splitter = new QSplitter(parent);
QListView *listview = new QListView;
QTreeView *treeview = new QTreeView;
QTextEdit *textedit = new QTextEdit;
splitter->addWidget(listview);
splitter->addWidget(treeview);
splitter->addWidget(textedit);
So in this example, splitter is created without any dialog resource. If I have to create this way, that would mean I have to create all my controls in the code as well rather than Qt Creator.
What is the right way to do this when I need other controls on the screen?
You can simply create splitter containing items in Qt Designer :
First place your widgets on your dialog or widget in designer (They should not be in a layout)
Select the widgets that you want to be in a splitter (By holding CTL and clicking on them)
Right click on a selected widget and from Layout menu select Lay Out Horizontally in Splitter or Lay Out Vertically in Splitter.
Now apply a grid layout to the dialog and everything should be OK. You would see something like this in Object Inspector View :
Okay, I know this is ancient, but here's the complete answer.
First, within some sort of widget container, plop your pieces in. For the window I just did, I have a Widget as my window. I put two widgets inside that labeled something like topContainer and bottomContainer. I then put all the widgets they each need into them, and gave them their own layouts.
Then do NOT select the main container. Select the two widgets you want to split. You're in effect putting a splitter on them, not on the main container. So I went to the widget list window and selected both together, then right-click for the dialog window, scroll down to the Layout option, and "Lay Out Vertically in a Splitter" is NOT greyed out. Select it.
You still need a layout on the main container. A splitter is not a layout. So at that point, I just put a vertical layout on the main container.
To repeat: you are NOT setting a layout on the container holding the pieces you're trying to split. You are selecting the two widgets to split and adding a QSplitter around them. That's the trick to get it to work.
You can still create your controls in a .ui file using Qt Designer (integrated in Qt Creator). Within Qt Designer, add a QWidget object to your dialog. Then, from QDialog derived class you'll write, directly in your constructor, create your QSplitter using the QWidget object as a parent.
This way, you can create all but the splitter object from Qt Designer.
I think it's also possible to create the QSplitter (as you can create a QButton, QCheckBox...) item directly from Qt Designer.

QComboBox - Drag and Drop

how to use the Drag-and-Drop (in Qt; C++) mechanisms for QComboBox. That is I want to drag a QComboBox and drop it somewhere else in its parent. I want its items in its popup list to remain exactly the same. In other words, the whole QComboBox is the QWidget onto which the Drag and Drop are applied.
My problem here is as follows: when there is a mouse press event, how to distinguish between (1) open popup list and (2) start a drag. Several solutions: 1. On the QComboBox, define one region for the “popup list” and one region for the drag. 2. Put the QComboBox in a larger Qwidget.
Any help is welcome
JP