Results 1 to 4 of 4

Thread: How to fill the grid with the color?

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

    Default How to fill the grid with the color?

    Hi all

    I m working on Qt4.2 on Intel MAC machine.

    My Problem is this : I am having a grid or say Matrix of(16x16) i.e 16 rows and 16 columns

    on the Widget and what i m doing is this when i click on the button the first row filled with the color when i again click on the button the second row filled with the color and so on..
    i.e when second row fills with the color the first row that is filled with the color is washed away.
    But I want is this on click of the button the color of the first row should not be washed away It still remains on the row.

    If anybody understands my prob.. then pls help

    Thanx
    Always Believe in Urself
    Merry

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

    Default Re: How to fill the grid with the color?

    Are you using QTableView or a simple widget? Could you show us your 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: How to fill the grid with the color?

    Hi I am using QWidget


    mainWindow.h
    Qt Code:
    1. ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QString>
    6.  
    7. class QClipboard;
    8. class QComboBox;
    9. class QLineEdit;
    10. class QCheckBox;
    11. class CharacterWidget;
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. MainWindow();
    19. public slots:
    20.  
    21. void fillGrid();
    22.  
    23. private:
    24. CharacterWidget *characterWidget;
    25. QClipboard *clipboard;
    26. QScrollArea *scrollArea;
    27. QPushButton *button;
    28. };
    29.  
    30. #endif
    To copy to clipboard, switch view to plain text mode 

    characterwidget.h

    Qt Code:
    1. #ifndef CHARACTERWIDGET_H
    2. #define CHARACTERWIDGET_H
    3.  
    4. #include <QFont>
    5. #include <QPoint>
    6. #include <QSize>
    7. #include <QString>
    8. #include <QWidget>
    9.  
    10.  
    11.  
    12. class CharacterWidget : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. CharacterWidget(QWidget *parent = 0);
    18. QSize sizeHint() const;
    19. void paintEvent(QPaintEvent *event);
    20.  
    21. protected:
    22. void mouseMoveEvent(QMouseEvent *event);
    23.  
    24.  
    25. private:
    26.  
    27. int columns;
    28. int lastKey;
    29. int squareSize;
    30.  
    31. public:
    32. QTimer *timer;
    33. int irow;
    34. int icol;
    35. void FillColor();
    36.  
    37. };
    38.  
    39. #endif
    To copy to clipboard, switch view to plain text mode 

    main.cpp


    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "mainwindow.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. MainWindow window;
    9. window.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include "characterwidget.h"
    3. #include "mainwindow.h"
    4.  
    5.  
    6. MainWindow::MainWindow()
    7. {
    8. QWidget *centralWidget = new QWidget;
    9. scrollArea = new QScrollArea;
    10. button = new QPushButton();
    11. characterWidget = new CharacterWidget;
    12. scrollArea->setWidget(characterWidget);
    13. clipboard = QApplication::clipboard();
    14. QHBoxLayout *controlsLayout = new QHBoxLayout;
    15. controlsLayout->addStretch(1);
    16. QHBoxLayout *lineLayout = new QHBoxLayout;
    17. lineLayout->addSpacing(12);
    18. QVBoxLayout *centralLayout = new QVBoxLayout;
    19. centralLayout->addLayout(controlsLayout);
    20. centralLayout->addWidget(scrollArea, 1);
    21. centralLayout->addWidget(button, 1);
    22. centralLayout->addSpacing(4);
    23. centralLayout->addLayout(lineLayout);
    24. centralWidget->setLayout(centralLayout);
    25. setCentralWidget(centralWidget);
    26. connect(button,SIGNAL(clicked()),this,SLOT(fillGrid()));
    27. }
    28.  
    29. void MainWindow::fillGrid()
    30. {
    31. characterWidget->FillColor();
    32. }
    To copy to clipboard, switch view to plain text mode 

    characterwidget.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include "characterwidget.h"
    3. #include "mainwindow.h"
    4.  
    5. bool flag=false;
    6.  
    7. CharacterWidget::CharacterWidget(QWidget *parent)
    8. : QWidget(parent)
    9. {
    10. squareSize = 16;
    11. columns = 16;
    12. irow =0;
    13. icol =0;
    14. setMouseTracking(true);
    15.  
    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.  
    42. painter.fillRect(event->rect(), QBrush(Qt::white));
    43. QRect redrawRect = event->rect();
    44. int beginRow = redrawRect.top()/squareSize;
    45. int endRow = redrawRect.bottom()/squareSize;
    46. int beginColumn = redrawRect.left()/squareSize;
    47. int endColumn = redrawRect.right()/squareSize;
    48.  
    49. painter.setPen(QPen(Qt::gray));
    50.  
    51. for (int row = beginRow; row <= endRow;++row)
    52. {
    53. for (int column = beginColumn; column <= endColumn; ++column)
    54. {
    55. painter.drawRect(column*squareSize, row*squareSize, squareSize, squareSize);
    56. }
    57. }
    58. painter.setPen(QPen(Qt::black));
    59.  
    60.  
    61. if(flag == true)
    62. {
    63. int row = irow;
    64.  
    65. {
    66.  
    67. for (int column =icol; column<=endColumn;++column)
    68.  
    69.  
    70. {
    71. painter.setClipRect(column*squareSize, row*squareSize, squareSize, squareSize);
    72. painter.fillRect(column*squareSize + 1, row*squareSize+ 1, squareSize, squareSize, QBrush(Qt::green));
    73.  
    74.  
    75. icol++;
    76. if(icol==16)
    77. {
    78. icol=0;
    79. irow++;
    80.  
    81. }
    82.  
    83.  
    84. }
    85.  
    86. }
    87. }
    88.  
    89. }
    To copy to clipboard, switch view to plain text mode 


    Please review my code .

    Thanx
    Last edited by wysota; 18th June 2007 at 11:04. Reason: changed [quote] to [code]
    Always Believe in Urself
    Merry

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

    Default Re: How to fill the grid with the color?

    1. mouse events receive coordinates in local coordinates therefore you shouldn't map them from global
    2. You can just use setToolTip() for your widget and have the same effect as your mouseMoveEvent+mouseTracking.
    3. Why is the "flag" variable global?
    4. What is irow?
    5. Your paint event is awfully complicated. Could you simplify or annotate it?

Similar Threads

  1. Using a QFrame as a color selection indicator
    By KShots in forum Qt Tools
    Replies: 8
    Last Post: 14th June 2011, 23:55
  2. How to fill the blocks of the Grid one by one?
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 6th June 2007, 12:20

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.