• In this paper, we implement the classic slide display control part and attached simple functions, mainly aims to study the realization method of custom controls the transplantation of encapsulated into their convenient control code, we should also learn to use your wheels, without having to make the wheel every time, so that it can improve the development efficiency of individual developers. The main effect diagram of the control in this paper is as follows:

Functions:

  1. QSpinBox up and down add or subtract, QSlider to follow the left and right.
  2. The QSlider slides left and right, and the numbers in the QSpinBox change accordingly.
  3. The QSlider slides, and the QLabel simultaneously displays the values in the QSpinBox.
  4. The encapsulated controls provide the necessary interface functions that can be called from the home screen or other interfaces.
  5. The two buttons are: get the current value and set to half; the front button is used to get the value in the QSpinBox after clicking and display it on the LCD Number control in the middle; the back button is used to slide the QSlider to the half of the total length and stop when clicking.
  • The above is a simple custom control written by the individual, which is just the encapsulation of Qt built-in three controls, respectively, QSpinBox, QSlider, QLabel, these three controls into a SliderandSpinbox control, need to use the main method is: Add sliderandSpinbox.h and sliderandSpinbox.cpp to your project, drag and drop a Widget into the UI, right-click on the Widget, add the class named SliderandSpinBox, and select Add. The class name will be displayed in the list box above, and then click the promote button to complete the operation. You can see that the class corresponding to the window changed from the QWidget to sliderandSpinBox. Run the program again, and the widget will display our custom window.

  • Going back to the implementation process in this article, first create a new project with the BASE class QWidget with the UI, then add a design-time interface class, which basically encapsulates the control, select the UI without any buttons in the Widget. Name sliderandSpinbox.h, sliderandSpinbox.cpp, sliderandSpinbox.ui, mainly in the file to achieve the encapsulation of the control, and provide a certain interface function for the main interface or other interface call.

  • Sliderandspinbox.h: sliderandSpinbox.h
#ifndef SLIDERANDSPINBOX_H #define SLIDERANDSPINBOX_H #include <QWidget> namespace Ui { class SliderAndSpinBox; } class SliderAndSpinBox : public QWidget { Q_OBJECT public: explicit SliderAndSpinBox(QWidget *parent = 0); ~SliderAndSpinBox(); Int getNum(); Void setNum(int num); private: Ui::SliderAndSpinBox *ui; }; #endif // SLIDERANDSPINBOX_HCopy the code
  • SliderandSpinbox.cpp
#include "sliderandspinbox.h" #include "ui_sliderandspinbox.h" SliderAndSpinBox::SliderAndSpinBox(QWidget *parent) : QWidget(parent), ui(new Ui::SliderAndSpinBox) { ui->setupUi(this); ui->spinBox->setRange(0, 130); ui->horizontalSlider->setRange(0, 130); ui->spinBox->setValue(0); QString s = QString::number(getNum()); ui->label->setText(s); Void (QSpinBox:: * spinBox) (int) = &QSpinBox::valueChanged; void(QSpinBox:: * spinBox) (int) = &QSpinBox::valueChanged; Connect (UI ->spinBox, spinBox, UI ->horizontalSlider, &qslider ::setValue); Connect (UI ->horizontalSlider,&QSlider::valueChanged, UI ->spinBox,&QSpinBox::setValue); //QSlider slides the value inside the label along with the change using a lambda expression connect(ui->horizontalSlider,&QSlider::valueChanged,ui->label,[=](){ QString s = QString::number(getNum()); ui->label->setText(s); }); Connect (UI ->spinBox, SIGNAL(valueChanged(int)), UI ->horizontalSlider, SLOT(setValue(int))); connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), ui->spinBox, SLOT(setValue(int))); Int SliderAndSpinBox::getNum() {UI ->spinBox->value(); } void SliderAndSpinBox::setNum(int num) {UI ->spinBox->setValue(num); } SliderAndSpinBox::~SliderAndSpinBox() { delete ui; }Copy the code
  • The sliderandSpinbox.ui layout is as follows:

  • So far encapsulates a simple slide display control, the code basically has comments, not a talk, the problem can leave a message exchange, progress together.
  • The next widget.h code looks like this:
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;

};

#endif // WIDGET_H
Copy the code
  • The code for widget. CPP is as follows:
#include "widget.h" #include "ui_widget.h" #include <QDebug> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); Connect (UI ->btn_get, &qpushButton ::clicked,[=](){UI ->lcdNumber->display(UI ->widget->getNum())); }); Connect (UI ->btn_set, &qpushbutton ::clicked,[=](){UI ->widget->setNum(65); }); } Widget::~Widget() { delete ui; }Copy the code
  • The widget. UI layout is as follows:

  • Above is a personal simple write a custom control, relatively simple, welcome to leave a message to learn communication, there is a need to source code can pay attention to the public number to ask, thank you.