Results 1 to 13 of 13

Thread: QScrollArea

  1. #1
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Question QScrollArea

    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
    Always Believe in Urself
    Merry

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollArea

    Can we see the code?

  3. #3
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QScrollArea

    Yaa the code is same as the code i sent u yesterday. But there is little alteration in characterwidget.cpp
    characterwidget.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "characterwidget.h"
    3. #include "mainwindow.h"
    4.  
    5. bool flag=false;
    6.  
    7. static int irow =16;
    8. static int icol = 0 ;
    9.  
    10. CharacterWidget::CharacterWidget(QWidget *parent)
    11. : QWidget(parent)
    12. {
    13. squareSize = 16;
    14. columns = 16;
    15. setMouseTracking(true);
    16. }
    17.  
    18. QSize CharacterWidget::sizeHint() const
    19. {
    20. return QSize(columns*squareSize, columns*squareSize);
    21. }
    22.  
    23. void CharacterWidget::mouseMoveEvent(QMouseEvent *event)
    24. {
    25. QPoint widgetPosition = mapFromGlobal(event->globalPos());
    26. uint key = (widgetPosition.y()/squareSize)*columns + widgetPosition.x()/squareSize;
    27. QString text = QString::number(key,10);
    28. QToolTip::showText(event->globalPos(), text, this);
    29. }
    30.  
    31. void CharacterWidget::FillColor()
    32. {
    33. flag=true;
    34. update();
    35. }
    36.  
    37. void CharacterWidget::paintEvent(QPaintEvent *event)
    38. {
    39. QPainter painter(this);
    40. QPainterPath myPath;
    41. painter.fillRect(event->rect(), QBrush(Qt::white));
    42. QRect redrawRect = event->rect();
    43. int beginRow = redrawRect.top()/squareSize;
    44. int endRow = redrawRect.bottom()/squareSize;
    45. int beginColumn = redrawRect.left()/squareSize;
    46. int endColumn = redrawRect.right()/squareSize;
    47. painter.setPen(QPen(Qt::gray));
    48.  
    49. for (int row = beginRow; row<=endRow;++row)
    50. {
    51. for (int column = beginColumn; column <= endColumn; ++column)
    52. {
    53. painter.drawRect(column*squareSize, row*squareSize, squareSize, squareSize);
    54. }
    55. }
    56.  
    57. painter.setPen(QPen(Qt::black));
    58. if(flag == true)
    59. {
    60. for (int row =beginRow; row<=icol;++row)
    61. {
    62. for (int column = beginColumn; column<=irow;++column)
    63. {
    64. painter.setClipRect(column*squareSize, row*squareSize, squareSize, squareSize);
    65. painter.fillRect(column*squareSize+1, row*squareSize+1, squareSize, squareSize, QBrush(Qt::blue));
    66. }
    67. }
    68. icol++;
    69. }
    70.  
    71. }
    To copy to clipboard, switch view to plain text mode 


    if u understand my code and my problem then pls help
    Thanx
    Always Believe in Urself
    Merry

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollArea

    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.

  5. #5
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QScrollArea

    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
    Always Believe in Urself
    Merry

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollArea

    Quote Originally Posted by merry View Post
    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:

    Qt Code:
    1. #include <QWidget>
    2. #include <QPaintEvent>
    3. #include <QTimerEvent>
    4. #include <QPainter>
    5. #include <QApplication>
    6.  
    7. class GridProgressBar : public QWidget {
    8. Q_OBJECT
    9. public:
    10. GridProgressBar(QWidget *parent=0) : QWidget(parent){
    11. m_max = 100;
    12. m_value = 0;
    13. m_tim = -1;
    14. }
    15. public slots:
    16. void setMax(int m){
    17. m_max = m;
    18. updateGeometry();
    19. }
    20. void start(){
    21. m_tim = startTimer(10);
    22. }
    23. void stop(){
    24. killTimer(m_tim);
    25. m_tim = -1;
    26. }
    27. QSize sizeHint() const { return QSize(m_max*3, m_max*3); }
    28. protected:
    29. void timerEvent(QTimerEvent *ev){
    30. if(ev->timerId()!=m_tim) return QWidget::timerEvent(ev);
    31. m_value++;
    32. update();
    33. if(m_value>=m_max){
    34. stop();
    35. }
    36. }
    37. void paintEvent(QPaintEvent *ev){
    38. QPainter p(this);
    39. int coox = 0;
    40. int cooy = 0;
    41. p.setBrush(Qt::red);
    42. for(int curr = 0; curr <= m_max; curr++){
    43. if(m_value+1==curr)
    44. p.setBrush(Qt::white);
    45. p.drawRect(coox, cooy, 12, 12);
    46. coox += 12;
    47. if(coox+12>width()){
    48. coox = 0;
    49. cooy += 12;
    50. }
    51. }
    52. }
    53. int m_tim, m_value, m_max;
    54. };
    55.  
    56. #include "main.moc"
    57.  
    58. int main(int argc, char **argv){
    59. QApplication app(argc, argv);
    60. GridProgressBar pbar;
    61. pbar.show();
    62. pbar.setMax(1000);
    63. pbar.start();
    64. return app.exec();
    65. }
    To copy to clipboard, switch view to plain text mode 

    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...

  7. The following user says thank you to wysota for this useful post:

    merry (19th June 2007)

  8. #7
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QScrollArea

    Thanx Wysota I 'll try this code.
    Always Believe in Urself
    Merry

  9. #8
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QScrollArea

    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.
    Always Believe in Urself
    Merry

  10. #9
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QScrollArea

    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
    Always Believe in Urself
    Merry

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScrollArea

    Put the widget into a scroll area and make sure it has a "hard changable" width or height.

  12. #11
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QScrollArea

    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
    Always Believe in Urself
    Merry

  13. #12
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QScrollArea

    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
    Always Believe in Urself
    Merry

  14. #13
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QScrollArea

    My problem Solved

    Thanx
    Always Believe in Urself
    Merry

Similar Threads

  1. QScrollArea and resizing
    By rarefluid in forum Qt Programming
    Replies: 8
    Last Post: 22nd July 2007, 15:18
  2. Replies: 2
    Last Post: 8th October 2006, 21:14
  3. Replies: 7
    Last Post: 20th September 2006, 15:45
  4. is there any signal for QScrollArea?
    By rajesh in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2006, 08:12
  5. QScrollArea problem positioning a QWidget inside
    By Spectator in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 23:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.