[英]Qt QActionGroup with QAction in toolbar
我正在尝试在设计器模式下制作的工具栏中使用 QAction 按钮创建一个 QActionGroup。
目标是只点击一个按钮。 我遵循了这个示例https://doc.qt.io/qt-5/qactiongroup.html#details但它不会使我的按钮独占。 我也试图强制alignmentGroup.setExclusive(true);
mainwindow.cpp,在 MainWindow::MainWindow(QWidget *parent)
alignmentGroup.addAction(ui->actionUsePointer);
alignmentGroup.addAction(ui->actionMoveView);
alignmentGroup.addAction(ui->actionEditText);
ui->actionUsePointer->setChecked(true);
我按照示例进行操作,但是例如,当我单击 actionUsePointer 时,我可以检查所有按钮而无需取消选中第一个按钮...
编辑这是 mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1098</width>
<height>815</height>
</rect>
</property>
<property name="windowTitle">
<string>FSM Editor</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QGraphicsView" name="graphicsView">
<property name="styleSheet">
<string notr="true">background-color:grey;</string>
</property>
</widget>
<widget class="QTextBrowser" name="textBrowser">
<property name="maximumSize">
<size>
<width>300</width>
<height>16777215</height>
</size>
</property>
</widget>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1098</width>
<height>22</height>
</rect>
</property>
<property name="nativeMenuBar">
<bool>false</bool>
</property>
<widget class="QMenu" name="menuFichier">
<property name="title">
<string>Fichier</string>
</property>
<addaction name="actionOuvrir"/>
<addaction name="actionR_cemment_ouvert"/>
<addaction name="actionEnregistrer"/>
<addaction name="actionEnregistrer_sous"/>
</widget>
<widget class="QMenu" name="menu_dition">
<property name="title">
<string>Édition</string>
</property>
<addaction name="actionAnnuler"/>
<addaction name="actionRevenir"/>
</widget>
<widget class="QMenu" name="menuBackground">
<property name="title">
<string>Background</string>
</property>
<addaction name="actionAucun"/>
<addaction name="actionQuadrillage"/>
</widget>
<addaction name="menuFichier"/>
<addaction name="menu_dition"/>
<addaction name="menuBackground"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<widget class="QToolBar" name="toolBar">
<property name="windowTitle">
<string>toolBar</string>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionUsePointer"/>
<addaction name="actionMoveView"/>
<addaction name="actionEditText"/>
<addaction name="separator"/>
<addaction name="actionShowEditor"/>
</widget>
<action name="actionOuvrir">
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/img/folder-open.png</normaloff>:/img/folder-open.png</iconset>
</property>
<property name="text">
<string>Ouvrir...</string>
</property>
</action>
<action name="actionR_cemment_ouvert">
<property name="text">
<string>Récemment ouvert</string>
</property>
</action>
<action name="actionEnregistrer">
<property name="text">
<string>Enregistrer</string>
</property>
</action>
<action name="actionEnregistrer_sous">
<property name="text">
<string>Enregistrer sous</string>
</property>
</action>
<action name="actionAnnuler">
<property name="text">
<string>Annuler</string>
</property>
</action>
<action name="actionRevenir">
<property name="text">
<string>Revenir</string>
</property>
</action>
<action name="actionUsePointer">
<property name="checkable">
<bool>true</bool>
</property>
<property name="enabled">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/img/mouse-pointer.png</normaloff>:/img/mouse-pointer.png</iconset>
</property>
<property name="text">
<string>UsePointer</string>
</property>
<property name="iconVisibleInMenu">
<bool>true</bool>
</property>
</action>
<action name="actionMoveView">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/img/arrows.png</normaloff>:/img/arrows.png</iconset>
</property>
<property name="text">
<string>moveView</string>
</property>
</action>
<action name="actionEditText">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/img/i-cursor.png</normaloff>:/img/i-cursor.png</iconset>
</property>
<property name="text">
<string>EditText</string>
</property>
</action>
<action name="actionAucun">
<property name="text">
<string>Aucun</string>
</property>
</action>
<action name="actionQuadrillage">
<property name="text">
<string>Quadrillage</string>
</property>
</action>
<action name="actionShowEditor">
<property name="text">
<string>Afficher l'XML</string>
</property>
</action>
</widget>
<resources>
<include location="icons.qrc"/>
</resources>
<connections/>
</ui>
编辑2:我没疯,表单编辑器中确实缺少操作组功能,它应该是: https://forum.qt.io/topic/27282/qt-creator-2-7-x-creating -action-groups-in-form-editor/2
我用这个解决了我的问题: https://forum.qt.io/topic/27282/qt-creator-2-7-x-creating-action-groups-in-form-editor/3
似乎 GUI Qt 编辑器不允许在视觉上添加 ActionGroup,因此您必须触摸 raw.xml ui。
我用文本编辑器而不是图形编辑器打开了 mainwindow.ui,并在里面创建了 ActionGroup(我称之为 GroupTool),里面有我的所有动作
<actiongroup name="GroupTool">
<action name="actionUsePointer">
<property name="checkable">
<bool>true</bool>
</property>
<property name="enabled">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/img/mouse-pointer.png</normaloff>:/img/mouse-pointer.png</iconset>
</property>
<property name="text">
<string>UsePointer</string>
</property>
<property name="autoRepeat">
<bool>true</bool>
</property>
<property name="iconVisibleInMenu">
<bool>true</bool>
</property>
</action>
<action name="actionMoveView">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/img/arrows.png</normaloff>:/img/arrows.png</iconset>
</property>
<property name="text">
<string>moveView</string>
</property>
</action>
<action name="actionEditText">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/img/i-cursor.png</normaloff>:/img/i-cursor.png</iconset>
</property>
<property name="text">
<string>EditText</string>
</property>
</action>
</actiongroup>
最后在 mainwindow.cpp 中:
ui->GroupTool->setExclusive(true);
问题中链接的示例大部分是其中的大部分,使用 Qt Designer 的表单需要进行一些小调整。
诀窍是您可以在 Qt 设计器创建操作后将操作添加到 QActionGroup。
在表格 header 文件(mainwindow.h)中,添加:QActionGroup *groupTool;
在连接信号/插槽的 MainWindow 构造函数中,创建操作组,将操作添加到其中,并将其设置为独占:
groupTool = new QActionGroup(this);
groupTool.addAction(ui->actionUsePointer);
...
groupTool.setExclusive(true);
在 Qt 设计器中:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.