PDA

View Full Version : QScrollArea



merry
19th June 2007, 08:41
:):)Hi all

I m working on Intel mac machine using Qt4.2 on it.

I m having a QWidget on which there is a matrix of (16x16) , On click event of the button the rows of the matrix is filled with the color one by one. i.e. on first click First row of the matrix is filled with second click, the second row is filled and so on..

But when I scroll the matrix up and down or change the size of the matrix then also the matrix filled with the color. But I dont want this ,i.e on scrolling or on resizing the matrix ,It shouldnot filled with the ciolor ,it remains the same as it is.

If anybody knows then Pls suggest the way .:)

Thanx

wysota
19th June 2007, 08:53
Can we see the code?

merry
19th June 2007, 09:43
Yaa the code is same as the code i sent u yesterday. But there is little alteration in characterwidget.cpp
characterwidget.cpp


#include <QtGui>
#include "characterwidget.h"
#include "mainwindow.h"

bool flag=false;

static int irow =16;
static int icol = 0 ;

CharacterWidget::CharacterWidget(QWidget *parent)
: QWidget(parent)
{
squareSize = 16;
columns = 16;
setMouseTracking(true);
}

QSize CharacterWidget::sizeHint() const
{
return QSize(columns*squareSize, columns*squareSize);
}

void CharacterWidget::mouseMoveEvent(QMouseEvent *event)
{
QPoint widgetPosition = mapFromGlobal(event->globalPos());
uint key = (widgetPosition.y()/squareSize)*columns + widgetPosition.x()/squareSize;
QString text = QString::number(key,10);
QToolTip::showText(event->globalPos(), text, this);
}

void CharacterWidget::FillColor()
{
flag=true;
update();
}

void CharacterWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPainterPath myPath;
painter.fillRect(event->rect(), QBrush(Qt::white));
QRect redrawRect = event->rect();
int beginRow = redrawRect.top()/squareSize;
int endRow = redrawRect.bottom()/squareSize;
int beginColumn = redrawRect.left()/squareSize;
int endColumn = redrawRect.right()/squareSize;
painter.setPen(QPen(Qt::gray));

for (int row = beginRow; row<=endRow;++row)
{
for (int column = beginColumn; column <= endColumn; ++column)
{
painter.drawRect(column*squareSize, row*squareSize, squareSize, squareSize);
}
}

painter.setPen(QPen(Qt::black));
if(flag == true)
{
for (int row =beginRow; row<=icol;++row)
{
for (int column = beginColumn; column<=irow;++column)
{
painter.setClipRect(column*squareSize, row*squareSize, squareSize, squareSize);
painter.fillRect(column*squareSize+1, row*squareSize+1, squareSize, squareSize, QBrush(Qt::blue));
}
}
icol++;
}

}


if u understand my code and my problem then pls help
Thanx

wysota
19th June 2007, 11:00
Why did you start a new thread on the same problem then?

I'd start by simplifying the code to the maximum, starting by redrawing the whole widget instead of the specified rectangle. You may have some calculation errors. This would help notice them.

merry
19th June 2007, 11:28
Ok Thanx ,

but is there any other simple way to fill the grid with the color without the use of paint event.

I tell u my problem that actually what i wanted to do , I have to create grid of rows and columns same grid as in "Charactermap Example" that is in Qt Assistant ->Widget Example
But the functioning I have to do is different from that ,After creating the grid on the widget I also have to upload a button on that widget ,when i click on the button the blocks or say rectangles in the grid starts filling color one by one with a gap of say 1000 nanoseconds that is firstblocks fills than after 1000ns second block fills and so on.........

if u have any other soln to this prob. then pls pls help.....

Thanx

wysota
19th June 2007, 12:08
but is there any other simple way to fill the grid with the color without the use of paint event.
I didn't say you shouldn't use paint event. Of course you can use QTableWidget if it fits your needs...


I tell u my problem that actually what i wanted to do , I have to create grid of rows and columns same grid as in "Charactermap Example" that is in Qt Assistant ->Widget Example
AFAIK it uses QTableView.


But the functioning I have to do is different from that ,After creating the grid on the widget I also have to upload a button on that widget ,when i click on the button the blocks or say rectangles in the grid starts filling color one by one with a gap of say 1000 nanoseconds that is firstblocks fills than after 1000ns second block fills and so on.........
In that case using QTableView would be an overkill as you don't need any interaction with the grid.

You just have to correct your code... I guess the most simple solution would be something like this:


#include <QWidget>
#include <QPaintEvent>
#include <QTimerEvent>
#include <QPainter>
#include <QApplication>

class GridProgressBar : public QWidget {
Q_OBJECT
public:
GridProgressBar(QWidget *parent=0) : QWidget(parent){
m_max = 100;
m_value = 0;
m_tim = -1;
}
public slots:
void setMax(int m){
m_max = m;
updateGeometry();
}
void start(){
m_tim = startTimer(10);
}
void stop(){
killTimer(m_tim);
m_tim = -1;
}
QSize sizeHint() const { return QSize(m_max*3, m_max*3); }
protected:
void timerEvent(QTimerEvent *ev){
if(ev->timerId()!=m_tim) return QWidget::timerEvent(ev);
m_value++;
update();
if(m_value>=m_max){
stop();
}
}
void paintEvent(QPaintEvent *ev){
QPainter p(this);
int coox = 0;
int cooy = 0;
p.setBrush(Qt::red);
for(int curr = 0; curr <= m_max; curr++){
if(m_value+1==curr)
p.setBrush(Qt::white);
p.drawRect(coox, cooy, 12, 12);
coox += 12;
if(coox+12>width()){
coox = 0;
cooy += 12;
}
}
}
int m_tim, m_value, m_max;
};

#include "main.moc"

int main(int argc, char **argv){
QApplication app(argc, argv);
GridProgressBar pbar;
pbar.show();
pbar.setMax(1000);
pbar.start();
return app.exec();
}

BTW. Forget about nanoseconds. If you fill one block each 1000ns then you can fill 1000000 blocks within a second. Based on the fact that your screen updates probably about 100 times per second, you wouldn't see anything - at each refresh 10000 cells would be filled. And of course you'd have an enourmous lag anyway...

merry
19th June 2007, 12:16
Thanx Wysota I 'll try this code.

merry
19th June 2007, 13:47
Thanx Wysota I had implemented the code and it works

Thanx alot.

But is there any way to fix the size of the widget or can we use QScrollArea in it.

merry
19th June 2007, 14:55
Hi Wysota

Please tell me how to fix the size of the widget and after fixing the size then for moving up and down how can we use scrollbar init.


Thanx

wysota
19th June 2007, 15:21
Put the widget into a scroll area and make sure it has a "hard changable" width or height.

merry
20th June 2007, 05:50
Pls tell me how can I fixed the size of the widget that is shown to me.


Put the widget into a scroll area and make sure it has a "hard changable" width or height.
I dont understand this, also tell me how can i use it in the above code that is given by you wysota.

Thanx

merry
20th June 2007, 08:20
Hi Wysota

Can u pls tell me that How can I fix the no. of rows and no. of columns and the size of the rectangle that is drawn in the above code that is given by you and also how can i insert the scrollArea here in this rectangle.


Thanx

merry
20th June 2007, 13:05
My problem Solved

Thanx