简体   繁体   中英

Scroll Bar Controlling scrolling a Scroll Area in Qt

I am new to QT framework , so please bear with me...

I have been given this simple task , i have a series of Qlabels each one is set to a .png pic

these Multiple Qlabels don't fit the screen , so a scroll bar here would be handy ...

so i inserted all of my items in my Scroll area

take a look at the situation : 在此处输入图片说明

i want the scroll bar to controll the Scroll area to scroll up and down

I Have created the slot slideMoved() that absorbs the signal generated by the scroll bar when the the slide is moved

this is the Form.cpp class :

//Form.cpp
#include "form1.h"
#include "form.h"
#include "ui_form.h"
#include "ui_form1.h"
#include<QScrollArea>
#include<QScrollBar>

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


}

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

void Form::slideMoved()
{

}

My Questions are the following,

-am i doing this right , or their is an other simpler way to do that ?

-how should slideMoved() handle the event by scrolling up and down the scroll area

Please be Specific since i am new to this, I would appreciate that

thank you

If you want to create a slot you must specify that it is a slot.

This is done like this

private slots:
    void slideMoved();

Then you have to implement this slot in the way you did.

void Form::slideMoved(int val)
{
    ...
}

In the end you have to connect a signal to your slot in order to trigger your slot when the signal is being emmited.

connect(sender,SIGNAL(sliderMoved(int)),receiver,SLOT(slideMoved(int)));

Then you can create a new variable to hold the previous slider value. Every time your slot will be executed you will compare the previous value with the new one in order to understand the movement.

private:
    int previousValue;

The slot can be something like this.

void Form::slideMoved(int val)
    {
        if(val<previousValue) qDebug() << "Previous value is smaller";
        else qDebug() << "Previous values is bigger";
        previousValue = val;
    }

You can read more about signals and slots here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM