making the internal menu being modal for the window

This commit is contained in:
Uwe Rathmann 2023-03-06 07:47:49 +01:00
parent 2a2793422f
commit f60fe75de4
2 changed files with 24 additions and 22 deletions

View File

@ -9,6 +9,8 @@
#include "QskMenu.h" #include "QskMenu.h"
#include "QskTextOptions.h" #include "QskTextOptions.h"
#include <qquickwindow.h>
QSK_SUBCONTROL( QskComboBox, Panel ) QSK_SUBCONTROL( QskComboBox, Panel )
QSK_SUBCONTROL( QskComboBox, Graphic ) QSK_SUBCONTROL( QskComboBox, Graphic )
QSK_SUBCONTROL( QskComboBox, Text ) QSK_SUBCONTROL( QskComboBox, Text )
@ -24,16 +26,16 @@ class QskComboBox::PrivateData
PrivateData( QskComboBox* const box ) PrivateData( QskComboBox* const box )
: menu( new QskMenu( box ) ) : menu( new QskMenu( box ) )
{ {
menu->setPopupFlag( QskPopup::DeleteOnClose, false ); menu = new QskMenu();
#if 1
/* /*
CloseOnPressOutside catches all mouse events in the parent The popup menu is supposed to be modal for the window and
what is the comboBox in our case. However we need to handle therefore needs the root item of the window as parent item.
events for the complete window to make the CloseOnPressOutside So we set the box as QObject parent only, so that it gets
work like expected. TODO ... destroyed properly.
*/ */
#endif menu->setParent( box );
menu->setPopupFlag( QskPopup::DeleteOnClose, false );
} }
QskMenu* menu; QskMenu* menu;
@ -189,7 +191,15 @@ void QskComboBox::togglePopup()
void QskComboBox::openPopup() void QskComboBox::openPopup()
{ {
m_data->menu->open(); const auto cr = contentsRect();
auto menu = m_data->menu;
menu->setParentItem( window()->contentItem() );
menu->setOrigin( mapToScene( cr.bottomLeft() ) );
menu->setFixedWidth( cr.width() );
menu->open();
} }
void QskComboBox::closePopup() void QskComboBox::closePopup()
@ -197,21 +207,16 @@ void QskComboBox::closePopup()
m_data->menu->close(); m_data->menu->close();
} }
void QskComboBox::updateLayout()
{
Inherited::updateLayout();
auto origin = contentsRect().bottomLeft();
m_data->menu->setOrigin( origin );
m_data->menu->setFixedWidth( contentsRect().width() );
}
void QskComboBox::mousePressEvent( QMouseEvent* ) void QskComboBox::mousePressEvent( QMouseEvent* )
{ {
setPressed( true ); setPressed( true );
} }
void QskComboBox::mouseUngrabEvent()
{
setPressed( false );
}
void QskComboBox::mouseReleaseEvent( QMouseEvent* ) void QskComboBox::mouseReleaseEvent( QMouseEvent* )
{ {
releaseButton(); releaseButton();
@ -233,7 +238,6 @@ void QskComboBox::keyPressEvent( QKeyEvent* event )
releaseButton(); releaseButton();
} }
// always accepting
return; return;
} }
} }
@ -249,7 +253,6 @@ void QskComboBox::keyReleaseEvent( QKeyEvent* event )
void QskComboBox::clear() void QskComboBox::clear()
{ {
m_data->menu->clear(); m_data->menu->clear();
update(); update();
} }

View File

@ -79,9 +79,8 @@ class QSK_EXPORT QskComboBox : public QskControl
void placeholderTextChanged( const QString& ); void placeholderTextChanged( const QString& );
protected: protected:
virtual void updateLayout() override;
void mousePressEvent( QMouseEvent* ) override; void mousePressEvent( QMouseEvent* ) override;
void mouseUngrabEvent() override;
void mouseReleaseEvent( QMouseEvent* ) override; void mouseReleaseEvent( QMouseEvent* ) override;
void keyPressEvent( QKeyEvent* ) override; void keyPressEvent( QKeyEvent* ) override;