qt中提升控件的操作

264 阅读1分钟

我的代码里面自己写了一个类myview,继承自QGraphicsView,然后就把拖到界面上的QGraphicsView控件通过提升控件的方式替换成自己的myview类。

先在工具栏拖一个QGraphicsView到主界面上,如下图:
在这里插入图片描述
右击界面上的Graphics view——选择“提升为”,如下图:
在这里插入图片描述

在提升的类名称:输入自己写的类名(注意大小写要一模一样!),如下图:
在这里插入图片描述

然后勾选上面的全局包含勾选框——点击提升,如下图:
在这里插入图片描述
到现在为止就算是提升好了,我们在mainwindow.ui文件里面就可以看到你拖到界面上的控件已经被替换成自己的类了,如下:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <widget class="MySlider" name="verticalSlider">
       <property name="minimumSize">
        <size>
         <width>50</width>
         <height>100</height>
        </size>
       </property>
       <property name="orientation">
        <enum>Qt::Vertical</enum>
       </property>
      </widget>
     </item>
     <item>
      <widget class="MyView" name="graphicsView">
       <property name="verticalScrollBarPolicy">
        <enum>Qt::ScrollBarAlwaysOff</enum>
       </property>
       <property name="horizontalScrollBarPolicy">
        <enum>Qt::ScrollBarAlwaysOff</enum>
       </property>
      </widget>
     </item>
    </layout>
   </item>
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout_2">
     <item>
      <widget class="QPushButton" name="pushButton_2">
       <property name="minimumSize">
        <size>
         <width>80</width>
         <height>20</height>
        </size>
       </property>
       <property name="text">
        <string>上一页</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QPushButton" name="pushButton">
       <property name="minimumSize">
        <size>
         <width>80</width>
         <height>20</height>
        </size>
       </property>
       <property name="text">
        <string>下一页</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QPushButton" name="pushButton_3">
       <property name="text">
        <string>纵向</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QPushButton" name="pushButton_4">
       <property name="text">
        <string>横向</string>
       </property>
      </widget>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <customwidgets>
  <customwidget>
   <class>MyView</class>
   <extends>QGraphicsView</extends>
   <header>myview.h</header>
  </customwidget>
  <customwidget>
   <class>MySlider</class>
   <extends>QSlider</extends>
   <header location="global">myslider.h</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>