Qt qslider is not smooth
I have a qslider to control the zoom of the map like this: connect (ui-> zoomSlider, SIGNAL (valueChanged (int)), ui-> map, SLOT (SetZoom (int))); However, since this online map answer is relatively slow.
I found that qslider's response also becomes very slow, which means that when you slide the slider, its position will not change until it moves to the position where you release the mouse.
How can I solve this?
source to share
One possible solution to delay your processing signal
is to connect it to the slot using Qt :: QueuedConnection.
connect(ui->zoomSlider, SIGNAL(valueChanged(int)), ui->map, SLOT(SetZoom(int)), Qt::QueuedConnection);
When an Qt::QueuedConnection
emitted valueChanged
signal event will not be processed during generation, as is the case with directly
linked signals. The event will be added to the event loop queue. This is how it Qt::QueuedConnection
is implemented internally in Qt.
Specially for Nejat
to test this approach, you can use the 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 the above code snippet you get the following output on the button:
signalReceived
signalReceivedQueued
The signal queue is emitted first, but received last. And this can be used in your case to prioritize the processing of emitted signals.
Most of all, however, using a queue in a queue won't help you because the user emits a slider event too much and the UI will freeze anyway. So I can suggest the following:
- Determine why the user interface is freezing, what part of the code is freezing it.
- Try not to freeze asynchronous calls or by moving logic to a separate thread or using
QtConcurrent
- If you really have no control over how the map is scaled on your web page, try ignoring all QSlider-generated events and reacting to the last
500
ms interval generated, for example .
source to share