Qt之自定义下拉菜单,支持单选和多选

网友投稿 1565 2022-09-28

Qt之自定义下拉菜单,支持单选和多选

Qt之自定义下拉菜单,支持单选和多选

QHLineEdit.h

#ifndef QHLineEdit_H#define QHLineEdit_H#include #include QT_BEGIN_NAMESPACEclass QAction;class QActionGroup;QT_END_NAMESPACEclass QHLineEdit : public QLineEdit{ Q_OBJECTpublic: explicit QHLineEdit(QWidget *parent = nullptr);};#endif // QHLineEdit_H

QHLineEdit.cpp

#include "QHLineEdit.h"#include #include #include #include #include #include #include #include #include QHLineEdit::QHLineEdit(QWidget *parent) : QLineEdit(parent){ setClearButtonEnabled(true); QMenu *menu = new QMenu(this); QAction *levelOneActon = menu->addAction(tr("Level One")); levelOneActon->setCheckable(true); QAction *levelTwoAction = menu->addAction(tr("Level Two")); levelTwoAction->setCheckable(true); menu->addSeparator(); QActionGroup *patternGroup = new QActionGroup(this); patternGroup->setExclusive(true); QAction *patternAction = menu->addAction("Fixed String"); patternAction->setData(QVariant(int(QRegExp::FixedString))); patternAction->setCheckable(true); patternAction->setChecked(true); patternGroup->addAction(patternAction); patternAction = menu->addAction("Regular Expression"); patternAction->setCheckable(true); patternAction->setData(QVariant(int(QRegExp::RegExp2))); patternGroup->addAction(patternAction); patternAction = menu->addAction("Wildcard"); patternAction->setCheckable(true); patternAction->setData(QVariant(int(QRegExp::Wildcard))); patternGroup->addAction(patternAction); const QIcon icon = QIcon(QPixmap(":/icons/find.png")); QToolButton *optionsButton = new QToolButton; optionsButton->setCursor(Qt::ArrowCursor); optionsButton->setFocusPolicy(Qt::NoFocus); optionsButton->setStyleSheet("* { border: none; }"); optionsButton->setIcon(icon); optionsButton->setMenu(menu); optionsButton->setPopupMode(QToolButton::InstantPopup); QWidgetAction *optionsAction = new QWidgetAction(this); optionsAction->setDefaultWidget(optionsButton); addAction(optionsAction, QLineEdit::LeadingPosition);}

如果要获取QActionGroup中选中的菜单,使用patternGroup->checkedAction()

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:外卖广告大规模深度学习模型工程实践
下一篇:H.264(H264)解码SPS获取分辨率和帧率
相关文章

 发表评论

暂时没有评论,来抢沙发吧~