【发布时间】:2021-03-13 01:14:31
【问题描述】:
我正在尝试使用在设计器模式下制作的工具栏中的 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
【问题讨论】:
-
您的问题不完整。如果您发布了complete Question,您将添加您在其中创建
alignmentGroup的方式,您会看到它可能是您设置该组的方式,即缺少QActionGroup::ExclusionPolicy::Exclusive。没有ui->actionUsePointer->setChecked(true);不算数,因为我们不知道ui是谁 -
其实你说得对,我是新手,不太擅长写这种帖子。我会改进的。ui是一个很大的.xml文件,由Qt Designer编辑器创建
-
我没疯,表单编辑器中确实缺少操作组:forum.qt.io/topic/27282/…