PDA

View Full Version : About QScroll Area



franco.amato
23rd January 2010, 00:33
Hi to all, I attached a widget to a QScrollArea. Such widget has a moving timeline. I would keep the timeline visible when moving,
This mean that when the timeline come to the end of the widget the whole scrollarea should be shifted to the left. I don't know how to do it.

I hope to get help.

Best Regards,
Franco

wysota
23rd January 2010, 15:57
Set value of the horizontal scroll bar of the scroll area to 0.

franco.amato
23rd January 2010, 16:49
Set value of the horizontal scroll bar of the scroll area to 0.


Hi it doesn't work...when the time line comes at the end of the viewport it disappear and nothing happens

wysota
23rd January 2010, 18:50
Please provide a minimal compilable example reproducing the problem.

franco.amato
23rd January 2010, 21:00
Please provide a minimal compilable example reproducing the problem.


WaveWidget::WaveWidget( QWidget* parent /* = 0 */ )
: QWidget( parent ),
m_wave(0),
m_ZoomFactor( 1.0 )
{
/* scroll area */
QScrollArea* sa = new QScrollArea;

sa->setWidgetResizable(true);
QScrollBar *scrollBar = sa->horizontalScrollBar();
scrollBar->setSliderDown( true );
scrollBar->setValue(0);
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn );

m_WaveDisplay = new WaveDisplay(sa->viewport());
m_WaveDisplay->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
sa->setWidget( m_WaveDisplay );


QHBoxLayout* hl = new QHBoxLayout();
hl->addWidget(m_panel);
hl->setSpacing( 0 );
hl->addWidget(sa);

QVBoxLayout* layout = new QVBoxLayout();
layout->addLayout(hl);
layout->addLayout(lo);
setLayout(layout);
setFocusPolicy( Qt::TabFocus );
}

m_WaveDisplay is a waveform display and when I click "play" a time line moves over it. When the timeline reach the end of the visible part it disappear on the right. I would shift the viewport() to keep visible ihe timeline.

I hope it's clear

wysota
23rd January 2010, 21:40
That's neither compilable nor reproducing the problem...

Anyway, this works for me (and this is what I mean by a minimal compilable example):

#include <QtGui>

class ScrollArea : public QScrollArea {
public:
ScrollArea() : QScrollArea(){}
protected:
void timerEvent(QTimerEvent *te){
int v = horizontalScrollBar()->value();
int m = horizontalScrollBar()->maximum();
if(v==m)
horizontalScrollBar()->setValue(0);
else
horizontalScrollBar()->setValue(v+1);
}
};

int main(int argc, char **argv){
QApplication app(argc, argv);
QLabel *label = new QLabel;
QPixmap px(2048, 60);
QLinearGradient grad(0,0,2048,0);
grad.setColorAt(0, Qt::red);
grad.setColorAt(1, Qt::blue);
QPainter p(&px);
p.fillRect(px.rect(), QBrush(grad));
p.end();
label->setPixmap(px);
ScrollArea area;
area.setWidget(label);
area.show();
area.startTimer(10);
return app.exec();
}

franco.amato
25th January 2010, 20:27
That's neither compilable nor reproducing the problem...

Anyway, this works for me (and this is what I mean by a minimal compilable example):

#include <QtGui>

class ScrollArea : public QScrollArea {
public:
ScrollArea() : QScrollArea(){}
protected:
void timerEvent(QTimerEvent *te){
int v = horizontalScrollBar()->value();
int m = horizontalScrollBar()->maximum();
if(v==m)
horizontalScrollBar()->setValue(0);
else
horizontalScrollBar()->setValue(v+1);
}
};

int main(int argc, char **argv){
QApplication app(argc, argv);
QLabel *label = new QLabel;
QPixmap px(2048, 60);
QLinearGradient grad(0,0,2048,0);
grad.setColorAt(0, Qt::red);
grad.setColorAt(1, Qt::blue);
QPainter p(&px);
p.fillRect(px.rect(), QBrush(grad));
p.end();
label->setPixmap(px);
ScrollArea area;
area.setWidget(label);
area.show();
area.startTimer(10);
return app.exec();
}

Dear Wysota I solved my problem using ensureVisible routine of the QScrollArea http://doc.trolltech.com/4.6/qscrollarea.html#ensureVisible givind periodically the position of the timeline and it works perfectly.
This routine is fantastic!!

Best Regards

wysota
25th January 2010, 22:26
That's the second time you "solved" your problem the wrong way. Keep doing that and at some point everything will fall apart.

franco.amato
25th January 2010, 23:21
That's the second time you "solved" your problem the wrong way. Keep doing that and at some point everything will fall apart.

Why wrong way? Seems work well. I don't have to set to zero the scroll bar, I would keep visible the timeline

wysota
26th January 2010, 00:42
Why wrong way? Seems work well.
Many things seem well but you shouldn't use them as at some point the combination of things that seem to work fell will blow up into your face.


I don't have to set to zero the scroll bar,
Honestly, you don't have to do anything...


I would keep visible the timeline
Did you run my example? Did you see the gradient vanishing after reaching the end? Maybe things are already exploding in your face...

franco.amato
26th January 2010, 03:35
Sorry but nothing is exploding in my face and yes I run your example.
I think you should speak in a different way as administrator without ridicole the work of other developers.
Maybe I'm not a Qt expert, my field is another and if I ask for help here is to learn and improve my Qt.

Best Regards

wysota
26th January 2010, 09:53
Sorry but nothing is exploding in my face
If things that are working for me are not working for you then the only thing to blame is the code already in your application.

and yes I run your example.
Did it work? Did you adapt it to your application? Did it work there?


I think you should speak in a different way as administrator without ridicole the work of other developers.
This forum is here mainly to help others and not to praise their work, be it good or bad.


Maybe I'm not a Qt expert, my field is another and if I ask for help here is to learn and improve my Qt.
So learn and improve instead of going forward the wrong path even if someone is constrantly trying to pull you away from it.

You can abuse some mechanisms in Qt and they will work. But at some point you reach a situation when the side effects of all the abuses get stacked and accumulate effectively preventing you from finishing your task successfully. The rules of Software Engineering say that the latter you spot the problem, withdraw from it and do things the proper way, the more expensive it is.

franco.amato
26th January 2010, 15:09
If things that are working for me are not working for you then the only thing to blame is the code already in your application.
I didn't say that it doesn't work, I said that I don't need it also if your example is very good. Maybe my english sometime is not perfect.


Did it work? Did you adapt it to your application? Did it work there?
Yes it works. In the attached widget I already have a timer that move a timeline so the routine used by me seems perfect for my purpose.
Johan Thelin suggested it to me.



This forum is here mainly to help others and not to praise their work, be it good or bad.


So learn and improve instead of going forward the wrong path even if someone is constrantly trying to pull you away from it.

You can abuse some mechanisms in Qt and they will work. But at some point you reach a situation when the side effects of all the abuses get stacked and accumulate effectively preventing you from finishing your task successfully. The rules of Software Engineering say that the latter you spot the problem, withdraw from it and do things the proper way, the more expensive it is.

If this routine is so bad, why it exists? Seems done for my purpose

wysota
26th January 2010, 18:00
Yes it works. In the attached widget I already have a timer that move a timeline so the routine used by me seems perfect for my purpose.
I don't see how using ensureVisible() is more "perfect" than setting the value of the scroll bar to 0 but have it your way...


If this routine is so bad, why it exists? Seems done for my purpose

"If division by 0 is a bad thing then why does division exist at all?"

In other words, I didn't say the routine is bad, I said it's bad in your case (actually it's not that bad in this situation but once you start adding more functionality it might become bad).

franco.amato
26th January 2010, 18:33
I don't see how using ensureVisible() is more "perfect" than setting the value of the scroll bar to 0 but have it your way....

Why should I set the value to zero? I have to keep visible the timeline ( in my case a moving line from left to right ) that disappear in the right size of the widget when I resize it ( while playing a sound )




"If division by 0 is a bad thing then why does division exist at all?"

In other words, I didn't say the routine is bad, I said it's bad in your case (actually it's not that bad in this situation but once you start adding more functionality it might become bad)

I think you didn't understand my scenario, sure for my bad english sorry.

Best Regards

wysota
26th January 2010, 22:04
Why should I set the value to zero? I have to keep visible the timeline ( in my case a moving line from left to right ) that disappear in the right size of the widget when I resize it ( while playing a sound )

Well, you said "shifted to the left". I understood you want to go back to the beginning when the timeline ends.

franco.amato
26th January 2010, 23:59
Well, you said "shifted to the left". I understood you want to go back to the beginning when the timeline ends.

Sorry my bad english plays against me