Popup widget in qt - c++

How to create it in qt?
When you click on button - should be shown popup widget and its width should be = button width.
And if main window (main form) drag to another place on the screen - popup widget should continuously follow the button (must be attached to the bottom border of the button).
before click image:
after click image:

Create widget, don't put it any layout, set it's parent to button's parent (lets call it "host"), set window flags to Qt::Window | Qt::FramelessWindowHint
mPopup = new QWidget(this);
mPopup->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
Override host's resizeEvent and moveEvent and adjust popup's geometry there using button's geometry.
void Host::adjustPopup() {
if (!mPopup->isVisible()) {
return;
}
QRect rect = mButton->geometry();
QPoint bottomLeft = this->mapToGlobal(rect.bottomLeft());
mPopup->setGeometry(QRect(bottomLeft, QSize(rect.width(),200)));
}
void Host::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
adjustPopup();
}
void Host::moveEvent(QMoveEvent *event)
{
QWidget::moveEvent(event);
adjustPopup();
}
full source: button-popup

Related

not calling mousemove event on widgets and graphics view

I have created a set of wizard pages inherited from qwizardpage. in one of my wizard page i have a graphics view and in that using graphics scene and a qrect a rectangle has been created and now i am trying to catch the callback event of mouse move when the mouse is in the graphicsview area, but outside this area only call back is happening. But mouse press event is functioning properly. could anyone suggest me any solution.
WizardPage::WizardPage(QWidget* parent)
{
m_pScene = new QGraphicsScene(this);
graphicsView->setScene(m_pScene);
m_pRect = new QRect(-25, 25, 100, 40);
m_pScene->addRect(*m_pRect);
setMouseTracking(true);
}
void EgoLeverWizardPage::mouseMoveEvent(QMouseEvent *event)
{
qDebug() << "move";
}

Access coordinates from the parent of a widget

I have a QGraphicsView inside the MainWindow that has implemented a QGraphicScene. I need to pop up a widget when I right click the mouse on a certain portion of the QGraphicScene. The parent of the widget needs to be MainWindow.
My problem is that I need to verify the validity of the portion on witch I clicked inside a mousePressEvent in QGraphicScene and to pop up the widget at the exact same location but the coordinates of the QGraphicScene and MainWindow are obvious not the same. For that I use a custom signal that trigger a slot inside MainWindow and get the coordinates from the mousePressEvent of the MainWindow. The problem is that the mouseEvent from the QGraphicsScene is triggered before the mouseEvent from MainWindow. This makes perfect sense and works if I right click twice but I need it to work from the first right click.
I can not implement a filter or change the focus because I have tons of events in the application.
QGraphicScene:
void CGraphicScene :: mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(event -> button() == Qt::RightButton)
{
//test stuff
emit signalChangeContextualMenuPosition();
m_contextualMenu -> show();
}
}
MainWindow:
CGraphicScene *scene;
CContextualMenu *m_contextualMenu;
m_contextualMenu = new CContextualMenu(this);
m_contextualMenu ->close();
scene = new CGraphicScene(m_contextualMenu);
ui->gvInterface -> setScene(scene);
connect(scene, SIGNAL(signalChangeContextualMenuPosition()), this, SLOT(openPopUp()));
void MainWindow :: openPopUp()
{
m_contextualMenu ->move(m_xCoordPopMenu, m_yCoordPopMenu);
}
void MainWindow :: mousePressEvent(QMouseEvent *event)
{
if(event -> button() == Qt::RightButton)
{
m_xCoordPopMenu = event -> x();
m_yCoordPopMenu = event -> y();
}
}
Use the QGraphicsView::mapFromScene() to map scene coordinates to the view widget coordinates and then QWidget::mapToParent() to map the coordinates to it's parent widget, which is probably your main window. You can also find useful the method QWidget::mapTo().

Qt mouseMoveEvent not firing when mouse pressed

I have a custom QGraphicsScene in which I have a mouseMoveEvent(QGraphicsSceneMouseEvent *event);
When I hover on the scene with the mouse, the mouseMoveEvent gets properly fired. However when I hover with a mouse button pressed, then it does not get fired anymore.
This is how I setup the whole scene in the main window:
scene = new NodeScene(this); -> My Custom QGraphicsScene class
scene->setSceneRect(QRectF(0, 0, 5000, 5000));
QHBoxLayout *layout = new QHBoxLayout;
view = new QGraphicsView(scene);
layout->addWidget(view);
view->setDragMode(QGraphicsView::RubberBandDrag);
view->setMouseTracking(true);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
scene->setCentralWidget(widget);
And here is the code where I do handle mouse events (it's for Maya execution):
void NodeScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
MGlobal::displayInfo("Move");
QGraphicsScene::mouseMoveEvent(event);
}
void NodeScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
MGlobal::displayInfo("Press");
QGraphicsScene::mousePressEvent(event);
}
void NodeScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
MGlobal::displayInfo("Release");
QGraphicsScene::mouseReleaseEvent(event);
}
Any idea how I can get the mouseMoveEvents even when a mouse button is pressed ?
It sounds like you're not implementing all of the mouse events, just the mouseMoveEvent.
When overriding a mouse event, you should handle all of them (move, press and release events).
You can then set a boolean in the press event to know whether or not the mouse button is held down when entering the mouseMove event.
Found the issue by comparing my code to other examples, and it is due to the setDragMode(QGraphicsView::RubberBandDrag);
line. The code should by default be on QGraphicsView::NoDrag and the RubberBandDrag be enabled only upon press.

Qt5 QDockWidget with Fixed Height CentralWidget

I'm trying to use dock widgets on the main window class in Qt5. However, when I set the central widget to have a fixed height Qt has trouble docking the windows to the top or bottom. Basically, it looks like there is some "padding" or "margins" above and below the central widget. If I set an auto height on the widget, the docking works fine all they way edge-to-edge (top/bottom). How can I either remove the margins or enable the docking function while using a fixed height central widget?
See screenshots for example.
Dock Right w/ Auto Height (No Margins on Central Widget)
Dock Bottom w/ Auto Height (No Margins on Central Widget)
Dock Bottom w/ Fixed Height (Margins/Padding--Grey areas, won't dock)
Here is the code if that helps.
Header:
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QString);
~MainWindow();
private:
void createDockWindows();
QListWidget *m_dock_list;
QString m_directory;
QWidget *m_mainWidget;
};
Class definition:
MainWindow::MainWindow(QString program)
: m_directory(".")
{
m_mainWidget = new QWidget;
m_mainWidget->setFixedHeight(156);
m_mainWidget->setStyleSheet("background-color: blue;");
createDockWindows();
// set central widget and default size
setCentralWidget(m_mainWidget);
}
// dock functions
void MainWindow::createDockWindows()
{
QDockWidget *dock = new QDockWidget(tr("Dock List"), this);
dock->setAllowedAreas(Qt::LeftDockWidgetArea |
Qt::RightDockWidgetArea |
Qt::BottomDockWidgetArea);
m_dock_list = new QListWidget(dock);
m_dock_list->addItems(QStringList()
<< "item 1"
<< "item 2"
<< "item 3"
<< "item 4");
dock->setWidget(m_dock_list);
addDockWidget(Qt::RightDockWidgetArea, dock);
}
In case anyone else tries to figure out this behavior, here is what I've found. The top and bottom margins are just Qt trying to place the fixed size widget into a space larger than the widget (obvious). However, the restricted docking ability results from the docking widget having a minimum vertical size. Thus, when window height < central widget height + min(docking widget height) Qt does not allow the dock function. Once the threshold window height is reached (by user resize) then Qt allows the dock function.
I would prefer if the docking feature occurred regardless of window height with an automatic window resize to accommodate the docking window.
Solution using signals
Arguably a bit hacky, and may be hard to maintain if you have a complex layout. But for my use case, this is workable.
Don't enforce the size on the center widget, let it auto-fill the main window.
When the dock changes location it emits the dockLocationChanged. We can auto-size the main window if the dockable window is moved into target location. For my case this is the bottom.
Code:
void MainWindow::createDockWindows()
{
...
QObject::connect(dock, SIGNAL(dockLocationChanged(Qt::DockWidgetArea)),
this, SLOT(s_dock_window_resize(Qt::DockWidgetArea)));
}
// Autosize window slot on dock location change.
void MainWindow::s_dock_window_resize(Qt::DockWidgetArea area)
{
if (area == Qt::DockWidgetArea::BottomDockWidgetArea) {
// m_mainWidget_min_height is the height we would have enforced
// on the widget. Now we set it somewhere else and check it here.
int min_height = m_dock_list->height() + m_mainWidget_min_height;
if (min_height > MainWindow::height()) {
MainWindow::resize(MainWindow::width(), min_height);
}
}
}

How to get QGridLayout item by clicked QPoint?

I have grid of QFrames in QGridLayout and a popup menu with some actions, which are targeted on the cell where mouse right click happens. On the implementation of ContextMenuEvent I get clicked QPoint using common event->pos() but how I get access to my correct cell object by that point? Or is there some better alternative solution path for this purpose?
void X::contextMenuEvent(QContextMenuEvent* event)
{ // QPoint target = event->pos();
// TODO: m_gridLayout-> ...
// myDerivedCell->setSomething();
}
There are a bunch of solutions here. The simplest is to go through your widgets, calling bool QWidget::underMouse () const. My favorite is this:
frame_i->setContextMenuPolicy(Qt::CustomContextMenu);
connect(frame_i, SIGNAL(customContextMenuRequested(QPoint))
, SLOT(onContextMenu(QPoint)));
...
void X::onContextMenu(const QPoint &pos)
{
QFrame *w = qobject_cast < QFrame * >(sender());
...
}