【问题标题】:Qt QActionGroup with QAction in toolbar工具栏中带有 QAction 的 Qt QActionGroup
【发布时间】: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); 

my ui designer objects

我按照示例进行操作,但是当我单击 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-&gt;actionUsePointer-&gt;setChecked(true); 不算数,因为我们不知道ui 是谁
  • 其实你说得对,我是新手,不太擅长写这种帖子。我会改进的。ui是一个很大的.xml文件,由Qt Designer编辑器创建
  • 我没疯,表单编辑器中确实缺少操作组:forum.qt.io/topic/27282/…

标签: c++ qt


【解决方案1】:

我用这个解决了我的问题: https://forum.qt.io/topic/27282/qt-creator-2-7-x-creating-action-groups-in-form-editor/3

GUI Qt 编辑器似乎不允许在视觉上添加 ActionGroup,因此您必须触摸原始 .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);

【讨论】:

    【解决方案2】:

    问题中链接的示例是大部分内容,使用 Qt Designer 的表单需要进行一些小调整。

    诀窍是您可以在 Qt 设计器创建操作后将操作添加到 QActionGroup。

    在表单头文件(mainwindow.h)中,添加: QActionGroup *groupTool;

    在连接信号/插槽的 MainWindow 构造函数中,创建操作组,将操作添加到其中,并将其设置为独占:

    groupTool = new QActionGroup(this);
    groupTool.addAction(ui->actionUsePointer);
    ...
    groupTool.setExclusive(true);
    

    在 Qt 设计器中:

    • 将每个操作的 checkable 属性设置为 true
    • 将其中一项操作的选中属性设置为 true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多