仍然没有弄清楚如何将 QListWidget 中的选择限制为一两个,但右键单击第二个项目时获得上下文菜单。
只需将选择模式设置为 QAbstractItemView::SingleSelection,然后重新实现 mousePressEvent() 和 contextMenuEvent()。
class MyListView : public QListWidget
{
Q_OBJECT
public:
ListView();
virtual ~ListView() {}
/** Add item to the list.
@param itemName Item's display name.
@param itemHandle Value to return if item selected.
*/
void AddItem(const QString itemName, const quint32 itemHandle);
private slots:
void slot_ItemSelectionChanged(void);
void slot_Option1(void);
void slot_Option1(void);
signals:
void signal_ItemSelectionChanged(const qint32 itemHandle);
private:
void mousePressEvent(QMouseEvent* mousePressEvent);
void contextMenuEvent(QContextMenuEvent* menuEvent);
QListWidgetItem *m_selectedItem; ///< Pointer to the currently selected item.
QListWidgetItem *m_rightClickedItem; ///< Pointer to right-clicked item in the list.
QAction* m_menuOption1; ///< Menu option 1.
QAction* m_menuOption2; ///< Menu option 2.
QMenu* m_myListContextMenu; ///< Menu with all List View menu actions.
};
MyListView::MyListView()
{
setSelectionMode(QAbstractItemView::SingleSelection);
// create my context menu with 2 options
m_menuOption1 = new QAction(tr("option 1"), this);
m_menuOption1 ->setEnabled(true);
m_menuOption2 = new QAction(tr("option 2"), this);
m_menuOption2 ->setEnabled(true);
m_myListContextMenu = new QMenu(this);
m_myListContextMenu ->addAction(m_menuOption1);
m_myListContextMenu ->addAction(m_menuOption2);
bool allConnected = true;
// process primary selection
allConnected &= connect(this, SIGNAL(itemSelectionChanged(void)), this, SLOT(slot_ItemSelectionChanged(void)));
// process menu option 1
allConnected &= connect(m_menuOption1, SIGNAL(triggered()), this, SLOT(slot_Option1()));
// process menu option 2
allConnected &= connect(m_menuOption2, SIGNAL(triggered()), this, SLOT(slot_Option2()));
if (!allConnected )
{
assert(0);
}
}
void ListView::slot_ItemSelectionChanged(void)
{
if (currentItem() != m_selectedItem)
{
m_selectedItem = currentItem();
emit signal_ItemSelectionChanged(m_selectedItem->type());
}
} // end of method slot_ItemSelectionChanged()
void MyListView::contextMenuEvent(QContextMenuEvent* menuEvent)
{
m_rightClickedItem = itemAt(menuEvent->pos());
// do my prep stuff here specific to right-clicked item
// in case one of menu options gets selected
m_listMenu->exec(menuEvent->globalPos());
} // end of method contextMenuEvent()
void MyListView::mousePressEvent(QMouseEvent* mousePressEvent)
{
// drop right mouse button event as it would otherwise cause a change of selection
if (!(mousePressEvent->buttons() & Qt::RightButton))
{
QListWidget::mousePressEvent(mousePressEvent);
}
} // end of method mousePressEvent()