PDA

View Full Version : How to fill the grid with the color?



merry
18th June 2007, 06:24
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:confused:

wysota
18th June 2007, 10:08
Are you using QTableView or a simple widget? Could you show us your code?

merry
18th June 2007, 10:37
Hi I am using QWidget


mainWindow.h



ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QString>

class QClipboard;
class QComboBox;
class QFontComboBox;
class QLineEdit;
class QScrollArea;
class QCheckBox;
class CharacterWidget;
class QPushButton;
class QPaintEvent;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();
public slots:

void fillGrid();

private:
CharacterWidget *characterWidget;
QClipboard *clipboard;
QScrollArea *scrollArea;
QPushButton *button;
};

#endif

characterwidget.h


#ifndef CHARACTERWIDGET_H
#define CHARACTERWIDGET_H

#include <QFont>
#include <QPoint>
#include <QSize>
#include <QString>
#include <QWidget>


class QPaintEvent;

class CharacterWidget : public QWidget
{
Q_OBJECT

public:
CharacterWidget(QWidget *parent = 0);
QSize sizeHint() const;
void paintEvent(QPaintEvent *event);

protected:
void mouseMoveEvent(QMouseEvent *event);


private:

int columns;
int lastKey;
int squareSize;

public:
QTimer *timer;
int irow;
int icol;
void FillColor();

};

#endif

main.cpp



#include <QApplication>

#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}

mainwindow.cpp


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


MainWindow::MainWindow()
{
QWidget *centralWidget = new QWidget;
scrollArea = new QScrollArea;
button = new QPushButton();
characterWidget = new CharacterWidget;
scrollArea->setWidget(characterWidget);
clipboard = QApplication::clipboard();
QHBoxLayout *controlsLayout = new QHBoxLayout;
controlsLayout->addStretch(1);
QHBoxLayout *lineLayout = new QHBoxLayout;
lineLayout->addSpacing(12);
QVBoxLayout *centralLayout = new QVBoxLayout;
centralLayout->addLayout(controlsLayout);
centralLayout->addWidget(scrollArea, 1);
centralLayout->addWidget(button, 1);
centralLayout->addSpacing(4);
centralLayout->addLayout(lineLayout);
centralWidget->setLayout(centralLayout);
setCentralWidget(centralWidget);
connect(button,SIGNAL(clicked()),this,SLOT(fillGri d()));
}

void MainWindow::fillGrid()
{
characterWidget->FillColor();
}

characterwidget.cpp


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

bool flag=false;

CharacterWidget::CharacterWidget(QWidget *parent)
: QWidget(parent)
{
squareSize = 16;
columns = 16;
irow =0;
icol =0;
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)
{
int row = irow;

{

for (int column =icol; column<=endColumn;++column)


{
painter.setClipRect(column*squareSize, row*squareSize, squareSize, squareSize);
painter.fillRect(column*squareSize + 1, row*squareSize+ 1, squareSize, squareSize, QBrush(Qt::green));


icol++;
if(icol==16)
{
icol=0;
irow++;

}


}

}
}

}



Please review my code .

Thanx

wysota
18th June 2007, 11:10
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?