我有一个弹出窗口QMenu在QListView中显示很多图标:
QMenu*menu=createMenu();
QListView*list=createList();
QWidgetAction*action=new QWidgetAction(menu);
action->setDefaultWidget(list);
menu->addAction(action);
menu->show();
有没有办法让它可以调整大小? 即我希望能够拖动弹出菜单的角落并用鼠标调整弹出菜单的大小。我无法在谷歌或Qt文档中找到它。
答案 0 :(得分:0)
您需要创建自定义弹出窗口小部件。我不知道如何正确地捕捉焦点事件,但常见的想法是:
<强> MegaPopup.h 强>
#ifndef MEGAPOPUP_H
#define MEGAPOPUP_H
#include <QWidget>
#include <QPointer>
#include <QListView>
#include <QStandardItemModel>
#include <QVBoxLayout>
#include <QAction>
#include <QVariant>
#include <QCursor>
class MegaPopup
: public QWidget
{
Q_OBJECT
private:
enum Roles
{
ActionRole = Qt::UserRole,
};
public:
MegaPopup( QWidget *parent );
~MegaPopup();
void addItem( QAction *action );
public slots:
void showMenu();
private:
QPointer< QListView > m_view;
QPointer< QStandardItemModel > m_model;
private slots:
void onClicked( const QModelIndex& index );
void onDestroyed();
};
Q_DECLARE_METATYPE( QAction * )
#endif // MEGAPOPUP_H
<强> MeraPopup.cpp 强>
#include "MegaPopup.h"
MegaPopup::MegaPopup( QWidget *parent )
: QWidget( parent, Qt::Window | Qt::CustomizeWindowHint )
{
qRegisterMetaType< QAction * >();
m_model = new QStandardItemModel( this );
m_view = new QListView( this );
QVBoxLayout *l = new QVBoxLayout();
setLayout( l );
l->addWidget( m_view );
m_view->setModel( m_model );
m_view->setViewMode( QListView::IconMode );
connect( m_view, &QListView::clicked, this, &MegaPopup::onClicked );
}
MegaPopup::~MegaPopup()
{
}
void MegaPopup::addItem( QAction* action )
{
Q_ASSERT( action );
QStandardItem *item = new QStandardItem();
// Magic
item->setData( QVariant::fromValue< QAction * >( action ), ActionRole );
item->setEnabled( action->isEnabled() );
item->setFont( action->font() );
item->setIcon( action->icon() );
item->setText( action->text() );
item->setToolTip( action->toolTip() );
m_model->appendRow( item );
connect( action, &QAction::destroyed, this, &MegaPopup::onDestroyed );
}
void MegaPopup::showMenu()
{
move( QCursor::pos() );
show();
}
void MegaPopup::onClicked( const QModelIndex& index )
{
QStandardItem *item = m_model->itemFromIndex( index );
QAction *action = item->data( ActionRole ).value< QAction * >();
action->trigger();
hide();
}
void MegaPopup::onDestroyed()
{
QAction *toDel = qobject_cast< QAction * >( sender() );
const int n = m_model->rowCount();
for ( int i = 0; i < n; i++ )
{
QStandardItem *item = m_model->item( i );
QAction *action = item->data( ActionRole ).value< QAction * >();
if ( action == toDel )
{
delete item;
break;
}
}
}
答案 1 :(得分:0)
试试这样:
...
QListView*list=createList();
Qt::WindowFlags winFlags = list->windowFlags();
winFlags = winFlags & ~Qt::MSWindowsFixedSizeDialogHint;
list->setWindowFlags(winFlags);
...
menu