Qt slide design and implementation, qt slide implementation

Source: Internet
Author: User

Qt slide design and implementation, qt slide implementation

I did not find the slider control of Qt, so I wrote one myself to implement brightness adjustment, volume adjustment, and other functions.

The effect is as follows:

Main design ideas:

Some adjustment functions, such as the contrast value, must be able to set the value range of the slider, not limited to 0 ~ 100

When you drag the mouse, the sliding block can move with the mouse, and the number on the right also changes.

When the mouse is released, the numeric value is set successfully to trigger an operation.

Code:

The entire window inherits QWdiget, draw a horizontal line, and the slider is also a crude line.

After the value is set successfully, the operation uses the callback function to design a callback base class. After the value is set successfully, the callback base class is called.

Override the Event Response Function for Mouse clicking, moving, and releasing. When you press the mouse, set the value of the variable mousedown to true. When the mouse moves, determine whether the value of mousedown is true. If the value is true, then, the coordinates of the slider are updated, and the START window is re-painted.

When the mouse is released, the value of mousedown is set to false. Like sliding the mouse, the coordinates of the slider are updated, the window is re-painted, and the value setting is completed by calling the callback function.

 

Code:

Callback base class:

 1 #ifndef TRACKBARCALLBACK_H 2 #define TRACKBARCALLBACK_H 3  4 class TrackBarCallBack 5 { 6 public: 7     TrackBarCallBack(); 8     virtual void callBack(int value); 9 };10 11 #endif // TRACKBARCALLBACK_H

 

1 #include "trackbarcallback.h"2 3 TrackBarCallBack::TrackBarCallBack()4 {5 }6 7 void TrackBarCallBack::callBack(int value)8 {9 }
TrackBarCallBack. cpp

 

Slide category:

Trackbar. h

1 # ifndef TRACKBAR_H 2 # define TRACKBAR_H 3 # include <QWidget> 4 # include "trackbarcallback. h "5 6 class TrackBar: public QWidget 7 {8 public: 9 TrackBar (TrackBarCallBack * callBack, int width = 390, int height = 40, int start = 0, int end = 100); 10 void setPosition (int position); 11 void mouseReleaseEvent (QMouseEvent *); 12 void paintEvent (QPaintEvent *); 13 void mousePressEvent (QMouseEvent *); 14 void mouseMoveEvent (QMouseEvent *); 15 private: 16 TrackBarCallBack * callBack; 17 int start; // The value range starts from 18 int end; // The value range ends with 19 int positions; 20 bool mousedown; 21 int widthOfLine; // horizontal line length 22}; 23 24 # endif // TRACKBAR_H

 

Trackbar. cpp

1 # include "trackbar. h "2 # include <QWidget> 3 # include <QPainter> 4 # include <QMouseEvent> 5 6 TrackBar: TrackBar (TrackBarCallBack * callBack, int width, int height, int start, int end): QWidget () 7 {8 this-> callBack = callBack; 9 this-> start = start; 10 this-> end = end; 11 this-> setMinimumSize (width, height); 12 this-> setMaximumSize (width, height); 13 this-> position = end; // The position of the vertical line, brightness Value 14 this-> mousedown = False; // to achieve the drag effect, first determine whether the mouse is clicked, and then move the mouse to make it valid 15} 16 17 void TrackBar: mouseReleaseEvent (QMouseEvent * event) {18 this-> mousedown = false; // After the mouse is put down, moving the mouse will no longer have the drag effect 19 int pos = event-> x (); 20 if (pos <5) // reduce the value by 5 because it is displayed from the position of 5 on the left for good looking, and is regarded as the scale 021 position = start; 22 else if (pos> (widthOfLine + 5 )) 23 position = end; 24 else {25 double ratio = 1.0 * (pos-5)/widthOfLine; 26 position = (end-start) * ratio + start; 27} 28 update (); // Trigger the re-painting operation to generate the paintEvent event 29 30 callBack-> callBack (position); // callBack function 31} 32 33 34 void TrackBar: paintEvent (QPaintEvent *) {35 QPainter p (this); 36 p. setPen (QPen (Qt: lightGray, 5); // The font used for writing. Use this font to obtain the pixel size of the word 37 int widthOfTitle = 5 + 1.2 * p. fontMetrics (). width (QString: number (-end); // The size required by the rightmost display number. Add a negative number to prevent negative numbers (-100 ~ (100) 38 int w = width ()-widthOfTitle; 39 widthOfLine = w-5; 40 p. setPen (Qt: lightGray); 41 p. drawLine (QPoint (5, height ()/2), QPoint (w, height ()/2); // The middle crossline is 42 p. setPen (QPen (Qt: lightGray, 5); 43 double ratio = 1.0 * (position-start)/(end-start); 44 if (ratio <0) {45 ratio =-ratio; 46} 47 int posX = (w-5) * ratio + 5; 48 p. drawLine (QPoint (posX, 10), QPoint (posX, height ()-10); // specifies the position of the brightness value, which is 49 int heightOfTitle = p. fontMetrics (). height (); 50 p. drawText (w + 5, height ()/2 + heightOfTitle/2, QString: number (position )); // The y coordinate of the written text is the coordinate 51 at the bottom of the text} 52 53 void TrackBar: mousePressEvent (QMouseEvent *) {54 this-> mousedown = true; // press the mouse, then move the mouse to produce a drag effect 55} 56 57 void TrackBar: mouseMoveEvent (QMouseEvent * event) {// move the mouse event, if it is dragging, you need to determine whether the mouse has pressed 58 if (mousedown = false) 59 return; 60 // The following is the same as the mouse release operation. 61 int pos = event-> x (); 62 if (pos <5) // reduce the value by 5 because the value is displayed from the position of 5 on the left for good looking. The value is displayed as the scale 063 position = start; 64 else if (pos> (widthOfLine + 5 )) 65 position = end; 66 else {67 double ratio = 1.0 * (pos-5)/widthOfLine; 68 position = (end-start) * ratio + start; 69} 70 update (); // trigger the re-painting operation to generate the paintEvent event 71} 72 73 void TrackBar: setPosition (int position) {74 this-> position = position; 75}

 

 

Use slide bar & Test

 1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3  4 #include <QMainWindow> 5 #include "trackbarcallback.h" 6 #include "trackbar.h" 7  8 namespace Ui { 9 class MainWindow;10 }11 12 class MainWindow : public QMainWindow,public TrackBarCallBack13 {14     Q_OBJECT15 16 public:17     explicit MainWindow(QWidget *parent = 0);18      void callBack(int value);19     ~MainWindow();20 21 private:22     Ui::MainWindow *ui;23     TrackBar *trackBar;24 };25 26 #endif // MAINWINDOW_H

 

# Include "mainwindow. h "# include" ui_main1_1_h "# include <qdebug. h> MainWindow: MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui: MainWindow) {ui-> setupUi (this ); trackBar = new TrackBar (this, 100,100,-); trackBar-> setWindowTitle (QString ("set positive and negative values"); trackBar-> show ();} MainWindow ::~ MainWindow () {delete ui;} void MainWindow: callBack (int value) {qDebug () <"value:" <value ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.