Qt学习笔记(自定义控件)

#-------------------------------------------------
#
# Project created by QtCreator 2019-08-17T08:42:10
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = 03_01_SmallWidget
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        mainwindow.cpp \
        mysmallwidget.cpp

HEADERS += \
        mainwindow.h \
        mysmallwidget.h

FORMS += \
        mainwindow.ui \
        mysmallwidget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
#ifndef MYSMALLWIDGET_H
#define MYSMALLWIDGET_H

#include <QWidget>

namespace Ui {
class MySmallWidget;
}

class MySmallWidget : public QWidget
{
    Q_OBJECT

public:
   //提供对外接口
    void setValue(int v);
    int getValue();

    explicit MySmallWidget(QWidget *parent = nullptr);
    ~MySmallWidget();

private:
    Ui::MySmallWidget *ui;
};

#endif // MYSMALLWIDGET_H
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    //点击设置  到一半位置
    connect(ui->btnSet,&QPushButton::clicked,this,[=](){
        ui->widget->setValue(50);
    });

    //点击获取 拿到当前值
    connect(ui->btnget,&QPushButton::clicked,this,[=](){
        qDebug()<<"当前值为:"<<ui->widget->getValue();
    });

}





MainWindow::~MainWindow()
{
    delete ui;
}
#include "mysmallwidget.h"
#include "ui_mysmallwidget.h"

MySmallWidget::MySmallWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MySmallWidget)
{
    ui->setupUi(this);
    //QSpinBox移动, Slider 跟着移动

    void(QSpinBox::*signal)(int)=&QSpinBox::valueChanged;
    connect(ui->spinBox,signal,ui->horizontalSlider,&QSlider::setValue);
    //反过来也要能行
    //Slider 移动 SpinBox 跟着移动
    connect(ui->horizontalSlider,&QSlider::valueChanged,ui->spinBox,&QSpinBox::setValue);



}

//实现提供的方法
void MySmallWidget::setValue(int v){
    ui->spinBox->setValue(v);
}

int MySmallWidget::getValue(){
    return ui->spinBox->value();
}


MySmallWidget::~MySmallWidget()
{
    delete 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>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="MySmallWidget" name="widget" native="true">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>40</y>
      <width>261</width>
      <height>80</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="btnget">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>190</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>获取值</string>
    </property>
   </widget>
   <widget class="QPushButton" name="btnSet">
    <property name="geometry">
     <rect>
      <x>180</x>
      <y>190</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>获取到一半</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <customwidgets>
  <customwidget>
   <class>MySmallWidget</class>
   <extends>QWidget</extends>
   <header location="global">mysmallwidget.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

自定义控件 

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MySmallWidget</class>
 <widget class="QWidget" name="MySmallWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>352</width>
    <height>57</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QWidget" name="layoutWidget">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>20</y>
     <width>281</width>
     <height>24</height>
    </rect>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <widget class="QSpinBox" name="spinBox"/>
    </item>
    <item>
     <widget class="QSlider" name="horizontalSlider">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

提供前景框 背景框  进度条图片就可以完善 进度条图片就可以完善
功能只局限于三个图片

自己封装控件
频繁大量使用控件,但控件功能不完善,就可以自己封装控件

把两个控件封装成一个控件

添加 Qt 设计师界面类
选项 选Widget
下一步
对控件起名

把刚才想封装的控件放进去,
整体做水平布局

添加widget容器
右键提升为 第二项写小控件的类名

点击全局包含
点击添加
点击提升

点全局包含方便下一次使用
多次使用

做功能上的东西
对小空间的 h cpp文件做操作

自定义控件封装

  创建Qt-一个设计师界面类
  拖拽一个Wideget 右键 提升为 ----类名写入----全局包含------添加------提升------Widget就变成了想要的效果
  QSpinBox 移动 Slider 跟着移动
  对外接口 setValue() getValue
  

上一篇:1 Qt框架初探


下一篇:c – 当objectname包含特定字符串时,QWidget findChildren