qt qslider not smooth

99封情书 提交于 2019-12-06 08:20:36

问题


I have a qslider to control the zooming of a map like this: connect(ui->zoomSlider, SIGNAL(valueChanged(int)), ui->map, SLOT(SetZoom(int))); However, because this online-map response relatively slow.

I found that the qslider's response also becomes very slow which means when you slide the slider, it's position won't change until suddenly it jump to the position where you release your mouse.

How could I solve this?


回答1:


One possible solution to delay processing of your signal is to connect it with slot by using Qt::QueuedConnection.

connect(ui->zoomSlider, SIGNAL(valueChanged(int)), ui->map, SLOT(SetZoom(int)), Qt::QueuedConnection);

With Qt::QueuedConnection emitted valueChanged signal event will be not processed at the time of generation, as it happens with directly connected signals. Event will be added to the event loop queue. This is how Qt::QueuedConnection is implemented inside Qt.

Specially for Nejat to test this approach it's possible to use following code:

MainWindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void signalReceived();
    void signalReceivedQueued();

    void buttonPressed();

signals:

    void directConnectedSignal();
    void queuedConnectedSignal();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

MainWindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(this, SIGNAL(directConnectedSignal()), SLOT(signalReceived()), Qt::DirectConnection);
    connect(this, SIGNAL(queuedConnectedSignal()), SLOT(signalReceivedQueued()), Qt::QueuedConnection);

    connect(ui->pushButton, SIGNAL(pressed()), SLOT(buttonPressed()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::signalReceived()
{
    qDebug() << "signalReceived";
}

void MainWindow::signalReceivedQueued()
{
    qDebug() << "signalReceivedQueued";
}

void MainWindow::buttonPressed()
{
    emit queuedConnectedSignal();
    emit directConnectedSignal();
}

If you run code snippet above you will get following output on button press:

signalReceived 
signalReceivedQueued 

Queued signal is emitted first, but received last. And this can be used in your case to prioritize processing of emitted signals.

However most of all using of queued connection will not help you, because user emits slider event too frequently and UI will freeze in any case. So, I can suggest following:

  1. Determine why exactly UI is freezes, what part of code freezing it.
  2. Try to avoid freezing by asynchronous calls or by moving logic into separate thread, or by using QtConcurrent
  3. If you really can't control the way how map is scaled in your webpage, try to ignore all events generated by the QSlider and react only on last generated in 500 ms interval, for example.


来源:https://stackoverflow.com/questions/26371571/qt-qslider-not-smooth

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!