QTToolTip::showText() flashes inside a QMousePressEvent - qt

All
I am using QToolTip::showText inside a QMousePressEvent handler;
void PmCellsBasedMap::mousePressEvent(QMouseEvent *mouseEvent)
{
if (mouseEvent->button() == Qt::LeftButton)
{
// mouse left button pressed
QString str;
if (getTip(mouseEvent->pos(), str))
{
QToolTip::showText(mouseEvent->globalPos(),str);
}
}
}
However, the QToolTip shows the text too quickly, I need to keep the mouse pressed state for a relative long time, it shows the text later.
My intention: when operator clicks the left mouse button, the QToolTip shows the text all the way and be updated when the next mouse button click action happens.
Can anyone help me on this?
Thanks in advance ...

Related

How can I distinguish wheel button click event from mouse press event?

I am wondering how i can distinguish wheel click event from mouse press event. Because i want to do different handling for these two events in pyside. Currently, every time I click the wheel button, the event is catched by mousepressevent. Can anyone explain ?
Edit: I want to implement this in a subclass of
qglwidget class
From its name, the mousePressEvent is responsible for mouse clicks while the wheelEvent is for scrolling solely. The wheelEvent will not catch the wheel button click. It is how the Qt's API is designed when it comes to mouse events processing.
In order to separate which mouse button is pressed (right, wheel or left), use button property of QMouseEvent.
This is how the code would look like using C++ (I imagine it is easy to translate it to pyside)
void GLWidget::mousePressEvent(QMouseEvent *event) // redefine the mouse event
{
switch( event->button() ) {
case Qt::LeftButton:
// do stuff for left button pressed
break;
case Qt::MiddleButton:
// do stuff for wheel button pressed
break;
// ...
}
}
So, for pyside, you only need to compare the button property of event in mousePressEvent and see whether it is Qt.LeftButton , Qt.RightButton or Qt.MidButton. Hope that helps.

How to detect a QGraphicsItem moving performing by mouse?

I have a child class of QGraphicsItem with Selectable and Movable flags. When I select many items and moving them, all recieves itemChange event. Is there any way to detect in itemChanged that mouse's button is still pressed?
Please refer to the QApplication::mouseButtons() function that will return the current state of the mouse buttons Qt::MouseButtons.
Qt::MouseButtons btns = QApplication::mouseButtons();
if (btns & Qt::LeftButton) {
// The left button is pressed.
[..]
}

How can I disable Alt + F4 window closing using Qt?

I've disabled X button in Qt from my dialog using this line:
myDialog->setWindowFlags(Qt::Dialog | Qt::Desktop)
but I couldn't detect Alt + F4 using this code:
void myClass::keyPressEvent(QKeyEvent *e)
{
if ((e->key()==Qt::Key_F4) && (e->modifiers()==Qt::AltModifier))
doSomething();
}
what should I do to detect Alt+F4 or disable it in Qt?
Pressing Alt+F4 results in a close event being sent to your top level window. In your window class, you can override closeEvent() to ignore it and prevent your application from closing.
void MainWindow::closeEvent(QCloseEvent * event)
{
event->ignore();
}
If you left the close button (X) visible, this method would also disable it from closing your app.
This is usually used to give the application a chance to decide if it wants to close or not or ask the user by displaying an "Are you sure?" message box.
The code below prevents a dialog close when pressed Alt+F4, [X] or Escape, but not by calling SomeDialog::close() method.
void SomeDialog::closeEvent(QCloseEvent *evt) {
evt->setAccepted( !evt->spontaneous() );
}
void SomeDialog::keyPressEvent(QKeyEvent *evt) {
// must be overridden but empty if the only you need is to prevent closing by Escape
}
good luck to all of us ;)
Also you can handle the event in your dialog's class (at least if it's modal dlg):
void MyDialog::closeEvent(QCloseEvent* e)
{
if ( condition )
e->ignore();
else
__super::closeEvent(e);
}

Custom QAction/QMenu for mouse button detection

I'm trying to create a popup menu, where I can detect the mouse button that was pressed for a given item. I've created a custom QAction already to build my QMenu, but the triggered signal when the menu item is pressed doesn't provide a QMouseEvent for me to query the button pressed.
Also, I'm setting the a status tip for each QAction, which appears in the status bar when I mouse over it, but it stays even after I close the QMenu. Is this normal behavior?
I'm not sure if I understood what do you want; but if you want to show a popup menu on right mouse click, you should at first in header file of your widget (or window class) override function related to mouse event and declare some function that will show your popup menu. So, the header file should contain these declarations:
...
void Popup(const QPoint& pt);
void mousePressEvent(QMouseEvent *event);
...
And in cpp file definitions of functions:
void testQt::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
this ->Popup(event ->pos());
event->accept();
}
}
void testQt::Popup(const QPoint& pt)
{
QPoint global = this ->mapToGlobal(pt);
QMenu* pPopup = new QMenu(this);
QAction* pAction1 = new QAction("Item 1", this);
QAction* pAction2 = new QAction("Item 2", this);
pPopup ->addAction(pAction1);
pPopup ->addAction(pAction2);
QAction* pItem = pPopup ->exec(global);
if(pItem == pAction1)
{
}
else if(pItem == pAction2)
{
}
}
Now, when you press right mouse button, a popup menu will appear at cursor's position.
I hope this helps.
NOTE: If you want to detect which of mouse buttons is pressed when an action is chosen, you should inherit your own class from QMenu. QMenu class contains protected function mousePressEvent(QMouseEvent *event) which should be overriden and you'll be able to detect if left or right mouse button is pressed when an item is chosen in your menu.
I know this is a very old post. But if you want to know what button you have clicked in a popup menu/ context menu.
Lets say you press button Save, that's connected with signals and slots etc. In the slot call a method called sender();. This returns a QObject which you can cast into your QAction* and get the data etc from it .
void MyClass::showMenu()
{
auto action(new QAction*("Blah", ui->my_toolbar));
QObject::connect(action, &QAction::triggered, this, &MyClass::mySlot);
}
void MyClass::mySlot()
{
auto myAction(static_cast<QAction*>(sender()));
myAction->doAwesomeStuff();
}

How to tell the mouse button using QApplication::mouseButtons() in a "click" slot?

I have a QMainWindow, and want to handle the "clicked" signal from a smaller widget (such as tableview) inside it.
Originally I connect the signal to a slot of this QMainWindow, this is the most common approach.
Now I need to tell which mouse button is clicked, and do different things for left and right button, I found that the "clicked" signal don't have the mouse event information.
I tried to implement the "mousePressEvent" function,but there are still some problem. if the mouse action is acted on the smaller widget, the MainWindow won't go into its mousePressEvent.
Some document says that we can tell the button by QQApplication::mousebuttons()
http://bugreports.qt-project.org/browse/QTBUG-1067
and I also found some sample code. However, this is for "press event", but I want to get the mouse button for "click event".
Follows is the sample code :
connect(moduleTree,SIGNAL(itemPressed(QTreeWidgetItem *, int)),this,SLOT(SlotItemClicked(QTreeWidgetItem *, int)));
void CGuiMainwindow::SlotItemClicked(QTreeWidgetItem *item, int column)
{
if (qApp->mouseButtons() == Qt::LeftButton)
{ return; }
if (qApp->mouseButtons() == Qt::RightButton)
{
......
}
}
When I try to do this, neither of the 2 if statements will be satisfied, I don't know why. the qApp->mouseButtons() always return 0, how can I get the clicked mouse button by QApplication::mouseButtons?
In my code, the slot looks like that :
void clickItem( const QModelIndex & idx){.....}
You get 0 becouse clicked is emited after mouse release, not at mouse press. What do you want to achieve ? Maybe try settings on you widget contextMenuPolicy to custom, and than connect to signal contextMenuRequested (for the right click) and clicked for the left click ?
for "connect" use this:
connect(moduleTree,SIGNAL(itemClicked(QTreeWidgetItem *,int )),this
,SLOT(SlotItemClicked(QTreeWidgetItem *, int)));
define a global flag:
public:
Qt::MouseButton mouseClickedBtnFlag;
and then reimplement "mouseReleaseEvent":
CGuiMainwindow::mouseReleaseEvent ( QMouseEvent * event )
{
mouseClickedBtnFlag = event->button();
}
and then:
void CGuiMainwindow::SlotItemClicked(QTreeWidgetItem *item, int column)
{
if (mouseClickedBtnFlag == Qt::LeftButton)
{ return; }
if (mouseClickedBtnFlag == Qt::RightButton)
{
......
}
}
Qt::MouseButtons is a QFlags type. You can't test it with == operator. Use & operator for testing:
if(QApplication::mouseButtons() & Qt:LeftButton) {
...
}

Resources