PDA

View Full Version : setValue of ScrollBar Don't work at first time



sergio87
7th June 2011, 12:56
Hi!

I open an image in a QLabel as a QPixmap. When I opened the image I resize it. Then I "center" the image respect to a particular pixel. "Center" the image is to set value to the ScrollBar. The code is:



imageLabelCandidate = new QLabel();
imageLabelCandidate->setBackgroundRole(QPalette::Base);
imageLabelCandidate->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabelCandidate->setScaledContents(true);

scrollAreaCandidate = new QScrollArea;
scrollAreaCandidate->setBackgroundRole(QPalette::Dark);
scrollAreaCandidate->setWidget(imageLabelCandidate);

void MyClass::openImage(string *path){
QString fileName = QString::fromStdString(*path);
if (!fileName.isEmpty()) {
QImage image(fileName);
if (image.isNull()) {
cout << "ERROR opening file" << endl;
return;
}
imageLabelCandidate->setPixmap(QPixmap::fromImage(image));
imageLabelCandidate->MPixmap(QPixmap::fromImage(image));
imageLabelCandidate->adjustSize();
scaleCandidateImage(1.3);
scrollAreaCandidate->horizontalScrollBar()->setValue(2000);
}

void MyClass::scaleCandidateImage(double factor)
{
Q_ASSERT(imageLabelCandidate->pixmap());
imageLabelCandidate->resize(factor * imageLabelCandidate->pixmap()->size());
}


The problem is that the first time I call "openImage" it don't set the value 2000 to the horizontalScrollBar. The following times it works properly.

Where I make the mistake? :confused:

Thanks in advance!!!!! ;)

Santosh Reddy
8th June 2011, 01:16
It is not very clear in what sequence you are doing the things

scrollAreaCandidate->horizontalScrollBar()->setValue(2000);
this should done some were after you load the pixmap into QLabel, and set the QLabel in the scroll area, and then set the scroll bar position

sergio87
8th June 2011, 08:54
That's just what I do, no?

I'll explain: The first eight lines of my code are in the constructor of the class. So, it only be executed once. I call al lot of times to "openImage". The first time I do it not work. And not work only the setValue of the scrollBar, because the resize (MyClass::scaleCandidateImage) works allways.

The following times it works properly.

¿Any ideas?

Santosh Reddy
8th June 2011, 09:02
Ok, try adding this

scrollAreaCandidate->horizontalScrollBar()->setValue(2000);
scrollAreaCandidate->update(); //Add this

sergio87
8th June 2011, 09:13
Thanks!!
I had already tested, and didn't works.

I tried:

scrollAreaCandidate->horizontalScrollBar()->update();
scrollAreaCandidate->update();
scrollAreaCandidate->horizontalScrollBar()->repaint();

But nothing works!!! :(

Santosh Reddy
8th June 2011, 09:26
were in you code you call openImage(), I mean which objects method / slot?
If openImage() is a slot, to which signal is it connected?

sergio87
8th June 2011, 09:43
Thanks for reply!

The connexions are very sophisticated. MyClass emits a signal when is clicked a button. I connect this signal with another slot of other class (MainWindow). MainWindow do some operations and generates a string (path). I emit a signal with this string, and the receiver is MyClass::openImage.

At fisrt time, mainWindow emits the signal with a default path. Perhaps the error could be there. But I can't understand why the first time it scale the image properly and not set the value of the scrollBar! :confused:

MarekR22
8th June 2011, 10:05
try to use: QScrollArea::alignment-prop I think this is what you really want.

sergio87
8th June 2011, 11:00
try to use: QScrollArea::alignment-prop I think this is what you really want.

I read that this is to centre (for example) the content. http://lists.trolltech.com/qt4-preview-feedback/2005-02/thread00063-0.html

Maybe my poor english is confusing. My apologies. Anyway, I tried and it didn't work.

I simplified the code to show you. The "2000", actually, is a variable that contains a different value called pointX. Point is calculated every time I load an image, before the setValue. It contain the correct number in each image, but it didn't set the value to the crolBar...

Thank you very much!! :D