На форме: QScrollArea(layout vertically) и на ней QLabel.
C++ (Qt)
QPoint pt;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->scrollArea->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject *obj, QEvent *e)
{
if (obj == ui->scrollArea) {
QMouseEvent *me;
QScrollBar *sb;
switch (e->type()) {
case QEvent::MouseButtonPress:
me = static_cast<QMouseEvent*>(e);
if (me->button() == Qt::LeftButton) {
pt = me->localPos().toPoint();
ui->scrollArea->setCursor(Qt::ClosedHandCursor);
}
break;
case QEvent::MouseMove:
me = static_cast<QMouseEvent*>(e);
if (me->buttons() & Qt::LeftButton) {
pt -= me->localPos().toPoint();
sb = ui->scrollArea->horizontalScrollBar();
sb->setValue(pt.x() + sb->value());
sb = ui->scrollArea->verticalScrollBar();
sb->setValue(pt.y() + sb->value());
pt = me->localPos().toPoint();
}
break;
case QEvent::MouseButtonRelease:
ui->scrollArea->setCursor(Qt::ArrowCursor);
break;
default:
break;
}
}
return QMainWindow::eventFilter(obj, e);
}