Qt press left click twice for context menu to disappear - qt

I'm trying to use a context menu from Qt when I press right click.
Here is what I've tried:
connect(mtreeView, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(showContextMenu(const QPoint&)));
Then
void MainTreeViewController::showContextMenu(const QPoint& pos)
{
QPoint globalPos = mtreeView->mapToGlobal(pos);
QMenu rightClickMenu;
rightClickMenu.addAction(QString("Option"));
rightClickMenu.exec(globalPos);
}
When I press right click, the menu appears. Then if I press left click outside of it nothing happens. I must press left click twice in order to make the menu disappear.
Why does that happen? Thanks!

This can happen if showContextMenu is called twice for a single right click. You can verify by setting a breakpoint in showContextMenu and checking whether it is called twice.
Probably your signal slot connection is created twice, which can be the reason behind this. You can verify by setting a breakpoint to the line where signal slot connection is made.

Related

What SIGNAL is emitted in Qt when pressing the "X" on the top right corner to terminate the Window?

Which SIGNAL is emitted, when pressing the "X" in the top right corner of a Qt Window application?
I have a second QDialog widget beside my MainWindow in my Qt application. And I would like to intercept the press on the "X" in my second QDialog, how?
The QCloseEvent class contains parameters that describe a close event. Close events are sent to widgets that the user wants to close, usually by choosing "Close" from the window menu, or by clicking the X title bar button. They are also sent when you call QWidget::close() to close a widget programmatically.
To ignore it you can call ignore() method:
void YourDialog::closeEvent(QCloseEvent* iEvent)
{
// ignore close event
iEvent->ignore();
}
There is no signal for that, you need to reimplement QWidget::closeEvent() or install an event filter filtering for QCloseEvents.

Close all ContextMenu with mouse button

Is there a way to close all opened context menus opened on the scene?
When I press a button I would like to close all Context menus opened by right muse click.
Sure, just have some data structure, say a Stack<ContextMenu> Where every time you make a context menu, you push it on the stack, and every time you hide it, you pop it off the stack. Then when you press a certain button, just iterate through the stack and call the .hide() method of contextMenu, on each item i the stack, hiding them as you go, and voila! that should do it.

Let QTabWidget switch to corresponding page when mouse cursor hover on the tab

I want to show the corresponding page when mouse cursor hover on the tab of a QTabWidget.
For example, when the mouse cursor hover on tab ‘page2’ here , I hope the QTabWidget shows the corresponding page automatically instead of clicking. How to implement this feature?
You may try adding an event filter on the QTabWidget object's QTabBar in order to trap the mouse move event. In the filter handler, use QTabBar::tabAt( QPoint ) to find which tab is below the cursor. Set up a timer when the cursor first enters a given tab, reset time when cursor leaves it. When the timer fires, switch active tabs.
You may try using setTabToolTop function.
ui->tabWidgetHz->setTabToolTip(0,"tooltip for tab1.");
ui->tabWidgetHz->setTabToolTip(1,"tooltip for tab2.");
ui->tabWidgetHz->setTabToolTip(2,"tooltip for tab3");

Why does my QGraphicsWidget receive events it shouldn't after QDrag?

I have a simple QGraphicsWidget, MyGraphicsWidget. Here's my mouseMoveEvent(), which seems to work fine :
void MyGraphicsWidget::mouseMoveEvent (QGraphicsSceneMouseEvent *event)
{
QPointF p = event->scenePos() - m_StartPos;
if(p.manhattanLength() < 20)
return;
//omitted drawing a rounded rect on the drag
QDrag *drag = new QDrag(event->widget());
drag->start(Qt::MoveAction);
}
The scene's dropEvent() just moves this widget to its new position, and I don't have a press/move event for the scene itself, so those should get passed on correctly to the widgets within.
However, once the drag completes, the next mouse press will be on this widget. So if I try to click and drag another widget, I'll be stuck dragging this one on accident, despite the fact that my cursor is not on this widget. I've printed out the event->pos() and event->scenePos(), and both reported that the cursor is where it appears to be (not on the widget at all). If I click once before trying to click and drag, everything works normally. Is there maybe something I need to implement within mouseReleaseEvent() or my mouseMoveEvent() ?
Thanks.
It's working now. I'm pretty sure this was the issue:
MyGraphicsWidget had another custom GraphicsWidget sitting on top of it, and in its mousePressEvent, I was calling QGraphicsWidget::mousePressEvent(event); at the start of the event. The rest of the event was never getting triggered, which was messing it all up. I moved that line to the end of the method, and everything seems okay now.

Qt - Radiobutton in diff dialog(window) -> taking result to the mainwindow into lineedit

my problem looks like that. I got dialog window and i know how to get result from checked radiobutton but only in this window. How to take result into different window(mainwindow).
button(Pobierz) is on mainwindow and close to this button is lineedit2 where i would like to take result from checked radiobutton, but dont know how. I make lineedit in this new Dialog window and its taking result, but i dont know how to take this result into mainwindow. Hope I explain good enough. Thanks for any help.
void Pobierz::on_pushButton_clicked()
{
if(ui->radioButton1->isChecked())
{
ui->lineEdit->setText("K");
}
if(ui->radioButton2->isChecked())
{
ui->lineEdit->setText("S");
}
if(ui->radioButton3->isChecked())
{
ui->lineEdit->setText("I");
}
}
Greetings,Tom.
up1
i tried couple ways but still cant solve this..
In your class for the dialog, declare a signal that passes a QString. Also override the accept function (if you don't already). In the accept function, emit the signal with the appropriate string according to the radio buttons. (Don't forget to call the parent accept function in your own).
In your class for the main window, when you create the dialog, connect the signal from the dialog to a slot that sets the text in the line edit in the main window. When the dialog is accepted, the signal should fire, running the slot in the main dialog, adding the appropriate text to the line edit.
Create a slot in the main window which would get the radio button status from the dialog window. In this slot set your main window's lineedit based on the result from the dialog window radio button checked status.
While creating the radio button on the dialog window, connect the radio button's clicked signal with the main window's slot defined earlier.

Resources