QskMenu::exec added
This commit is contained in:
parent
89a2a395fd
commit
9cef7705d8
@ -85,18 +85,20 @@ class GraphicLabel : public QskGraphicLabel
|
|||||||
protected:
|
protected:
|
||||||
void mousePressEvent( QMouseEvent* event ) override
|
void mousePressEvent( QMouseEvent* event ) override
|
||||||
{
|
{
|
||||||
auto menu = new QskMenu( this );
|
QskMenu menu( this );
|
||||||
|
menu.setPopupFlag( QskPopup::DeleteOnClose, false );
|
||||||
|
|
||||||
menu->addItem( "image://shapes/Rectangle/White", "Print" );
|
menu.addItem( "image://shapes/Rectangle/White", "Print" );
|
||||||
menu->addItem( "image://shapes/Diamond/Yellow", "Save As" );
|
menu.addItem( "image://shapes/Diamond/Yellow", "Save As" );
|
||||||
menu->addItem( "image://shapes/Ellipse/Red", "Setup" );
|
menu.addItem( "image://shapes/Ellipse/Red", "Setup" );
|
||||||
menu->addItem( "image://shapes/Hexagon/PapayaWhip", "Help" );
|
menu.addItem( "image://shapes/Hexagon/PapayaWhip", "Help" );
|
||||||
|
|
||||||
menu->setOrigin( qskMousePosition( event ) );
|
menu.setOrigin( qskMousePosition( event ) );
|
||||||
menu->open();
|
|
||||||
|
|
||||||
connect( menu, &QskMenu::triggered,
|
if ( int result = menu.exec() )
|
||||||
this, []( int index ) { qDebug() << index; } );
|
{
|
||||||
|
qDebug() << result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include <qvector.h>
|
#include <qvector.h>
|
||||||
#include <qvariant.h>
|
#include <qvariant.h>
|
||||||
|
#include <qeventloop.h>
|
||||||
|
|
||||||
QSK_SUBCONTROL( QskMenu, Panel )
|
QSK_SUBCONTROL( QskMenu, Panel )
|
||||||
QSK_SUBCONTROL( QskMenu, Cell )
|
QSK_SUBCONTROL( QskMenu, Cell )
|
||||||
@ -37,6 +38,8 @@ class QskMenu::PrivateData
|
|||||||
QPointF origin;
|
QPointF origin;
|
||||||
|
|
||||||
int currentIndex = -1;
|
int currentIndex = -1;
|
||||||
|
int triggeredIndex = -1;
|
||||||
|
|
||||||
bool isPressed = false;
|
bool isPressed = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -328,7 +331,9 @@ void QskMenu::setSelectedIndex( int index )
|
|||||||
if ( index >= 0 )
|
if ( index >= 0 )
|
||||||
setCurrentIndex( index );
|
setCurrentIndex( index );
|
||||||
|
|
||||||
|
m_data->triggeredIndex = index;
|
||||||
Q_EMIT triggered( index );
|
Q_EMIT triggered( index );
|
||||||
|
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -344,4 +349,68 @@ int QskMenu::indexAtPosition( const QPointF& pos ) const
|
|||||||
this, contentsRect(), QskMenu::Cell, pos );
|
this, contentsRect(), QskMenu::Cell, pos );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
class EventLoop : public QEventLoop
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
EventLoop( QskMenu* menu )
|
||||||
|
: QEventLoop( menu )
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
We want menu being the parent, so that the loop can be found
|
||||||
|
by menu->findChild< QEventLoop* >().
|
||||||
|
*/
|
||||||
|
|
||||||
|
connect( menu, &QObject::destroyed, this, &EventLoop::reject );
|
||||||
|
connect( menu, &QskMenu::fadingChanged, this, &EventLoop::maybeQuit );
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void reject()
|
||||||
|
{
|
||||||
|
setParent( nullptr );
|
||||||
|
quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void maybeQuit()
|
||||||
|
{
|
||||||
|
auto menu = static_cast< const QskMenu* >( parent() );
|
||||||
|
if ( !menu->isOpen() && !menu->isFading() )
|
||||||
|
quit();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
int QskMenu::exec()
|
||||||
|
{
|
||||||
|
if ( isOpen() || isFading() )
|
||||||
|
{
|
||||||
|
qWarning() << "QskMenu::exec: menu is already opened";
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool deleteOnClose = popupFlags() & QskPopup::DeleteOnClose;
|
||||||
|
if ( deleteOnClose )
|
||||||
|
setPopupFlag( QskPopup::DeleteOnClose, false );
|
||||||
|
|
||||||
|
QPointer< QskMenu > menu = this;
|
||||||
|
menu->open();
|
||||||
|
|
||||||
|
EventLoop loop( this );
|
||||||
|
(void )loop.exec( QEventLoop::DialogExec );
|
||||||
|
|
||||||
|
int result = -1;
|
||||||
|
|
||||||
|
if ( menu )
|
||||||
|
{
|
||||||
|
result = menu->m_data->triggeredIndex;
|
||||||
|
|
||||||
|
if ( deleteOnClose )
|
||||||
|
delete menu;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
#include "moc_QskMenu.cpp"
|
#include "moc_QskMenu.cpp"
|
||||||
|
@ -66,6 +66,8 @@ class QSK_EXPORT QskMenu : public QskPopup
|
|||||||
QRectF cellRect( int index ) const;
|
QRectF cellRect( int index ) const;
|
||||||
int indexAtPosition( const QPointF& ) const;
|
int indexAtPosition( const QPointF& ) const;
|
||||||
|
|
||||||
|
Q_INVOKABLE int exec();
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
void cascadingChanged( bool );
|
void cascadingChanged( bool );
|
||||||
void originChanged( const QPointF& );
|
void originChanged( const QPointF& );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user