首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >工具栏中带有QAction的Qt QAction

工具栏中带有QAction的Qt QAction
EN

Stack Overflow用户
提问于 2020-06-04 10:00:26
回答 2查看 1.6K关注 0票数 1

我试图在设计器模式下的工具栏中使用QActionGroup按钮制作一个QAction。

目标是只点击一个按钮。我遵循这个例子,https://doc.qt.io/qt-5/qactiongroup.html#details,但它并不使我的按钮排他性。我还试着强迫alignmentGroup.setExclusive(true);

mainwindow.cpp,以MainWindow::MainWindow(QWidget *父)

代码语言:javascript
复制
        alignmentGroup.addAction(ui->actionUsePointer);
        alignmentGroup.addAction(ui->actionMoveView);
        alignmentGroup.addAction(ui->actionEditText);
        ui->actionUsePointer->setChecked(true); 

我的ui设计器对象

我遵循了这个示例,但是当我单击actionUsePointer时,我可以检查所有的按钮,而不需要取消第一个按钮.

编辑这里是mainwindow.ui

代码语言:javascript
复制
<?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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-04 13:41:32

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

GUI似乎不允许在视觉上添加ActionGroup,因此您必须触摸原始的.xml ui。

我用文本编辑器而不是图形编辑器打开mainwindow.ui,并在里面创建了ActionGroup (我称之为GroupTool),其中包含了所有的操作。

代码语言:javascript
复制
<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中:

代码语言:javascript
复制
ui->GroupTool->setExclusive(true);
票数 3
EN

Stack Overflow用户

发布于 2020-11-14 19:41:22

问题中链接的示例大部分是它,使用来自Qt设计器的表单需要一个小的调整。

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

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

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

代码语言:javascript
复制
groupTool = new QActionGroup(this);
groupTool.addAction(ui->actionUsePointer);
...
groupTool.setExclusive(true);

在Qt设计中:

  • 将每个操作的checkable属性设置为true。
  • 为其中一个操作将选中的属性设置为true。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62191890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档