QSlider 拖动滑动条使用valueChanged(int value)信号重复调用槽函数问题解决


废话不多说,直接上代码,关键点: 一个变量和三个信号
bool m_sliderPressed
void sliderPressed()
 
  
void sliderReleased()
 
  
void valueChanged(int value)

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
  
#include 
 
  
namespace Ui {
class MainWindow;
}
 
  
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
  
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
 
  
    void sliderValueChanged(int value);
private slots:
    void on_horizontalSlider_valueChanged(int value);
 
  
    void on_horizontalSlider_sliderPressed();
 
  
    void on_horizontalSlider_sliderReleased();
 
  
private:
    Ui::MainWindow *ui;
    bool m_sliderPressed;
};
 
  

#endif//MAINWINDOW_H

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_sliderPressed = false;
}
 
  
MainWindow::~MainWindow()
{
    delete ui;
}
 
  
void MainWindow::sliderValueChanged(int value)
{
    qDebug() << "sliderValueChanged:" << value;
    ui->lineEdit->setText(QString::number(value));
}
 
  
void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    if(!m_sliderPressed)
    {
        sliderValueChanged(value);
    }
}
 
  
void MainWindow::on_horizontalSlider_sliderPressed()
{
    m_sliderPressed = true;
}
 
  
void MainWindow::on_horizontalSlider_sliderReleased()
{
    m_sliderPressed = false;
    int value = ui->horizontalSlider->value();
    sliderValueChanged(value);
}
 
  

 
 

你可能感兴趣的:(Qt)