[SOLVED]setting scroll bar

  • Hi to all,

    I want to use a vertical scroll bar in a function. I want to set its range using two global variables [global1,global2].And I want to use its value, as a global variable (global3) each time is changed by an event. Could somebody help me to set this vertical scroll bar?

    Thanks in advance

  • Moderators

    have a look at :
    "examples > widgets > sliders ":http://doc.qt.nokia.com/4.7-snapshot/widgets-sliders.html

    I think this is what you want.

  • Do you know which method is responsible for getting the value of a vertical scroll bar?

  • Moderators

    You can connect the signal
    void QAbstractSlider::valueChanged ( int value )
    to a slot where you use value

    In the examples they use the following to connect in 2 directions
    @ connect(slider, SIGNAL(valueChanged(int)), scrollBar, SLOT(setValue(int)));
    connect(scrollBar, SIGNAL(valueChanged(int)), dial, SLOT(setValue(int)));@

  • What is the reason that you want to use global variables? In general, that is not a good idea at all.

  • As I can see in my program I may not need to use global variables.Well, I have created a button , when you press it you get and display in a QGraphicsView a 2D slice from a 3D volume. But I have set my own a fixed value from this slice (z=89).But my 3D volume has z with range [1,139].I want to display 2D slices of different z every time the value of the scroll bar is changed. Since so far I have managed to display my initial slice, but to display an other slice of z, I must press again the button and reload my 3D image. How could I solve this problem? Here I put some code:

    @QObject::connect(ui->verticalScrollBar_z, SIGNAL(valueChanged()), this, SLOT(push_button_File()));@


    @void MainWindow::push_button_File()

    QString fileName = QFileDialog::getOpenFileName(this,
    tr("Open File"), QDir::currentPath());

    ........... code of ITK to display 2D slices from 3D volume.......
    ////////this 2 lines are rensponsible for setting the slice number
    unsigned int sliceNumber = ui->verticalScrollBar_z->value();
    start[2] = sliceNumber;

    static QGraphicsPixmapItem* pixmapItem;

    if (!pixmapItem) {
    // pixmapItem = scene->addPixmap(QPixmap("2D.png"));

    QPixmap tmpmap (QPixmap("2D.png"));
        pixmapItem = scene->addPixmap ( tmpmap.scaled (214,256) );
        } else {

    QPixmap tmpmap (QPixmap("2D.png"));
    pixmapItem->setPixmap(tmpmap.scaled ( 214,256));



  • In my mainwindow I have written the following, but when I put a break-point, when I change the value of scroll bar, I cannot get inside the function push_button_File. Had I to set the valueChanged() function somewhere? What I am doing wrong?
    I hope somebody could help me.

    @QObject::connect(ui->verticalScrollBar_z, SIGNAL(valueChanged()), this, SLOT(push_button_File()));@

  • Lets suppose that we have a slicer consists of 137 slices (z=[1 137]). To choose which slice I must display in my QGraphicsView, I call one method which loads a 3D volume and contains a parameter z. I also use, a vertical Scroll bar of range [1 137]. My question, is how could I change with the mouse the value of the scroll bar and insert this value into my method, in order to display all the slices from 1 to 137, just with the press of the scroll bar.

    I hope somebody could help me this time.

  • Well, to understand what you are saying. If I write this:
    @ public slots:
    void push_button_File(int x);@

    @connect(ui->verticalScrollBar_z, SIGNAL(valueChanged(int x)), this, SLOT(push_button_File(int x)));@

    @void MainWindow::push_button_File(int x)
    unsigned int sliceNumber = x;

    Then, every time I change with my mouse the value of scrollbar, the function push_button_File runs , and the slicenumber gets the value x?

  • Yes. You could have found that out by either:

    • reading the documentation on how Signals & Slots works, or
    • just try the code.

  • Well, I am using ITK with Qt and still I have the same problem. Here is my code and I hope that someone could tell me what I am doing wrong. When I deckare the function_ void MainWindow::push_button_File(), like this, at least I get some results.

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "canvas.h"

    #include "itkImage.h"
    #include "itkImageFileReader.h"
    #include "itkImageFileWriter.h"
    #include "itkExtractImageFilter.h"
    #include <itkSize.h>

    QString fileName;

    unsigned long size_x;
    unsigned long size_y;
    unsigned long size_z;

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    ui->graphicsView_inputImage->setScene(scene=new Canvas());

    QObject::connect(ui->pushButton_openFile, SIGNAL(clicked()),
    this, SLOT(push_button_File(int x)));

    connect(ui->verticalScrollBar_z, SIGNAL(valueChanged(int x)),
    this, SLOT(push_button_File(int x)));


    void MainWindow::push_button_File(int x)

    QString fileName = QFileDialog::getOpenFileName(this,
    tr("Open File"), QDir::currentPath());

    ///unsigned char to display in .png format
    typedef unsigned char InputPixelType;
    typedef unsigned char OutputPixelType;

    typedef itk::Image< InputPixelType, 3 > InputImageType;
    typedef itk::Image< OutputPixelType, 2 > OutputImageType;

    typedef itk::ImageFileReader< InputImageType > ReaderType;
    typedef itk::ImageFileWriter< OutputImageType > WriterType;

    ReaderType::Pointer reader = ReaderType::New();
    WriterType::Pointer writer = WriterType::New();

    // reader->SetFileName("C:/Users/manolis/Desktop/data/test.png");
    reader->SetFileName( fileName.toStdString() );
    writer->SetFileName( "2D.png" );

    typedef itk::ExtractImageFilter< InputImageType, OutputImageType > FilterType;
    FilterType::Pointer filter = FilterType::New();


    InputImageType::RegionType inputRegion =

    InputImageType::SizeType size = inputRegion.GetSize();

    // get the size of the hole 3D image
    unsigned long size_x = size[0];
    unsigned long size_y = size[1];
    unsigned long size_z = size[2];

    // get slices of z coordiante
    size[2] = 0;

    InputImageType::IndexType start = inputRegion.GetIndex();
    unsigned int sliceNumber = x; //ui->verticalScrollBar_z->value();
    start[2] = sliceNumber;

    InputImageType::RegionType desiredRegion;
    desiredRegion.SetSize( size );
    desiredRegion.SetIndex( start );

    filter->SetExtractionRegion( desiredRegion );

    filter->SetInput( reader->GetOutput() );
    writer->SetInput( filter->GetOutput() );

    catch( itk::ExceptionObject & err )
    std::cerr << "ExceptionObject caught !" << std::endl;
    std::cerr << err << std::endl;


    // taking the size of the loaded image

    ui->label_4->setText(QString("size x:%1").arg(size_x));
    ui->label_5->setText(QString("size y:%1").arg(size_y));
    ui->label_6->setText(QString("size z:%1").arg(size_z));



    void MainWindow::mousejustpressed(int x,int y)
    int k1=256-y;

    unsigned char value;

    QImage image(scene->sceneRect().size().toSize(),

    QPainter painter(&image);



    ui->label_3->setText(QString("pixel value:%1").arg(value));


    delete ui;

  • A little late.. but the problem jk_mk is in your connect statement :D

    connect(ui->verticalScrollBar_z, SIGNAL(valueChanged(int x)),
    this, SLOT(push_button_File(int x)));

    what you're doing wrong is writing the variable names in the connect. You need to only specify the type. So that should instead be:

    connect(ui->verticalScrollBar_z, SIGNAL(valueChanged(int)),
    this, SLOT(push_button_File(int)));

    Just "int" not "int x" in the connect statement.. that should solve your problem.

  • Yes, this has solved my problem. Thanks a lot. But I have noticed something strange in my program when I use this event of scrollbar.To be more specific I use this event to display slices of 3D volume in a QGraphicsScene. When I change the value of the QScrollbar slowly then I do not have any problem concerning the display of my image. But when I change the value of QScrollbar Quickly, then my image is diaplayed with a little delay.
    What do you think that is responsible for this problem? Might it be the code included in the function? Might it be too long?Or the reason for this problem is that every time firstly I save the slice as png file in memory and after that I display the image in the QGraphicsView?


  • Hmm... if you are accessing the filesystem in the slot, then it would be slow. Accessing the hard disk is many orders slower than accessing your RAM. Load your 3D Model/2D images in memory (i.e RAM) and then use it/them in the slot.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.