QTableWidget check right mouse even cell pressed - c++

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.

Related

QT Drag and Drop Event Between List Widget and GraphicView

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.

Allow user to resize widgets at runtime in QT5

I have an application written in C++/QT5 with a QListView widget within a QHBoxLayout within a QGroupBox. There is also a QTabWidget in the main window. I would like the user to be able to resize the QListView widget by clicking and dragging and for the other items to automatically resize themselves accordingly.
I feel like this should be something that is easily done within the framework of QT5, but I can't for the life of me find a way. Even having a border on the list view that I can resize within the code of my application would be a start.
Thanks to jhnnslschnr I was able to solve this via the QSplitter widget. If you're using QtCreator as I was, you can use QSplitter simply by Ctrl-clicking the widgets you want in the splitter and then selecting "Lay out horizontally (vertically) in splitter". The user can now select the partitioning at run-time.

Setting and manipulating Icons in QT

I want to add icons in QMainWindow and when i will click that window it should perform some action like popup some window. So what should i use for the icon menu?
You could use the QToolButton class to accomplish this task.
It is possible to set it to only contain an image/icon without text.
l buttons are normally created when new QAction instances are created with QToolBar::addAction() or existing actions are added to a toolbar with QToolBar::addAction(). It is also possible to construct tool buttons in the same way as any other widget, and arrange them alongside other widgets in layouts.
A tool button's icon is set as QIcon. This makes it possible to specify different pixmaps for the disabled and active state. The disabled pixmap is used when the button's functionality is not available. The active pixmap is displayed when the button is auto-raised because the mouse pointer is hovering over it.
The button's look and dimension is adjustable with setToolButtonStyle() and setIconSize(). When used inside a QToolBar in a QMainWindow, the button automatically adjusts to QMainWindow's settings (see QMainWindow::setToolButtonStyle() and QMainWindow::setIconSize()). Instead of an icon, a tool button can also display an arrow symbol, specified with arrowType.
So, you would use these methods:
QAction * QToolBar::addAction(const QIcon & icon, const QString & text)
Creates a new action with the given icon and text. This action is added to the end of the toolbar.
and
toolButtonStyle : Qt::ToolButtonStyle
This property holds whether the tool button displays an icon only, text only, or text beside/below the icon.
The default is Qt::ToolButtonIconOnly.
To have the style of toolbuttons follow the system settings (as available in GNOME and KDE desktop environments), set this property to Qt::ToolButtonFollowStyle.
QToolButton automatically connects this slot to the relevant signal in the QMainWindow in which is resides.
As you can see, the default is icon only.

How can I set Qt tab order for a form with a composite widget?

I have a form written with Qt Designer, which contains a QWidget promoted to a custom widget I have. The custom widget contains several combo boxes. I want the form to have a reasonable tab order, with focus moving from the widget immediately before the custom widget, then going through the combo boxes in the custom widget, and proceeding to the widget after the custom widget. So I set the QWidget to have tab focus in Designer, but the custom widget doesn't handle having focus properly.
I could solve this problem using QWidget::setTabOrder, but that would be messy because I would have to reach into the custom widget from the outside. Alternatively I could give the custom widget a member function to set the tab order. Ideally there should be a simpler way. Is there?

Qt popup grabMouse for all children

I'm trying to create a popup menu like control that contains custom widgets. I need to capture the mouse, but I need to have the children in the widget still get their mouse messages. It appears the grabMouse sends events only to the widget that grabbed the mouse, not its children.
The popup is simply a series of buttons (using a QGridLayout). The control should work that the user presses the right-mouse button, the popup appears, they move to an item and release the mouse button. Optimally it would work exactly like a QMenu popup but with custom widgets and a custom layout.
How can I achieve this?
It appears that simply specifying attribute Qt::Popup is enough to get the fundamental behaviour required.
Installing an event filter on all children is also necessary. All mouse events, enter/leave/hover events must be captured. QT has a defect with grabMouse so that won't work -- the filter must be used to get expected behaviour.